blob: c45a74e9a31c97a927d6ac030a4158b6886fb9a1 [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 Tarreauc7e42382012-08-24 19:22:53 +020022#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020025#include <common/debug.h>
Bhaskar98634f02013-10-29 23:30:51 -040026#include <common/hash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010029#include <common/namespace.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010033#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020034#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020037#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020038#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010039#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020040#include <proto/lb_fwlc.h>
41#include <proto/lb_fwrr.h>
42#include <proto/lb_map.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/log.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010044#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010045#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020046#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020048#include <proto/proto_tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010051#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020053#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010054#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/task.h>
56
Willy Tarreau732eac42015-07-09 11:40:25 +020057#ifdef USE_OPENSSL
58#include <proto/ssl_sock.h>
59#endif /* USE_OPENSSL */
60
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050061int be_lastsession(const struct proxy *be)
62{
63 if (be->be_counters.last_sess)
64 return now.tv_sec - be->be_counters.last_sess;
65
66 return -1;
67}
68
Bhaskar98634f02013-10-29 23:30:51 -040069/* helper function to invoke the correct hash method */
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070070static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040071{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070072 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040073
74 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
75 case BE_LB_HFCN_DJB2:
76 hash = hash_djb2(key, len);
77 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010078 case BE_LB_HFCN_WT6:
79 hash = hash_wt6(key, len);
80 break;
Willy Tarreau324f07f2015-01-20 19:44:50 +010081 case BE_LB_HFCN_CRC32:
82 hash = hash_crc32(key, len);
83 break;
Bhaskar98634f02013-10-29 23:30:51 -040084 case BE_LB_HFCN_SDBM:
85 /* this is the default hash function */
86 default:
87 hash = hash_sdbm(key, len);
88 break;
89 }
90
91 return hash;
92}
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/*
95 * This function recounts the number of usable active and backup servers for
96 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010097 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010098 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010099 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200101void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
103 struct server *srv;
104
Willy Tarreau20697042007-11-15 23:26:18 +0100105 px->srv_act = px->srv_bck = 0;
106 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100107 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200109 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100110 continue;
111
Willy Tarreauc93cd162014-05-13 15:54:22 +0200112 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100113 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100114 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100115 px->lbprm.fbck = srv;
116 px->srv_bck++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400117 srv->cumulative_weight = px->lbprm.tot_wbck;
Willy Tarreaub625a082007-11-26 01:15:43 +0100118 px->lbprm.tot_wbck += srv->eweight;
119 } else {
120 px->srv_act++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400121 srv->cumulative_weight = px->lbprm.tot_wact;
Willy Tarreaub625a082007-11-26 01:15:43 +0100122 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 }
124 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100125}
Willy Tarreau20697042007-11-15 23:26:18 +0100126
Willy Tarreaub625a082007-11-26 01:15:43 +0100127/* This function simply updates the backend's tot_weight and tot_used values
128 * after servers weights have been updated. It is designed to be used after
129 * recount_servers() or equivalent.
130 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200131void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100132{
Willy Tarreau20697042007-11-15 23:26:18 +0100133 if (px->srv_act) {
134 px->lbprm.tot_weight = px->lbprm.tot_wact;
135 px->lbprm.tot_used = px->srv_act;
136 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100137 else if (px->lbprm.fbck) {
138 /* use only the first backup server */
139 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
140 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100141 }
142 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100143 px->lbprm.tot_weight = px->lbprm.tot_wbck;
144 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100145 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100146}
147
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200148/*
149 * This function tries to find a running server for the proxy <px> following
150 * the source hash method. Depending on the number of active/backup servers,
151 * it will either look for active servers, or for backup servers.
152 * If any server is found, it will be returned. If no valid server is found,
153 * NULL is returned.
154 */
Christopher Fauletf0614e82017-06-09 14:20:29 +0200155static struct server *get_server_sh(struct proxy *px, const char *addr, int len)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200156{
157 unsigned int h, l;
158
159 if (px->lbprm.tot_weight == 0)
160 return NULL;
161
162 l = h = 0;
163
164 /* note: we won't hash if there's only one server left */
165 if (px->lbprm.tot_used == 1)
166 goto hash_done;
167
168 while ((l + sizeof (int)) <= len) {
169 h ^= ntohl(*(unsigned int *)(&addr[l]));
170 l += sizeof (int);
171 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500172 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100173 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200174 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200175 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
176 return chash_get_server_hash(px, h);
177 else
178 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200179}
180
181/*
182 * This function tries to find a running server for the proxy <px> following
183 * the URI hash method. In order to optimize cache hits, the hash computation
184 * ends at the question mark. Depending on the number of active/backup servers,
185 * it will either look for active servers, or for backup servers.
186 * If any server is found, it will be returned. If no valid server is found,
187 * NULL is returned.
188 *
189 * This code was contributed by Guillaume Dallaire, who also selected this hash
190 * algorithm out of a tens because it gave him the best results.
191 *
192 */
Christopher Fauletf0614e82017-06-09 14:20:29 +0200193static struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200194{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700195 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200196 int c;
197 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400198 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200199
200 if (px->lbprm.tot_weight == 0)
201 return NULL;
202
203 /* note: we won't hash if there's only one server left */
204 if (px->lbprm.tot_used == 1)
205 goto hash_done;
206
207 if (px->uri_len_limit)
208 uri_len = MIN(uri_len, px->uri_len_limit);
209
Bhaskar98634f02013-10-29 23:30:51 -0400210 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200211 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200212 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200213 if (c == '/') {
214 slashes++;
215 if (slashes == px->uri_dirs_depth1) /* depth+1 */
216 break;
217 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100218 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200219 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200220 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200221 }
Bhaskar98634f02013-10-29 23:30:51 -0400222
223 hash = gen_hash(px, start, (end - start));
224
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500225 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100226 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200227 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200228 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
229 return chash_get_server_hash(px, hash);
230 else
231 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200232}
233
Willy Tarreau01732802007-11-01 22:48:15 +0100234/*
235 * This function tries to find a running server for the proxy <px> following
236 * the URL parameter hash method. It looks for a specific parameter in the
237 * URL and hashes it to compute the server ID. This is useful to optimize
238 * performance by avoiding bounces between servers in contexts where sessions
239 * are shared but cookies are not usable. If the parameter is not found, NULL
240 * is returned. If any server is found, it will be returned. If no valid server
241 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100242 */
243struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
244{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700245 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400246 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200247 const char *p;
248 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100249 int plen;
250
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200251 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100252 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100253 return NULL;
254
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200255 if ((p = memchr(uri, '?', uri_len)) == NULL)
256 return NULL;
257
Willy Tarreau01732802007-11-01 22:48:15 +0100258 p++;
259
260 uri_len -= (p - uri);
261 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100263
264 while (uri_len > plen) {
265 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266 if (params[plen] == '=') {
267 if (memcmp(params, px->url_param_name, plen) == 0) {
268 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100269 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200270 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100271 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200272 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400273 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200274 uri_len -= plen + 1;
275
Bhaskar98634f02013-10-29 23:30:51 -0400276 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100277 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400278 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100279 }
Bhaskar98634f02013-10-29 23:30:51 -0400280 hash = gen_hash(px, start, (end - start));
281
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500282 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100283 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400284
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200285 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
286 return chash_get_server_hash(px, hash);
287 else
288 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100289 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200290 }
291 /* skip to next parameter */
292 p = memchr(params, '&', uri_len);
293 if (!p)
294 return NULL;
295 p++;
296 uri_len -= (p - params);
297 params = p;
298 }
299 return NULL;
300}
301
302/*
303 * this does the same as the previous server_ph, but check the body contents
304 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200305struct server *get_server_ph_post(struct stream *s)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200306{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700307 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200308 struct http_txn *txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100309 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 struct http_msg *msg = &txn->req;
311 struct proxy *px = s->be;
312 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200313 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200314 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100315 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400316 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317
Willy Tarreau157dd632009-12-06 19:18:09 +0100318 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319 return NULL;
320
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200321 if (len > req->buf->data + req->buf->size - p)
322 len = req->buf->data + req->buf->size - p;
323
Willy Tarreau157dd632009-12-06 19:18:09 +0100324 if (px->lbprm.tot_weight == 0)
325 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200326
327 while (len > plen) {
328 /* Look for the parameter name followed by an equal symbol */
329 if (params[plen] == '=') {
330 if (memcmp(params, px->url_param_name, plen) == 0) {
331 /* OK, we have the parameter here at <params>, and
332 * the value after the equal sign, at <p>
333 * skip the equal symbol
334 */
335 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400336 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200337 len -= plen + 1;
338
Bhaskar98634f02013-10-29 23:30:51 -0400339 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200340 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100341 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400342 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100343 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200344 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
345 break;
346 return NULL; /* oh, no; this is not uri-encoded.
347 * This body does not contain parameters.
348 */
349 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200350 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400351 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200352 /* should we break if vlen exceeds limit? */
353 }
Bhaskar98634f02013-10-29 23:30:51 -0400354 hash = gen_hash(px, start, (end - start));
355
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500356 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100357 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400358
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200359 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
360 return chash_get_server_hash(px, hash);
361 else
362 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200363 }
364 }
Willy Tarreau01732802007-11-01 22:48:15 +0100365 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200366 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100367 if (!p)
368 return NULL;
369 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200370 len -= (p - params);
371 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100372 }
373 return NULL;
374}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200376
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377/*
Benoitaffb4812009-03-25 13:02:10 +0100378 * This function tries to find a running server for the proxy <px> following
379 * the Header parameter hash method. It looks for a specific parameter in the
380 * URL and hashes it to compute the server ID. This is useful to optimize
381 * performance by avoiding bounces between servers in contexts where sessions
382 * are shared but cookies are not usable. If the parameter is not found, NULL
383 * is returned. If any server is found, it will be returned. If no valid server
384 * is found, NULL is returned.
385 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200386struct server *get_server_hh(struct stream *s)
Benoitaffb4812009-03-25 13:02:10 +0100387{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700388 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200389 struct http_txn *txn = s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100390 struct proxy *px = s->be;
391 unsigned int plen = px->hh_len;
392 unsigned long len;
393 struct hdr_ctx ctx;
394 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400395 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100396
397 /* tot_weight appears to mean srv_count */
398 if (px->lbprm.tot_weight == 0)
399 return NULL;
400
Benoitaffb4812009-03-25 13:02:10 +0100401 ctx.idx = 0;
402
403 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100404 http_find_header2(px->hh_name, plen, b_ptr(s->req.buf, -http_hdr_rewind(&txn->req)), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100405
406 /* if the header is not found or empty, let's fallback to round robin */
407 if (!ctx.idx || !ctx.vlen)
408 return NULL;
409
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200410 /* note: we won't hash if there's only one server left */
411 if (px->lbprm.tot_used == 1)
412 goto hash_done;
413
Benoitaffb4812009-03-25 13:02:10 +0100414 /* Found a the hh_name in the headers.
415 * we will compute the hash based on this value ctx.val.
416 */
417 len = ctx.vlen;
418 p = (char *)ctx.line + ctx.val;
419 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400420 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100421 } else {
422 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100423 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100424 /* special computation, use only main domain name, not tld/host
425 * going back from the end of string, start hashing at first
426 * dot stop at next.
427 * This is designed to work with the 'Host' header, and requires
428 * a special option to activate this.
429 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100430 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100431 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100432 if (dohash) {
433 /* Rewind the pointer until the previous char
434 * is a dot, this will allow to set the start
435 * position of the domain. */
436 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100437 break;
Benoitaffb4812009-03-25 13:02:10 +0100438 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100439 else if (*p == '.') {
440 /* The pointer is rewinded to the dot before the
441 * tld, we memorize the end of the domain and
442 * can enter the domain processing. */
443 end = p;
444 dohash = 1;
445 }
Benoitaffb4812009-03-25 13:02:10 +0100446 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100447 len--;
Benoitaffb4812009-03-25 13:02:10 +0100448 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100449 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400450 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100451 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500452 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100453 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200454 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200455 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
456 return chash_get_server_hash(px, hash);
457 else
458 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100459}
460
Willy Tarreau34db1082012-04-19 17:16:54 +0200461/* RDP Cookie HASH. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200462struct server *get_server_rch(struct stream *s)
Emeric Brun736aa232009-06-30 17:56:00 +0200463{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700464 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200465 struct proxy *px = s->be;
466 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200467 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200468 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200469 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200470
471 /* tot_weight appears to mean srv_count */
472 if (px->lbprm.tot_weight == 0)
473 return NULL;
474
Willy Tarreau37406352012-04-23 16:16:37 +0200475 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200476
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100477 b_rew(s->req.buf, rewind = s->req.buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200478
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200479 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200480 len = smp.data.u.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100481
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100482 b_adv(s->req.buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200483
Willy Tarreau37406352012-04-23 16:16:37 +0200484 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200485 return NULL;
486
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200487 /* note: we won't hash if there's only one server left */
488 if (px->lbprm.tot_used == 1)
489 goto hash_done;
490
Emeric Brun736aa232009-06-30 17:56:00 +0200491 /* Found a the hh_name in the headers.
492 * we will compute the hash based on this value ctx.val.
493 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200494 hash = gen_hash(px, smp.data.u.str.str, len);
Bhaskar98634f02013-10-29 23:30:51 -0400495
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500496 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100497 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200498 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200499 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
500 return chash_get_server_hash(px, hash);
501 else
502 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200503}
Benoitaffb4812009-03-25 13:02:10 +0100504
505/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200506 * This function applies the load-balancing algorithm to the stream, as
507 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200508 * 'assigned'.
509 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200510 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200511 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100512 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 * The function tries to keep the original connection slot if it reconnects to
514 * the same server, otherwise it releases it and tries to offer it.
515 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200516 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517 *
518 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100519 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200520 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
521 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 * SRV_STATUS_INTERNAL for other unrecoverable errors.
523 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200524 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100525 * it does not need to be called anymore. This means that target_srv(&s->target)
526 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527 *
528 */
529
Willy Tarreau87b09662015-04-03 00:22:06 +0200530int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200532 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200533 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100534 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100536
Simon Horman8effd3d2011-08-13 08:03:52 +0900537 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538
Willy Tarreau7c669d72008-06-20 15:04:11 +0200539 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200540 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200541 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100542
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100543 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200544 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545
Willy Tarreau7c669d72008-06-20 15:04:11 +0200546 /* We have to release any connection slot before applying any LB algo,
547 * otherwise we may erroneously end up with no available slot.
548 */
549 if (conn_slot)
550 sess_change_server(s, NULL);
551
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100552 /* We will now try to find the good server and store it into <objt_server(s->target)>.
553 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200554 * as well as if no server is available (check error code).
555 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100556
Willy Tarreau827aee92011-03-10 16:55:02 +0100557 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100558 s->target = NULL;
Willy Tarreau350f4872014-11-28 14:42:25 +0100559 conn = objt_conn(s->si[1].end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100560
Willy Tarreau068621e2013-12-23 15:11:25 +0100561 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100562 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100563 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreaueee5b512015-04-03 23:46:31 +0200564 ((s->txn && s->txn->flags & TX_PREFER_LAST) ||
Willy Tarreauc35362a2014-04-25 13:58:37 +0200565 ((s->be->options & PR_O_PREF_LAST) &&
Olivier Doucet1ca1b6f2017-01-02 11:48:57 +0100566 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200567 (!s->be->max_ka_queue ||
568 server_has_room(__objt_server(conn->target)) ||
569 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200570 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200571 /* This stream was relying on a server in a previous request
Olivier Doucet1ca1b6f2017-01-02 11:48:57 +0100572 * and the proxy has "option prefer-last-server" set
573 * and balance algorithm dont tell us to do otherwise, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100574 * let's try to reuse the same server.
575 */
576 srv = __objt_server(conn->target);
577 s->target = &srv->obj_type;
578 }
579 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200580 /* we must check if we have at least one server available */
581 if (!s->be->lbprm.tot_weight) {
582 err = SRV_STATUS_NOSRV;
583 goto out;
584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 /* First check whether we need to fetch some data or simply call
587 * the LB lookup function. Only the hashing functions will need
588 * some input data in fact, and will support multiple algorithms.
589 */
590 switch (s->be->lbprm.algo & BE_LB_LKUP) {
591 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200593 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594
Willy Tarreauf09c6602012-02-13 17:12:08 +0100595 case BE_LB_LKUP_FSTREE:
596 srv = fas_get_next_server(s->be, prev_srv);
597 break;
598
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200599 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100600 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200601 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200602
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200603 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200605 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200606 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200610 break;
611 }
612 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200614 err = SRV_STATUS_INTERNAL;
615 goto out;
616 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200617
618 switch (s->be->lbprm.algo & BE_LB_PARM) {
619 case BE_LB_HASH_SRC:
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200620 conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200621 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200622 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200623 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200624 4);
625 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200626 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200627 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200628 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200629 16);
630 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200631 else {
632 /* unknown IP family */
633 err = SRV_STATUS_INTERNAL;
634 goto out;
635 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200636 break;
637
638 case BE_LB_HASH_URI:
639 /* URI hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200640 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100641 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100642 srv = get_server_uh(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200643 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
644 s->txn->req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200645 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200646
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 case BE_LB_HASH_PRM:
648 /* URL Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200649 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100650 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100651
Willy Tarreau827aee92011-03-10 16:55:02 +0100652 srv = get_server_ph(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200653 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
654 s->txn->req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100655
Willy Tarreaueee5b512015-04-03 23:46:31 +0200656 if (!srv && s->txn->meth == HTTP_METH_POST)
Willy Tarreau827aee92011-03-10 16:55:02 +0100657 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200658 break;
Benoitaffb4812009-03-25 13:02:10 +0100659
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200660 case BE_LB_HASH_HDR:
661 /* Header Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200662 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100663 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100664 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200665 break;
666
667 case BE_LB_HASH_RDP:
668 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100669 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200670 break;
671
672 default:
673 /* unknown balancing algorithm */
674 err = SRV_STATUS_INTERNAL;
675 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100676 }
Emeric Brun736aa232009-06-30 17:56:00 +0200677
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200678 /* If the hashing parameter was not found, let's fall
679 * back to round robin on the map.
680 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100681 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200682 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100683 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200684 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100685 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200686 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687
688 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200689 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200690
Willy Tarreau7c669d72008-06-20 15:04:11 +0200691 default:
692 /* unknown balancing algorithm */
693 err = SRV_STATUS_INTERNAL;
694 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200696
Willy Tarreau827aee92011-03-10 16:55:02 +0100697 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200698 err = SRV_STATUS_FULL;
699 goto out;
700 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100701 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100702 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100703 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100704 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100705 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200706 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200707 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100708 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100710 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau350f4872014-11-28 14:42:25 +0100711 (conn = objt_conn(s->si[1].end)) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200712 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100713 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100714 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100715 }
716 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200717 err = SRV_STATUS_NOSRV;
718 goto out;
719 }
720
Willy Tarreaue7dff022015-04-03 01:14:29 +0200721 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200722 err = SRV_STATUS_OK;
723 out:
724
725 /* Either we take back our connection slot, or we offer it to someone
726 * else if we don't need it anymore.
727 */
728 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100729 if (conn_slot == srv) {
730 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200731 } else {
732 if (may_dequeue_tasks(conn_slot, s->be))
733 process_srv_queue(conn_slot);
734 }
735 }
736
737 out_err:
738 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739}
740
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741/*
Willy Tarreaue7dff022015-04-03 01:14:29 +0200742 * This function assigns a server address to a stream, and sets SF_ADDR_SET.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 * The address is taken from the currently assigned server, or from the
744 * dispatch or transparent address.
745 *
746 * It may return :
747 * SRV_STATUS_OK if everything is OK.
748 * SRV_STATUS_INTERNAL for other unrecoverable errors.
749 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200750 * Upon successful return, the stream flag SF_ADDR_SET is set. This flag is
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751 * not cleared, so it's to the caller to clear it if required.
752 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200753 * The caller is responsible for having already assigned a connection
754 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200755 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200757int assign_server_address(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200759 struct connection *cli_conn = objt_conn(strm_orig(s));
Willy Tarreau350f4872014-11-28 14:42:25 +0100760 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200761
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762#ifdef DEBUG_FULL
763 fprintf(stderr,"assign_server_address : s=%p\n",s);
764#endif
765
Willy Tarreaue7dff022015-04-03 01:14:29 +0200766 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200767 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 return SRV_STATUS_INTERNAL;
770
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200771 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreau04276f32017-01-06 17:41:29 +0100772 set_host_port(&srv_conn->addr.to, objt_server(s->target)->svc_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200775 /* if the server has no address, we use the same address
776 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200777 * locally on multiple addresses at once. Nothing is done
778 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200779 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200781
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200782 if (cli_conn->addr.to.ss_family == AF_INET) {
783 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
784 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
785 ((struct sockaddr_in6 *)&srv_conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200786 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200787 }
788
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 /* if this server remaps proxied ports, we'll use
790 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200791 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100792 int base_port;
793
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200794 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100795
796 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200797 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100798
799 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200800 base_port += get_host_port(&srv_conn->addr.to);
801 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 }
803 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200804 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200806 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200808 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200810 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200812 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
813 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100815 else if (s->be->options & PR_O_HTTP_PROXY) {
816 /* If HTTP PROXY option is set, then server is already assigned
817 * during incoming client request parsing. */
818 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100819 else {
820 /* no server and no LB algorithm ! */
821 return SRV_STATUS_INTERNAL;
822 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100824 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100825 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100826
Willy Tarreaue7dff022015-04-03 01:14:29 +0200827 s->flags |= SF_ADDR_SET;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828 return SRV_STATUS_OK;
829}
830
Willy Tarreau87b09662015-04-03 00:22:06 +0200831/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200833 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200834 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200835 * be called before any connection and after any retry or redispatch occurs.
836 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200837 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 *
839 * Returns :
840 *
841 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100842 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 * SRV_STATUS_QUEUED if the connection has been queued.
844 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100845 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 * SRV_STATUS_INTERNAL for other unrecoverable errors.
848 *
849 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200850int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851{
852 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100853 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 int err;
855
856 if (s->pend_pos)
857 return SRV_STATUS_INTERNAL;
858
Willy Tarreau7c669d72008-06-20 15:04:11 +0200859 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100861 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100862
Willy Tarreau7c669d72008-06-20 15:04:11 +0200863 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100864 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200865 /* This stream was previously assigned to a server. We have to
866 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200867 * - if the server changed :
868 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200869 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200870 * - increment srv->redispatches and be->redispatches
871 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100872 */
873
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100874 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200875 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
876 s->txn->flags &= ~TX_CK_MASK;
877 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200878 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200879 s->flags |= SF_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100880 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100881 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200882 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100883 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100884 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100885 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100886 }
887 }
888
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889 switch (err) {
890 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200891 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100892 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100893 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200894 return SRV_STATUS_OK; /* dispatch or proxy mode */
895
896 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100897 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200898 return SRV_STATUS_OK;
899
Willy Tarreau87b09662015-04-03 00:22:06 +0200900 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901 * connection slot yet. Either it is a redispatch, or it was
902 * assigned from persistence information (direct mode).
903 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200904 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200905 /* server scheduled for redirection, and already assigned. We
906 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100907 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100908 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100909 return SRV_STATUS_OK;
910 }
911
Willy Tarreau87b09662015-04-03 00:22:06 +0200912 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200913 * We know we have to queue it into the server's queue, so if a maxqueue
914 * is set on the server, we must also check that the server's queue is
915 * not full, in which case we have to return FULL.
916 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100917 if (srv->maxconn &&
918 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919
Willy Tarreau827aee92011-03-10 16:55:02 +0100920 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200921 return SRV_STATUS_FULL;
922
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923 p = pendconn_add(s);
924 if (p)
925 return SRV_STATUS_QUEUED;
926 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200927 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200929
930 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 return SRV_STATUS_OK;
933
934 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +0200935 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936 p = pendconn_add(s);
937 if (p)
938 return SRV_STATUS_QUEUED;
939 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200940 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941
942 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200943 return err;
944
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 case SRV_STATUS_INTERNAL:
946 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200947
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 default:
949 return SRV_STATUS_INTERNAL;
950 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100951}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952
Willy Tarreaub1d67742010-03-29 19:36:59 +0200953/* If an explicit source binding is specified on the server and/or backend, and
954 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 * assigned to the stream's pending connection. This function assumes that an
Willy Tarreau350f4872014-11-28 14:42:25 +0100956 * outgoing connection has already been assigned to s->si[1].end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200957 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200958static void assign_tproxy_address(struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +0200959{
Willy Tarreau29fbe512015-08-20 19:35:14 +0200960#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100961 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100962 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200963 struct connection *cli_conn;
Willy Tarreau350f4872014-11-28 14:42:25 +0100964 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100965
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100966 if (srv && srv->conn_src.opts & CO_SRC_BIND)
967 src = &srv->conn_src;
968 else if (s->be->conn_src.opts & CO_SRC_BIND)
969 src = &s->be->conn_src;
970 else
971 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100972
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100973 switch (src->opts & CO_SRC_TPROXY_MASK) {
974 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200975 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100976 break;
977 case CO_SRC_TPROXY_CLI:
978 case CO_SRC_TPROXY_CIP:
979 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200980 cli_conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200981 if (cli_conn)
982 srv_conn->addr.from = cli_conn->addr.from;
983 else
984 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100985 break;
986 case CO_SRC_TPROXY_DYN:
Willy Tarreaueee5b512015-04-03 23:46:31 +0200987 if (src->bind_hdr_occ && s->txn) {
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100988 char *vptr;
989 int vlen;
990 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100991
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100992 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200993 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
994 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
995 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100996
Willy Tarreaueee5b512015-04-03 23:46:31 +0200997 b_rew(s->req.buf, rewind = http_hdr_rewind(&s->txn->req));
998 if (http_get_hdr(&s->txn->req, src->bind_hdr_name, src->bind_hdr_len,
999 &s->txn->hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001000 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001001 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +02001002 }
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001003 b_adv(s->req.buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001004 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001005 break;
1006 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001007 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001008 }
1009#endif
1010}
1011
1012
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001014 * This function initiates a connection to the server assigned to this stream
Willy Tarreau350f4872014-11-28 14:42:25 +01001015 * (s->target, s->si[1].addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001016 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001018 * - SF_ERR_NONE if everything's OK
1019 * - SF_ERR_SRVTO if there are no more servers
1020 * - SF_ERR_SRVCL if the connection was refused by the server
1021 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1022 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1023 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001024 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001025 * The server-facing stream interface is expected to hold a pre-allocated connection
Willy Tarreau350f4872014-11-28 14:42:25 +01001026 * in s->si[1].conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001028int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001029{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001030 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001031 struct connection *srv_conn;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001032 struct connection *old_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001033 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001034 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001035 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036
Willy Tarreau7b004922015-08-04 19:34:21 +02001037 srv = objt_server(s->target);
Willy Tarreau350f4872014-11-28 14:42:25 +01001038 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau34601a82013-12-15 16:33:46 +01001039 if (srv_conn)
1040 reuse = s->target == srv_conn->target;
1041
Willy Tarreau8dff9982015-08-04 20:45:52 +02001042 if (srv && !reuse) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001043 old_conn = srv_conn;
1044 if (old_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001045 srv_conn = NULL;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001046 old_conn->owner = NULL;
1047 si_detach_endpoint(&s->si[1]);
1048 /* note: if the connection was in a server's idle
1049 * queue, it doesn't get dequeued.
1050 */
Willy Tarreau8dff9982015-08-04 20:45:52 +02001051 }
1052
Willy Tarreau449d74a2015-08-05 17:16:33 +02001053 /* Below we pick connections from the safe or idle lists based
1054 * on the strategy, the fact that this is a first or second
1055 * (retryable) request, with the indicated priority (1 or 2) :
1056 *
1057 * SAFE AGGR ALWS
1058 *
1059 * +-----+-----+ +-----+-----+ +-----+-----+
1060 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1061 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1062 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1063 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1064 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1065 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1066 */
1067
1068 if (!LIST_ISEMPTY(&srv->idle_conns) &&
1069 ((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR &&
1070 s->txn && (s->txn->flags & TX_NOT_FIRST))) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001071 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001072 }
1073 else if (!LIST_ISEMPTY(&srv->safe_conns) &&
1074 ((s->txn && (s->txn->flags & TX_NOT_FIRST)) ||
1075 (s->be->options & PR_O_REUSE_MASK) >= PR_O_REUSE_AGGR)) {
1076 srv_conn = LIST_ELEM(srv->safe_conns.n, struct connection *, list);
1077 }
1078 else if (!LIST_ISEMPTY(&srv->idle_conns) &&
1079 (s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) {
1080 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
1081 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001082
Willy Tarreau449d74a2015-08-05 17:16:33 +02001083 /* If we've picked a connection from the pool, we now have to
1084 * detach it. We may have to get rid of the previous idle
1085 * connection we had, so for this we try to swap it with the
1086 * other owner's. That way it may remain alive for others to
1087 * pick.
1088 */
1089 if (srv_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001090 LIST_DEL(&srv_conn->list);
1091 LIST_INIT(&srv_conn->list);
1092
Willy Tarreauefb90f92015-08-06 11:37:10 +02001093 if (srv_conn->owner) {
1094 si_detach_endpoint(srv_conn->owner);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001095 if (old_conn && !(old_conn->flags & CO_FL_PRIVATE)) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001096 si_attach_conn(srv_conn->owner, old_conn);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001097 si_idle_conn(srv_conn->owner, NULL);
1098 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001099 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001100 si_attach_conn(&s->si[1], srv_conn);
1101 reuse = 1;
1102 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001103
Willy Tarreau449d74a2015-08-05 17:16:33 +02001104 /* we may have to release our connection if we couldn't swap it */
Willy Tarreauefb90f92015-08-06 11:37:10 +02001105 if (old_conn && !old_conn->owner) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001106 LIST_DEL(&old_conn->list);
1107 conn_force_close(old_conn);
1108 conn_free(old_conn);
1109 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001110 }
1111
Willy Tarreau34601a82013-12-15 16:33:46 +01001112 if (reuse) {
1113 /* Disable connection reuse if a dynamic source is used.
1114 * As long as we don't share connections between servers,
1115 * we don't need to disable connection reuse on no-idempotent
1116 * requests nor when PROXY protocol is used.
1117 */
Willy Tarreau34601a82013-12-15 16:33:46 +01001118 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1119 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1120 reuse = 0;
1121 }
1122 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1123 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1124 reuse = 0;
1125 }
1126 }
1127
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001128 if (!reuse)
Willy Tarreau973a5422015-08-05 21:47:23 +02001129 srv_conn = si_alloc_conn(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02001130 else {
1131 /* reusing our connection, take it out of the idle list */
1132 LIST_DEL(&srv_conn->list);
1133 LIST_INIT(&srv_conn->list);
1134 }
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001135
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001136 if (!srv_conn)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001137 return SF_ERR_RESOURCE;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001138
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 if (!(s->flags & SF_ADDR_SET)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 err = assign_server_address(s);
1141 if (err != SRV_STATUS_OK)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001142 return SF_ERR_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 }
1144
Willy Tarreauff605db2013-12-20 11:09:51 +01001145 if (!conn_xprt_ready(srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001146 /* the target was only on the stream, assign it to the SI now */
Willy Tarreauff605db2013-12-20 11:09:51 +01001147 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001148
Willy Tarreauff605db2013-12-20 11:09:51 +01001149 /* set the correct protocol on the output stream interface */
Willy Tarreau7b004922015-08-04 19:34:21 +02001150 if (srv) {
1151 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), srv->xprt);
Willy Tarreauff605db2013-12-20 11:09:51 +01001152 }
1153 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1154 /* proxies exclusively run on raw_sock right now */
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001155 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), xprt_get(XPRT_RAW));
Willy Tarreau350f4872014-11-28 14:42:25 +01001156 if (!objt_conn(s->si[1].end) || !objt_conn(s->si[1].end)->ctrl)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001157 return SF_ERR_INTERNAL;
Willy Tarreauff605db2013-12-20 11:09:51 +01001158 }
1159 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001160 return SF_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001161
Willy Tarreauff605db2013-12-20 11:09:51 +01001162 /* process the case where the server requires the PROXY protocol to be sent */
1163 srv_conn->send_proxy_ofs = 0;
Willy Tarreau7b004922015-08-04 19:34:21 +02001164 if (srv && srv->pp_opts) {
Willy Tarreau387ebf82015-08-04 19:24:13 +02001165 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauff605db2013-12-20 11:09:51 +01001166 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001167 cli_conn = objt_conn(strm_orig(s));
Willy Tarreauff605db2013-12-20 11:09:51 +01001168 if (cli_conn)
1169 conn_get_to_addr(cli_conn);
1170 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001171
Willy Tarreau350f4872014-11-28 14:42:25 +01001172 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001173
Willy Tarreauff605db2013-12-20 11:09:51 +01001174 assign_tproxy_address(s);
1175 }
1176 else {
1177 /* the connection is being reused, just re-attach it */
Willy Tarreau350f4872014-11-28 14:42:25 +01001178 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 s->flags |= SF_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001180 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001181
William Lallemandb7ff6a32012-03-02 14:35:21 +01001182 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001183 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Willy Tarreau350f4872014-11-28 14:42:25 +01001184 s->si[1].flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001185
Willy Tarreau14f8e862012-08-30 22:23:13 +02001186 /* disable lingering */
1187 if (s->be->options & PR_O_TCP_NOLING)
Willy Tarreau350f4872014-11-28 14:42:25 +01001188 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001189
Willy Tarreau350f4872014-11-28 14:42:25 +01001190 err = si_connect(&s->si[1]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191
Willy Tarreaue7dff022015-04-03 01:14:29 +02001192 if (err != SF_ERR_NONE)
Willy Tarreau9650f372009-08-16 14:02:45 +02001193 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001194
Willy Tarreau14f8e862012-08-30 22:23:13 +02001195 /* set connect timeout */
Willy Tarreau350f4872014-11-28 14:42:25 +01001196 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001197
Willy Tarreau827aee92011-03-10 16:55:02 +01001198 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001199 s->flags |= SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001200 srv->cur_sess++;
1201 if (srv->cur_sess > srv->counters.cur_sess_max)
1202 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001203 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001204 s->be->lbprm.server_take_conn(srv);
Willy Tarreau732eac42015-07-09 11:40:25 +02001205
1206#ifdef USE_OPENSSL
1207 if (srv->ssl_ctx.sni) {
1208 struct sample *smp;
1209 int rewind;
1210
1211 /* Tricky case : we have already scheduled the pending
1212 * HTTP request or TCP data for leaving. So in HTTP we
1213 * rewind exactly the headers, otherwise we rewind the
1214 * output data.
1215 */
1216 rewind = s->txn ? http_hdr_rewind(&s->txn->req) : s->req.buf->o;
1217 b_rew(s->req.buf, rewind);
1218
1219 smp = sample_fetch_as_type(s->be, s->sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, srv->ssl_ctx.sni, SMP_T_STR);
1220
1221 /* restore the pointers */
1222 b_adv(s->req.buf, rewind);
1223
Willy Tarreau2e0565c2016-08-09 11:55:21 +02001224 if (smp_make_safe(smp)) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001225 ssl_sock_set_servername(srv_conn, smp->data.u.str.str);
Willy Tarreau387ebf82015-08-04 19:24:13 +02001226 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau732eac42015-07-09 11:40:25 +02001227 }
1228 }
1229#endif /* USE_OPENSSL */
1230
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231 }
1232
Willy Tarreaue7dff022015-04-03 01:14:29 +02001233 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001234}
1235
1236
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237/* This function performs the "redispatch" part of a connection attempt. It
1238 * will assign a server if required, queue the connection if required, and
1239 * handle errors that might arise at this level. It can change the server
1240 * state. It will return 1 if it encounters an error, switches the server
1241 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1242 * that the connection is ready to use.
1243 */
1244
Willy Tarreau87b09662015-04-03 00:22:06 +02001245int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246{
Willy Tarreau827aee92011-03-10 16:55:02 +01001247 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248 int conn_err;
1249
1250 /* We know that we don't have any connection pending, so we will
1251 * try to get a new one, and wait in this state if it's queued
1252 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001253 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001254 conn_err = assign_server_and_queue(s);
1255 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001256
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257 switch (conn_err) {
1258 case SRV_STATUS_OK:
1259 break;
1260
Willy Tarreau7c669d72008-06-20 15:04:11 +02001261 case SRV_STATUS_FULL:
1262 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1263 * and we can redispatch to another server, or it is not and we return
1264 * 503. This only makes sense in DIRECT mode however, because normal LB
1265 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001266 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001267 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001268 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001269 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001270 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001271 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001272 goto redispatch;
1273 }
1274
Willy Tarreau350f4872014-11-28 14:42:25 +01001275 if (!s->si[1].err_type) {
1276 s->si[1].err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001277 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001278
Willy Tarreau827aee92011-03-10 16:55:02 +01001279 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001280 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001281 return 1;
1282
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001284 /* note: it is guaranteed that srv == NULL here */
Willy Tarreau350f4872014-11-28 14:42:25 +01001285 if (!s->si[1].err_type) {
1286 s->si[1].err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001287 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001288
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001289 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001290 return 1;
1291
1292 case SRV_STATUS_QUEUED:
Willy Tarreau350f4872014-11-28 14:42:25 +01001293 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1294 s->si[1].state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001295 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001296 return 1;
1297
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298 case SRV_STATUS_INTERNAL:
1299 default:
Willy Tarreau350f4872014-11-28 14:42:25 +01001300 if (!s->si[1].err_type) {
1301 s->si[1].err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001302 }
1303
Willy Tarreau827aee92011-03-10 16:55:02 +01001304 if (srv)
1305 srv_inc_sess_ctr(srv);
1306 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001307 srv_set_sess_last(srv);
1308 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001309 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001310 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311
Willy Tarreau87b09662015-04-03 00:22:06 +02001312 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001313 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001314 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 return 1;
1316 }
1317 /* if we get here, it's because we got SRV_STATUS_OK, which also
1318 * means that the connection has not been queued.
1319 */
1320 return 0;
1321}
1322
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001323/* sends a log message when a backend goes down, and also sets last
1324 * change date.
1325 */
1326void set_backend_down(struct proxy *be)
1327{
1328 be->last_change = now.tv_sec;
1329 be->down_trans++;
1330
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001331 if (!(global.mode & MODE_STARTING)) {
1332 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1333 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1334 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001335}
1336
Willy Tarreau87b09662015-04-03 00:22:06 +02001337/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001338 * tries to extract an RDP cookie from the request buffer, and look for the
1339 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02001340 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001341 * list. Nothing is performed if a server was already assigned.
1342 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001343int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001344{
1345 struct proxy *px = s->be;
1346 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001347 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001348 struct server *srv = px->srv;
Willy Tarreau04276f32017-01-06 17:41:29 +01001349 uint16_t port;
1350 uint32_t addr;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001351 char *p;
1352
Willy Tarreau87b09662015-04-03 00:22:06 +02001353 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001354 now_ms, __FUNCTION__,
1355 s,
1356 req,
1357 req->rex, req->wex,
1358 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001359 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001360 req->analysers);
1361
Willy Tarreaue7dff022015-04-03 01:14:29 +02001362 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001363 goto no_cookie;
1364
Willy Tarreau37406352012-04-23 16:16:37 +02001365 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001366
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001367 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001368 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001369 goto no_cookie;
1370
Willy Tarreau04276f32017-01-06 17:41:29 +01001371 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return.
1372 * The cookie format is <ip> "." <port> where "ip" is the integer corresponding to the
1373 * server's IP address in network order, and "port" is the integer corresponding to the
1374 * server's port in network order. Comments please Emeric.
1375 */
1376 addr = strtoul(smp.data.u.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001377 if (*p != '.')
1378 goto no_cookie;
1379 p++;
Willy Tarreau04276f32017-01-06 17:41:29 +01001380
1381 port = ntohs(strtoul(p, &p, 10));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001382 if (*p != '.')
1383 goto no_cookie;
1384
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001385 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001386 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001387 if (srv->addr.ss_family == AF_INET &&
Willy Tarreau04276f32017-01-06 17:41:29 +01001388 port == srv->svc_port &&
1389 addr == ((struct sockaddr_in *)&srv->addr)->sin_addr.s_addr) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001390 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001391 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001392 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001393 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001394 break;
1395 }
1396 }
1397 srv = srv->next;
1398 }
1399
1400no_cookie:
1401 req->analysers &= ~an_bit;
1402 req->analyse_exp = TICK_ETERNITY;
1403 return 1;
1404}
1405
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001406int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001407 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001408 return px->down_time;
1409
1410 return now.tv_sec - px->last_change + px->down_time;
1411}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001413/*
1414 * This function returns a string containing the balancing
1415 * mode of the proxy in a format suitable for stats.
1416 */
1417
1418const char *backend_lb_algo_str(int algo) {
1419
1420 if (algo == BE_LB_ALGO_RR)
1421 return "roundrobin";
1422 else if (algo == BE_LB_ALGO_SRR)
1423 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001424 else if (algo == BE_LB_ALGO_FAS)
1425 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001426 else if (algo == BE_LB_ALGO_LC)
1427 return "leastconn";
1428 else if (algo == BE_LB_ALGO_SH)
1429 return "source";
1430 else if (algo == BE_LB_ALGO_UH)
1431 return "uri";
1432 else if (algo == BE_LB_ALGO_PH)
1433 return "url_param";
1434 else if (algo == BE_LB_ALGO_HH)
1435 return "hdr";
1436 else if (algo == BE_LB_ALGO_RCH)
1437 return "rdp-cookie";
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001438 else if (algo == BE_LB_ALGO_NONE)
1439 return "none";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001440 else
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001441 return "unknown";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001442}
1443
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001444/* This function parses a "balance" statement in a backend section describing
1445 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001446 * returns -1, it will write an error message into the <err> buffer which will
1447 * automatically be allocated and must be passed as NULL. The trailing '\n'
1448 * will not be written. The function must be called with <args> pointing to the
1449 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001450 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001451int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001452{
1453 if (!*(args[0])) {
1454 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001455 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1456 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001457 return 0;
1458 }
1459
1460 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001461 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1462 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001463 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001464 else if (!strcmp(args[0], "static-rr")) {
1465 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1466 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1467 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001468 else if (!strcmp(args[0], "first")) {
1469 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1470 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1471 }
Willy Tarreau51406232008-03-10 22:04:20 +01001472 else if (!strcmp(args[0], "leastconn")) {
1473 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1474 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1475 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001476 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001477 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1478 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001479 }
1480 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001481 int arg = 1;
1482
Willy Tarreau31682232007-11-29 15:38:04 +01001483 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1484 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001485
Oskar Stolc8dc41842012-05-19 10:19:54 +01001486 curproxy->uri_whole = 0;
1487
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001488 while (*args[arg]) {
1489 if (!strcmp(args[arg], "len")) {
1490 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001491 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001492 return -1;
1493 }
1494 curproxy->uri_len_limit = atoi(args[arg+1]);
1495 arg += 2;
1496 }
1497 else if (!strcmp(args[arg], "depth")) {
1498 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001499 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001500 return -1;
1501 }
1502 /* hint: we store the position of the ending '/' (depth+1) so
1503 * that we avoid a comparison while computing the hash.
1504 */
1505 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1506 arg += 2;
1507 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001508 else if (!strcmp(args[arg], "whole")) {
1509 curproxy->uri_whole = 1;
1510 arg += 1;
1511 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001512 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001513 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001514 return -1;
1515 }
1516 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001517 }
Willy Tarreau01732802007-11-01 22:48:15 +01001518 else if (!strcmp(args[0], "url_param")) {
1519 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001520 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001521 return -1;
1522 }
Willy Tarreau31682232007-11-29 15:38:04 +01001523 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1524 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001525
1526 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001527 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001528 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001529 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001530 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001531 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001532 return -1;
1533 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001534 }
Benoitaffb4812009-03-25 13:02:10 +01001535 }
1536 else if (!strncmp(args[0], "hdr(", 4)) {
1537 const char *beg, *end;
1538
1539 beg = args[0] + 4;
1540 end = strchr(beg, ')');
1541
1542 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001543 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001544 return -1;
1545 }
1546
1547 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1548 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1549
1550 free(curproxy->hh_name);
1551 curproxy->hh_len = end - beg;
1552 curproxy->hh_name = my_strndup(beg, end - beg);
1553 curproxy->hh_match_domain = 0;
1554
1555 if (*args[1]) {
1556 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001557 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001558 return -1;
1559 }
1560 curproxy->hh_match_domain = 1;
1561 }
Emeric Brun736aa232009-06-30 17:56:00 +02001562 }
1563 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1564 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1565 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1566
1567 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1568 const char *beg, *end;
1569
1570 beg = args[0] + 11;
1571 end = strchr(beg, ')');
1572
1573 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001574 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001575 return -1;
1576 }
1577
1578 free(curproxy->hh_name);
1579 curproxy->hh_name = my_strndup(beg, end - beg);
1580 curproxy->hh_len = end - beg;
1581 }
1582 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1583 free(curproxy->hh_name);
1584 curproxy->hh_name = strdup("mstshash");
1585 curproxy->hh_len = strlen(curproxy->hh_name);
1586 }
1587 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001588 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001589 return -1;
1590 }
Willy Tarreau01732802007-11-01 22:48:15 +01001591 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001592 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001593 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 +01001594 return -1;
1595 }
1596 return 0;
1597}
1598
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001599
1600/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001601/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001602/************************************************************************/
1603
Willy Tarreau34db1082012-04-19 17:16:54 +02001604/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001605 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001606 * undefined behaviour.
1607 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001608static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001609smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001610{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001611 struct proxy *px;
1612
Willy Tarreau37406352012-04-23 16:16:37 +02001613 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001614 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001615 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001616
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01001617 smp->data.u.sint = be_usable_srv(px);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001618
1619 return 1;
1620}
1621
Willy Tarreau37406352012-04-23 16:16:37 +02001622/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001623 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001624 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1625 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001626 */
1627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001628smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001629{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001630 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001631
Willy Tarreau37406352012-04-23 16:16:37 +02001632 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001633 smp->data.type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001634 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001635 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001636 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001637 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001638 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001639 return 1;
1640}
1641
Willy Tarreau34db1082012-04-19 17:16:54 +02001642/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001643 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001644 * undefined behaviour.
1645 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001646static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001647smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001648{
1649 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001650
Willy Tarreau37406352012-04-23 16:16:37 +02001651 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001652 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001653 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001654
Willy Tarreau24e32d82012-04-23 23:55:44 +02001655 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001656 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001657 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001658
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001659 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001660 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001661 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001662 return 1;
1663 }
1664
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001665 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001666 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001667 }
1668
1669 return 1;
1670}
1671
Willy Tarreaua5e37562011-12-16 17:06:15 +01001672/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001673static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001674smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001675{
Willy Tarreaube508f12016-03-10 11:47:01 +01001676 if (!smp->strm)
1677 return 0;
1678
Willy Tarreauf853c462012-04-23 18:53:56 +02001679 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001680 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001681 smp->data.u.sint = smp->strm->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001682 return 1;
1683}
1684
Marcin Deranekd2471c22016-12-12 14:08:05 +01001685/* set string to the name of the backend */
1686static int
1687smp_fetch_be_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1688{
1689 if (!smp->strm)
1690 return 0;
1691
1692 smp->data.u.str.str = (char *)smp->strm->be->id;
1693 if (!smp->data.u.str.str)
1694 return 0;
1695
1696 smp->data.type = SMP_T_STR;
1697 smp->flags = SMP_F_CONST;
1698 smp->data.u.str.len = strlen(smp->data.u.str.str);
1699
1700 return 1;
1701}
1702
Willy Tarreaua5e37562011-12-16 17:06:15 +01001703/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001705smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001706{
Willy Tarreaube508f12016-03-10 11:47:01 +01001707 if (!smp->strm)
1708 return 0;
1709
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001710 if (!objt_server(smp->strm->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001711 return 0;
1712
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001713 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001714 smp->data.u.sint = objt_server(smp->strm->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001715
1716 return 1;
1717}
1718
Willy Tarreau34db1082012-04-19 17:16:54 +02001719/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001720 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001721 * undefined behaviour.
1722 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001723static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001724smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001725{
Willy Tarreau37406352012-04-23 16:16:37 +02001726 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001727 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001728 smp->data.u.sint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001729 return 1;
1730}
1731
Willy Tarreau34db1082012-04-19 17:16:54 +02001732/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001733 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001734 * undefined behaviour.
1735 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001736static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001737smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001738{
Willy Tarreau37406352012-04-23 16:16:37 +02001739 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001740 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001741 smp->data.u.sint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001742 return 1;
1743}
1744
Willy Tarreau34db1082012-04-19 17:16:54 +02001745/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001746 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001747 * undefined behaviour.
1748 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001749static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001750smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001751{
Willy Tarreau37406352012-04-23 16:16:37 +02001752 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001753 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001754 smp->data.u.sint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001755 return 1;
1756}
1757
Willy Tarreaua5e37562011-12-16 17:06:15 +01001758/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001759 * by the number of running servers and rounded up. If there is no running
1760 * server, we return twice the total, just as if we had half a running server.
1761 * This is more or less correct anyway, since we expect the last server to come
1762 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001763 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001764 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001765 */
1766static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001767smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001768{
1769 int nbsrv;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001770 struct proxy *px;
Willy Tarreaua36af912009-10-10 12:02:45 +02001771
Willy Tarreau37406352012-04-23 16:16:37 +02001772 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001773 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001774 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001775
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01001776 nbsrv = be_usable_srv(px);
Willy Tarreaua36af912009-10-10 12:02:45 +02001777
1778 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001779 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001780 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001781 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001782
1783 return 1;
1784}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001785
Willy Tarreau34db1082012-04-19 17:16:54 +02001786/* set temp integer to the number of concurrent connections on the server in the backend.
1787 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1788 * undefined behaviour.
1789 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001790static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001791smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001792{
Willy Tarreauf853c462012-04-23 18:53:56 +02001793 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001794 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001795 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001796 return 1;
1797}
1798
Tait Clarridge7896d522012-12-05 21:39:31 -05001799/* set temp integer to the number of enabled servers on the proxy.
1800 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1801 * undefined behaviour.
1802 */
1803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001804smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05001805{
1806 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001807 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001808 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05001809 return 1;
1810}
1811
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001812static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
1813{
1814
1815 struct proxy *px;
1816
1817 if (!smp_make_safe(smp))
1818 return 0;
1819
1820 px = proxy_find_by_name(smp->data.u.str.str, PR_CAP_BE, 0);
1821 if (!px)
1822 return 0;
1823
1824 smp->data.type = SMP_T_SINT;
1825 smp->data.u.sint = be_usable_srv(px);
1826
1827 return 1;
1828}
1829
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001830
1831/* Note: must not be declared <const> as its list will be overwritten.
1832 * Please take care of keeping this list alphabetically sorted.
1833 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001834static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001835 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1836 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1837 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
Marcin Deranekd2471c22016-12-12 14:08:05 +01001838 { "be_name", smp_fetch_be_name, 0, NULL, SMP_T_STR, SMP_USE_BKEND, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001839 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1840 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1841 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1842 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1843 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1844 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001845 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001846 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001847 { /* END */ },
1848}};
1849
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001850/* Note: must not be declared <const> as its list will be overwritten */
1851static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1852 { "nbsrv", sample_conv_nbsrv, 0, NULL, SMP_T_STR, SMP_T_SINT },
1853 { /* END */ },
1854}};
1855
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001856
Willy Tarreau61612d42012-04-19 18:42:05 +02001857/* Note: must not be declared <const> as its list will be overwritten.
1858 * Please take care of keeping this list alphabetically sorted.
1859 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001860static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001861 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001862}};
1863
1864
1865__attribute__((constructor))
1866static void __backend_init(void)
1867{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001868 sample_register_fetches(&smp_kws);
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001869 sample_register_convs(&sample_conv_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001870 acl_register_keywords(&acl_kws);
1871}
1872
Willy Tarreaubaaee002006-06-26 02:48:02 +02001873/*
1874 * Local variables:
1875 * c-indent-level: 8
1876 * c-basic-offset: 8
1877 * End:
1878 */