blob: d17635ff5ff30157596e9d8693a9df9b3b01dc79 [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.
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 * This functions is designed to be called before server's weight and state
101 * commit so it uses 'next' weight and states values.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200103void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104{
105 struct server *srv;
106
Willy Tarreau20697042007-11-15 23:26:18 +0100107 px->srv_act = px->srv_bck = 0;
108 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100109 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 for (srv = px->srv; srv != NULL; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200111 if (!srv_willbe_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100112 continue;
113
Willy Tarreauc93cd162014-05-13 15:54:22 +0200114 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100115 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100116 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100117 px->lbprm.fbck = srv;
118 px->srv_bck++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400119 srv->cumulative_weight = px->lbprm.tot_wbck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200120 px->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100121 } else {
122 px->srv_act++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400123 srv->cumulative_weight = px->lbprm.tot_wact;
Emeric Brun52a91d32017-08-31 14:41:55 +0200124 px->lbprm.tot_wact += srv->next_eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 }
126 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100127}
Willy Tarreau20697042007-11-15 23:26:18 +0100128
Willy Tarreaub625a082007-11-26 01:15:43 +0100129/* This function simply updates the backend's tot_weight and tot_used values
130 * after servers weights have been updated. It is designed to be used after
131 * recount_servers() or equivalent.
132 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200133void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100134{
Willy Tarreau20697042007-11-15 23:26:18 +0100135 if (px->srv_act) {
136 px->lbprm.tot_weight = px->lbprm.tot_wact;
137 px->lbprm.tot_used = px->srv_act;
138 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100139 else if (px->lbprm.fbck) {
140 /* use only the first backup server */
Emeric Brun52a91d32017-08-31 14:41:55 +0200141 px->lbprm.tot_weight = px->lbprm.fbck->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100142 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100143 }
144 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100145 px->lbprm.tot_weight = px->lbprm.tot_wbck;
146 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100147 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100148}
149
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200150/*
151 * This function tries to find a running server for the proxy <px> following
152 * the source hash method. Depending on the number of active/backup servers,
153 * it will either look for active servers, or for backup servers.
154 * If any server is found, it will be returned. If no valid server is found,
155 * NULL is returned.
156 */
Christopher Fauletf0614e82017-06-09 14:20:29 +0200157static struct server *get_server_sh(struct proxy *px, const char *addr, int len)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200158{
159 unsigned int h, l;
160
161 if (px->lbprm.tot_weight == 0)
162 return NULL;
163
164 l = h = 0;
165
166 /* note: we won't hash if there's only one server left */
167 if (px->lbprm.tot_used == 1)
168 goto hash_done;
169
170 while ((l + sizeof (int)) <= len) {
171 h ^= ntohl(*(unsigned int *)(&addr[l]));
172 l += sizeof (int);
173 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500174 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100175 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200176 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200177 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
178 return chash_get_server_hash(px, h);
179 else
180 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200181}
182
183/*
184 * This function tries to find a running server for the proxy <px> following
185 * the URI hash method. In order to optimize cache hits, the hash computation
186 * ends at the question mark. Depending on the number of active/backup servers,
187 * it will either look for active servers, or for backup servers.
188 * If any server is found, it will be returned. If no valid server is found,
189 * NULL is returned.
190 *
191 * This code was contributed by Guillaume Dallaire, who also selected this hash
192 * algorithm out of a tens because it gave him the best results.
193 *
194 */
Christopher Fauletf0614e82017-06-09 14:20:29 +0200195static struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200196{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700197 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200198 int c;
199 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400200 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200201
202 if (px->lbprm.tot_weight == 0)
203 return NULL;
204
205 /* note: we won't hash if there's only one server left */
206 if (px->lbprm.tot_used == 1)
207 goto hash_done;
208
209 if (px->uri_len_limit)
210 uri_len = MIN(uri_len, px->uri_len_limit);
211
Bhaskar98634f02013-10-29 23:30:51 -0400212 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200213 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200214 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200215 if (c == '/') {
216 slashes++;
217 if (slashes == px->uri_dirs_depth1) /* depth+1 */
218 break;
219 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100220 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200221 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200222 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200223 }
Bhaskar98634f02013-10-29 23:30:51 -0400224
225 hash = gen_hash(px, start, (end - start));
226
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500227 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100228 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200229 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200230 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
231 return chash_get_server_hash(px, hash);
232 else
233 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200234}
235
Willy Tarreau01732802007-11-01 22:48:15 +0100236/*
237 * This function tries to find a running server for the proxy <px> following
238 * the URL parameter hash method. It looks for a specific parameter in the
239 * URL and hashes it to compute the server ID. This is useful to optimize
240 * performance by avoiding bounces between servers in contexts where sessions
241 * are shared but cookies are not usable. If the parameter is not found, NULL
242 * is returned. If any server is found, it will be returned. If no valid server
243 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100244 */
Christopher Faulet8fe48912017-06-09 14:23:09 +0200245static struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
Willy Tarreau01732802007-11-01 22:48:15 +0100246{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700247 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400248 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200249 const char *p;
250 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100251 int plen;
252
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200253 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100254 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100255 return NULL;
256
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200257 if ((p = memchr(uri, '?', uri_len)) == NULL)
258 return NULL;
259
Willy Tarreau01732802007-11-01 22:48:15 +0100260 p++;
261
262 uri_len -= (p - uri);
263 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100265
266 while (uri_len > plen) {
267 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268 if (params[plen] == '=') {
269 if (memcmp(params, px->url_param_name, plen) == 0) {
270 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100271 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200272 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100273 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200274 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400275 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200276 uri_len -= plen + 1;
277
Bhaskar98634f02013-10-29 23:30:51 -0400278 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100279 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400280 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100281 }
Bhaskar98634f02013-10-29 23:30:51 -0400282 hash = gen_hash(px, start, (end - start));
283
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500284 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100285 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400286
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200287 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
288 return chash_get_server_hash(px, hash);
289 else
290 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100291 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 }
293 /* skip to next parameter */
294 p = memchr(params, '&', uri_len);
295 if (!p)
296 return NULL;
297 p++;
298 uri_len -= (p - params);
299 params = p;
300 }
301 return NULL;
302}
303
304/*
305 * this does the same as the previous server_ph, but check the body contents
306 */
Christopher Faulet8fe48912017-06-09 14:23:09 +0200307static struct server *get_server_ph_post(struct stream *s)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700309 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200310 struct http_txn *txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100311 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 struct http_msg *msg = &txn->req;
313 struct proxy *px = s->be;
314 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200315 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200316 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100317 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400318 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319
Willy Tarreau157dd632009-12-06 19:18:09 +0100320 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200321 return NULL;
322
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200323 if (len > req->buf->data + req->buf->size - p)
324 len = req->buf->data + req->buf->size - p;
325
Willy Tarreau157dd632009-12-06 19:18:09 +0100326 if (px->lbprm.tot_weight == 0)
327 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328
329 while (len > plen) {
330 /* Look for the parameter name followed by an equal symbol */
331 if (params[plen] == '=') {
332 if (memcmp(params, px->url_param_name, plen) == 0) {
333 /* OK, we have the parameter here at <params>, and
334 * the value after the equal sign, at <p>
335 * skip the equal symbol
336 */
337 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400338 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200339 len -= plen + 1;
340
Bhaskar98634f02013-10-29 23:30:51 -0400341 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200342 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100343 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400344 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100345 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200346 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
347 break;
348 return NULL; /* oh, no; this is not uri-encoded.
349 * This body does not contain parameters.
350 */
351 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200352 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400353 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200354 /* should we break if vlen exceeds limit? */
355 }
Bhaskar98634f02013-10-29 23:30:51 -0400356 hash = gen_hash(px, start, (end - start));
357
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500358 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100359 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400360
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200361 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
362 return chash_get_server_hash(px, hash);
363 else
364 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200365 }
366 }
Willy Tarreau01732802007-11-01 22:48:15 +0100367 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100369 if (!p)
370 return NULL;
371 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200372 len -= (p - params);
373 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100374 }
375 return NULL;
376}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200378
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379/*
Benoitaffb4812009-03-25 13:02:10 +0100380 * This function tries to find a running server for the proxy <px> following
381 * the Header parameter hash method. It looks for a specific parameter in the
382 * URL and hashes it to compute the server ID. This is useful to optimize
383 * performance by avoiding bounces between servers in contexts where sessions
384 * are shared but cookies are not usable. If the parameter is not found, NULL
385 * is returned. If any server is found, it will be returned. If no valid server
386 * is found, NULL is returned.
387 */
Christopher Faulet8fe48912017-06-09 14:23:09 +0200388static struct server *get_server_hh(struct stream *s)
Benoitaffb4812009-03-25 13:02:10 +0100389{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700390 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200391 struct http_txn *txn = s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100392 struct proxy *px = s->be;
393 unsigned int plen = px->hh_len;
394 unsigned long len;
395 struct hdr_ctx ctx;
396 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400397 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100398
399 /* tot_weight appears to mean srv_count */
400 if (px->lbprm.tot_weight == 0)
401 return NULL;
402
Benoitaffb4812009-03-25 13:02:10 +0100403 ctx.idx = 0;
404
405 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100406 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 +0100407
408 /* if the header is not found or empty, let's fallback to round robin */
409 if (!ctx.idx || !ctx.vlen)
410 return NULL;
411
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200412 /* note: we won't hash if there's only one server left */
413 if (px->lbprm.tot_used == 1)
414 goto hash_done;
415
Benoitaffb4812009-03-25 13:02:10 +0100416 /* Found a the hh_name in the headers.
417 * we will compute the hash based on this value ctx.val.
418 */
419 len = ctx.vlen;
420 p = (char *)ctx.line + ctx.val;
421 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400422 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100423 } else {
424 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100425 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100426 /* special computation, use only main domain name, not tld/host
427 * going back from the end of string, start hashing at first
428 * dot stop at next.
429 * This is designed to work with the 'Host' header, and requires
430 * a special option to activate this.
431 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100432 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100433 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100434 if (dohash) {
435 /* Rewind the pointer until the previous char
436 * is a dot, this will allow to set the start
437 * position of the domain. */
438 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100439 break;
Benoitaffb4812009-03-25 13:02:10 +0100440 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100441 else if (*p == '.') {
442 /* The pointer is rewinded to the dot before the
443 * tld, we memorize the end of the domain and
444 * can enter the domain processing. */
445 end = p;
446 dohash = 1;
447 }
Benoitaffb4812009-03-25 13:02:10 +0100448 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100449 len--;
Benoitaffb4812009-03-25 13:02:10 +0100450 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100451 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400452 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100453 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500454 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100455 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200456 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200457 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
458 return chash_get_server_hash(px, hash);
459 else
460 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100461}
462
Willy Tarreau34db1082012-04-19 17:16:54 +0200463/* RDP Cookie HASH. */
Christopher Faulet8fe48912017-06-09 14:23:09 +0200464static struct server *get_server_rch(struct stream *s)
Emeric Brun736aa232009-06-30 17:56:00 +0200465{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700466 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200467 struct proxy *px = s->be;
468 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200469 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200470 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200471 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200472
473 /* tot_weight appears to mean srv_count */
474 if (px->lbprm.tot_weight == 0)
475 return NULL;
476
Willy Tarreau37406352012-04-23 16:16:37 +0200477 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200478
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100479 b_rew(s->req.buf, rewind = s->req.buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200480
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200481 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200482 len = smp.data.u.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 b_adv(s->req.buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200485
Willy Tarreau37406352012-04-23 16:16:37 +0200486 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200487 return NULL;
488
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200489 /* note: we won't hash if there's only one server left */
490 if (px->lbprm.tot_used == 1)
491 goto hash_done;
492
Emeric Brun736aa232009-06-30 17:56:00 +0200493 /* Found a the hh_name in the headers.
494 * we will compute the hash based on this value ctx.val.
495 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200496 hash = gen_hash(px, smp.data.u.str.str, len);
Bhaskar98634f02013-10-29 23:30:51 -0400497
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500498 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100499 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200500 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200501 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
502 return chash_get_server_hash(px, hash);
503 else
504 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200505}
Benoitaffb4812009-03-25 13:02:10 +0100506
507/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200508 * This function applies the load-balancing algorithm to the stream, as
509 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200510 * 'assigned'.
511 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200512 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100514 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 * The function tries to keep the original connection slot if it reconnects to
516 * the same server, otherwise it releases it and tries to offer it.
517 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200518 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519 *
520 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100521 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200522 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
523 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 * SRV_STATUS_INTERNAL for other unrecoverable errors.
525 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200526 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 * it does not need to be called anymore. This means that target_srv(&s->target)
528 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529 *
530 */
531
Willy Tarreau87b09662015-04-03 00:22:06 +0200532int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200534 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100536 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100538
Simon Horman8effd3d2011-08-13 08:03:52 +0900539 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540
Willy Tarreau7c669d72008-06-20 15:04:11 +0200541 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200542 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200543 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100544
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100545 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200546 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547
Willy Tarreau7c669d72008-06-20 15:04:11 +0200548 /* We have to release any connection slot before applying any LB algo,
549 * otherwise we may erroneously end up with no available slot.
550 */
551 if (conn_slot)
552 sess_change_server(s, NULL);
553
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100554 /* We will now try to find the good server and store it into <objt_server(s->target)>.
555 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200556 * as well as if no server is available (check error code).
557 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100558
Willy Tarreau827aee92011-03-10 16:55:02 +0100559 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100560 s->target = NULL;
Willy Tarreau350f4872014-11-28 14:42:25 +0100561 conn = objt_conn(s->si[1].end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100562
Willy Tarreau068621e2013-12-23 15:11:25 +0100563 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100564 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100565 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreaueee5b512015-04-03 23:46:31 +0200566 ((s->txn && s->txn->flags & TX_PREFER_LAST) ||
Willy Tarreauc35362a2014-04-25 13:58:37 +0200567 ((s->be->options & PR_O_PREF_LAST) &&
Olivier Doucet1ca1b6f2017-01-02 11:48:57 +0100568 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200569 (!s->be->max_ka_queue ||
570 server_has_room(__objt_server(conn->target)) ||
571 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200572 srv_currently_usable(__objt_server(conn->target))) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200573 /* This stream was relying on a server in a previous request
Olivier Doucet1ca1b6f2017-01-02 11:48:57 +0100574 * and the proxy has "option prefer-last-server" set
575 * and balance algorithm dont tell us to do otherwise, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100576 * let's try to reuse the same server.
577 */
578 srv = __objt_server(conn->target);
579 s->target = &srv->obj_type;
580 }
581 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200582 /* we must check if we have at least one server available */
583 if (!s->be->lbprm.tot_weight) {
584 err = SRV_STATUS_NOSRV;
585 goto out;
586 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 /* First check whether we need to fetch some data or simply call
589 * the LB lookup function. Only the hashing functions will need
590 * some input data in fact, and will support multiple algorithms.
591 */
592 switch (s->be->lbprm.algo & BE_LB_LKUP) {
593 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200595 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596
Willy Tarreauf09c6602012-02-13 17:12:08 +0100597 case BE_LB_LKUP_FSTREE:
598 srv = fas_get_next_server(s->be, prev_srv);
599 break;
600
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100602 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200603 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200606 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200607 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200610 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200612 break;
613 }
614 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200615 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200616 err = SRV_STATUS_INTERNAL;
617 goto out;
618 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
620 switch (s->be->lbprm.algo & BE_LB_PARM) {
621 case BE_LB_HASH_SRC:
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200622 conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200623 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200624 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200625 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200626 4);
627 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200628 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200629 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200630 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200631 16);
632 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200633 else {
634 /* unknown IP family */
635 err = SRV_STATUS_INTERNAL;
636 goto out;
637 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200638 break;
639
640 case BE_LB_HASH_URI:
641 /* URI hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200642 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100643 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 srv = get_server_uh(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200645 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
646 s->txn->req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200648
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200649 case BE_LB_HASH_PRM:
650 /* URL Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200651 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100652 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100653
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 srv = get_server_ph(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200655 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
656 s->txn->req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100657
Willy Tarreaueee5b512015-04-03 23:46:31 +0200658 if (!srv && s->txn->meth == HTTP_METH_POST)
Willy Tarreau827aee92011-03-10 16:55:02 +0100659 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200660 break;
Benoitaffb4812009-03-25 13:02:10 +0100661
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200662 case BE_LB_HASH_HDR:
663 /* Header Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200664 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100665 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100666 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667 break;
668
669 case BE_LB_HASH_RDP:
670 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100671 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200672 break;
673
674 default:
675 /* unknown balancing algorithm */
676 err = SRV_STATUS_INTERNAL;
677 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100678 }
Emeric Brun736aa232009-06-30 17:56:00 +0200679
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200680 /* If the hashing parameter was not found, let's fall
681 * back to round robin on the map.
682 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100683 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200684 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100685 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200686 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100687 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200688 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200689
690 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200691 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200692
Willy Tarreau7c669d72008-06-20 15:04:11 +0200693 default:
694 /* unknown balancing algorithm */
695 err = SRV_STATUS_INTERNAL;
696 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200698
Willy Tarreau827aee92011-03-10 16:55:02 +0100699 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200700 err = SRV_STATUS_FULL;
701 goto out;
702 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100703 else if (srv != prev_srv) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200704 HA_ATOMIC_ADD(&s->be->be_counters.cum_lbconn, 1);
Christopher Faulet29f77e82017-06-08 14:04:45 +0200705 HA_ATOMIC_ADD(&srv->counters.cum_lbconn, 1);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100706 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100707 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200708 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200709 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100710 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100712 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau350f4872014-11-28 14:42:25 +0100713 (conn = objt_conn(s->si[1].end)) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200714 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100715 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100716 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100717 }
718 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200719 err = SRV_STATUS_NOSRV;
720 goto out;
721 }
722
Willy Tarreaue7dff022015-04-03 01:14:29 +0200723 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200724 err = SRV_STATUS_OK;
725 out:
726
727 /* Either we take back our connection slot, or we offer it to someone
728 * else if we don't need it anymore.
729 */
730 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100731 if (conn_slot == srv) {
732 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200733 } else {
734 if (may_dequeue_tasks(conn_slot, s->be))
735 process_srv_queue(conn_slot);
736 }
737 }
738
739 out_err:
740 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741}
742
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743/*
Willy Tarreaue7dff022015-04-03 01:14:29 +0200744 * This function assigns a server address to a stream, and sets SF_ADDR_SET.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 * The address is taken from the currently assigned server, or from the
746 * dispatch or transparent address.
747 *
748 * It may return :
749 * SRV_STATUS_OK if everything is OK.
750 * SRV_STATUS_INTERNAL for other unrecoverable errors.
751 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200752 * Upon successful return, the stream flag SF_ADDR_SET is set. This flag is
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 * not cleared, so it's to the caller to clear it if required.
754 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200755 * The caller is responsible for having already assigned a connection
756 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200757 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200759int assign_server_address(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200761 struct connection *cli_conn = objt_conn(strm_orig(s));
Willy Tarreau350f4872014-11-28 14:42:25 +0100762 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200763
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764#ifdef DEBUG_FULL
765 fprintf(stderr,"assign_server_address : s=%p\n",s);
766#endif
767
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200769 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200770 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 return SRV_STATUS_INTERNAL;
772
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200773 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreau04276f32017-01-06 17:41:29 +0100774 set_host_port(&srv_conn->addr.to, objt_server(s->target)->svc_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200776 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200777 /* if the server has no address, we use the same address
778 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200779 * locally on multiple addresses at once. Nothing is done
780 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200781 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200782 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200783
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200784 if (cli_conn->addr.to.ss_family == AF_INET) {
785 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
786 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
787 ((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 +0200788 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200789 }
790
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 /* if this server remaps proxied ports, we'll use
792 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200793 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100794 int base_port;
795
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200796 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100797
798 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200799 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100800
801 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200802 base_port += get_host_port(&srv_conn->addr.to);
803 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804 }
805 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200806 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200808 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200810 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200812 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200814 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
815 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100817 else if (s->be->options & PR_O_HTTP_PROXY) {
818 /* If HTTP PROXY option is set, then server is already assigned
819 * during incoming client request parsing. */
820 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100821 else {
822 /* no server and no LB algorithm ! */
823 return SRV_STATUS_INTERNAL;
824 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100826 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100827 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100828
Willy Tarreaue7dff022015-04-03 01:14:29 +0200829 s->flags |= SF_ADDR_SET;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 return SRV_STATUS_OK;
831}
832
Willy Tarreau87b09662015-04-03 00:22:06 +0200833/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200835 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837 * be called before any connection and after any retry or redispatch occurs.
838 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200839 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 *
841 * Returns :
842 *
843 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100844 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 * SRV_STATUS_QUEUED if the connection has been queued.
846 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 * SRV_STATUS_INTERNAL for other unrecoverable errors.
850 *
851 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200852int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853{
854 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100855 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 int err;
857
858 if (s->pend_pos)
859 return SRV_STATUS_INTERNAL;
860
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200862 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100863 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100864
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100866 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200867 /* This stream was previously assigned to a server. We have to
868 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869 * - if the server changed :
870 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200871 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200872 * - increment srv->redispatches and be->redispatches
873 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100874 */
875
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100876 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200877 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
878 s->txn->flags &= ~TX_CK_MASK;
879 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200881 s->flags |= SF_REDISP;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200882 HA_ATOMIC_ADD(&prev_srv->counters.redispatches, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200883 HA_ATOMIC_ADD(&s->be->be_counters.redispatches, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200884 } else {
Christopher Faulet29f77e82017-06-08 14:04:45 +0200885 HA_ATOMIC_ADD(&prev_srv->counters.retries, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200886 HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100887 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100888 }
889 }
890
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891 switch (err) {
892 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200893 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100894 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200896 return SRV_STATUS_OK; /* dispatch or proxy mode */
897
898 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100899 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200900 return SRV_STATUS_OK;
901
Willy Tarreau87b09662015-04-03 00:22:06 +0200902 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200903 * connection slot yet. Either it is a redispatch, or it was
904 * assigned from persistence information (direct mode).
905 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200906 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200907 /* server scheduled for redirection, and already assigned. We
908 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100909 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100910 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100911 return SRV_STATUS_OK;
912 }
913
Willy Tarreau87b09662015-04-03 00:22:06 +0200914 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200915 * We know we have to queue it into the server's queue, so if a maxqueue
916 * is set on the server, we must also check that the server's queue is
917 * not full, in which case we have to return FULL.
918 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100919 if (srv->maxconn &&
920 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200921
Willy Tarreau827aee92011-03-10 16:55:02 +0100922 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200923 return SRV_STATUS_FULL;
924
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 p = pendconn_add(s);
926 if (p)
927 return SRV_STATUS_QUEUED;
928 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200929 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200931
932 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100933 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 return SRV_STATUS_OK;
935
936 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +0200937 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 p = pendconn_add(s);
939 if (p)
940 return SRV_STATUS_QUEUED;
941 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200942 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943
944 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945 return err;
946
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947 case SRV_STATUS_INTERNAL:
948 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200949
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 default:
951 return SRV_STATUS_INTERNAL;
952 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100953}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954
Willy Tarreaub1d67742010-03-29 19:36:59 +0200955/* If an explicit source binding is specified on the server and/or backend, and
956 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau87b09662015-04-03 00:22:06 +0200957 * assigned to the stream's pending connection. This function assumes that an
Willy Tarreau350f4872014-11-28 14:42:25 +0100958 * outgoing connection has already been assigned to s->si[1].end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200959 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200960static void assign_tproxy_address(struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +0200961{
Willy Tarreau29fbe512015-08-20 19:35:14 +0200962#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100963 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100964 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200965 struct connection *cli_conn;
Willy Tarreau350f4872014-11-28 14:42:25 +0100966 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100967
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100968 if (srv && srv->conn_src.opts & CO_SRC_BIND)
969 src = &srv->conn_src;
970 else if (s->be->conn_src.opts & CO_SRC_BIND)
971 src = &s->be->conn_src;
972 else
973 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100974
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100975 switch (src->opts & CO_SRC_TPROXY_MASK) {
976 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200977 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100978 break;
979 case CO_SRC_TPROXY_CLI:
980 case CO_SRC_TPROXY_CIP:
981 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200982 cli_conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200983 if (cli_conn)
984 srv_conn->addr.from = cli_conn->addr.from;
985 else
986 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100987 break;
988 case CO_SRC_TPROXY_DYN:
Willy Tarreaueee5b512015-04-03 23:46:31 +0200989 if (src->bind_hdr_occ && s->txn) {
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100990 char *vptr;
991 int vlen;
992 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100993
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100994 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200995 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
996 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
997 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100998
Willy Tarreaueee5b512015-04-03 23:46:31 +0200999 b_rew(s->req.buf, rewind = http_hdr_rewind(&s->txn->req));
1000 if (http_get_hdr(&s->txn->req, src->bind_hdr_name, src->bind_hdr_len,
1001 &s->txn->hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001002 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001003 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +02001004 }
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001005 b_adv(s->req.buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001006 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001007 break;
1008 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001009 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001010 }
1011#endif
1012}
1013
1014
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001016 * This function initiates a connection to the server assigned to this stream
Willy Tarreau350f4872014-11-28 14:42:25 +01001017 * (s->target, s->si[1].addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001018 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001020 * - SF_ERR_NONE if everything's OK
1021 * - SF_ERR_SRVTO if there are no more servers
1022 * - SF_ERR_SRVCL if the connection was refused by the server
1023 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1024 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1025 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001026 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001027 * The server-facing stream interface is expected to hold a pre-allocated connection
Willy Tarreau350f4872014-11-28 14:42:25 +01001028 * in s->si[1].conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001029 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001030int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001031{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001032 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001033 struct connection *srv_conn;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001034 struct connection *old_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001035 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001036 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001037 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038
Willy Tarreau7b004922015-08-04 19:34:21 +02001039 srv = objt_server(s->target);
Willy Tarreau350f4872014-11-28 14:42:25 +01001040 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau34601a82013-12-15 16:33:46 +01001041 if (srv_conn)
1042 reuse = s->target == srv_conn->target;
1043
Willy Tarreau8dff9982015-08-04 20:45:52 +02001044 if (srv && !reuse) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001045 old_conn = srv_conn;
1046 if (old_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001047 srv_conn = NULL;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001048 old_conn->owner = NULL;
1049 si_detach_endpoint(&s->si[1]);
1050 /* note: if the connection was in a server's idle
1051 * queue, it doesn't get dequeued.
1052 */
Willy Tarreau8dff9982015-08-04 20:45:52 +02001053 }
1054
Willy Tarreau449d74a2015-08-05 17:16:33 +02001055 /* Below we pick connections from the safe or idle lists based
1056 * on the strategy, the fact that this is a first or second
1057 * (retryable) request, with the indicated priority (1 or 2) :
1058 *
1059 * SAFE AGGR ALWS
1060 *
1061 * +-----+-----+ +-----+-----+ +-----+-----+
1062 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1063 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1064 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1065 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1066 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1067 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1068 */
Christopher Faulet40a007c2017-07-03 15:41:01 +02001069 if (srv->idle_conns && !LIST_ISEMPTY(&srv->idle_conns[tid]) &&
Willy Tarreau449d74a2015-08-05 17:16:33 +02001070 ((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR &&
1071 s->txn && (s->txn->flags & TX_NOT_FIRST))) {
Christopher Faulet40a007c2017-07-03 15:41:01 +02001072 srv_conn = LIST_ELEM(srv->idle_conns[tid].n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001073 }
Christopher Faulet40a007c2017-07-03 15:41:01 +02001074 else if (srv->safe_conns && !LIST_ISEMPTY(&srv->safe_conns[tid]) &&
Willy Tarreau449d74a2015-08-05 17:16:33 +02001075 ((s->txn && (s->txn->flags & TX_NOT_FIRST)) ||
1076 (s->be->options & PR_O_REUSE_MASK) >= PR_O_REUSE_AGGR)) {
Christopher Faulet40a007c2017-07-03 15:41:01 +02001077 srv_conn = LIST_ELEM(srv->safe_conns[tid].n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001078 }
Christopher Faulet40a007c2017-07-03 15:41:01 +02001079 else if (srv->idle_conns && !LIST_ISEMPTY(&srv->idle_conns[tid]) &&
Willy Tarreau449d74a2015-08-05 17:16:33 +02001080 (s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) {
Christopher Faulet40a007c2017-07-03 15:41:01 +02001081 srv_conn = LIST_ELEM(srv->idle_conns[tid].n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001082 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001083
Willy Tarreau449d74a2015-08-05 17:16:33 +02001084 /* If we've picked a connection from the pool, we now have to
1085 * detach it. We may have to get rid of the previous idle
1086 * connection we had, so for this we try to swap it with the
1087 * other owner's. That way it may remain alive for others to
1088 * pick.
1089 */
1090 if (srv_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001091 LIST_DEL(&srv_conn->list);
1092 LIST_INIT(&srv_conn->list);
1093
Willy Tarreauefb90f92015-08-06 11:37:10 +02001094 if (srv_conn->owner) {
1095 si_detach_endpoint(srv_conn->owner);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001096 if (old_conn && !(old_conn->flags & CO_FL_PRIVATE)) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001097 si_attach_conn(srv_conn->owner, old_conn);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001098 si_idle_conn(srv_conn->owner, NULL);
1099 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001100 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001101 si_attach_conn(&s->si[1], srv_conn);
1102 reuse = 1;
1103 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001104
Willy Tarreau449d74a2015-08-05 17:16:33 +02001105 /* we may have to release our connection if we couldn't swap it */
Willy Tarreauefb90f92015-08-06 11:37:10 +02001106 if (old_conn && !old_conn->owner) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001107 LIST_DEL(&old_conn->list);
Willy Tarreauf098fd02017-10-22 09:35:01 +02001108 conn_full_close(old_conn);
Willy Tarreauefb90f92015-08-06 11:37:10 +02001109 conn_free(old_conn);
1110 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001111 }
1112
Willy Tarreau34601a82013-12-15 16:33:46 +01001113 if (reuse) {
1114 /* Disable connection reuse if a dynamic source is used.
1115 * As long as we don't share connections between servers,
1116 * we don't need to disable connection reuse on no-idempotent
1117 * requests nor when PROXY protocol is used.
1118 */
Willy Tarreau34601a82013-12-15 16:33:46 +01001119 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1120 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1121 reuse = 0;
1122 }
1123 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1124 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1125 reuse = 0;
1126 }
1127 }
1128
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001129 if (!reuse)
Willy Tarreau973a5422015-08-05 21:47:23 +02001130 srv_conn = si_alloc_conn(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02001131 else {
1132 /* reusing our connection, take it out of the idle list */
1133 LIST_DEL(&srv_conn->list);
1134 LIST_INIT(&srv_conn->list);
1135 }
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001136
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001137 if (!srv_conn)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001138 return SF_ERR_RESOURCE;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001139
Willy Tarreaue7dff022015-04-03 01:14:29 +02001140 if (!(s->flags & SF_ADDR_SET)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 err = assign_server_address(s);
1142 if (err != SRV_STATUS_OK)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001143 return SF_ERR_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 }
1145
Willy Tarreauff605db2013-12-20 11:09:51 +01001146 if (!conn_xprt_ready(srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001147 /* the target was only on the stream, assign it to the SI now */
Willy Tarreauff605db2013-12-20 11:09:51 +01001148 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001149
Willy Tarreauff605db2013-12-20 11:09:51 +01001150 /* set the correct protocol on the output stream interface */
Willy Tarreau7b004922015-08-04 19:34:21 +02001151 if (srv) {
1152 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), srv->xprt);
Willy Tarreauff605db2013-12-20 11:09:51 +01001153 }
1154 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1155 /* proxies exclusively run on raw_sock right now */
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001156 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), xprt_get(XPRT_RAW));
Willy Tarreau350f4872014-11-28 14:42:25 +01001157 if (!objt_conn(s->si[1].end) || !objt_conn(s->si[1].end)->ctrl)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 return SF_ERR_INTERNAL;
Willy Tarreauff605db2013-12-20 11:09:51 +01001159 }
1160 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 return SF_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001162
Willy Tarreauff605db2013-12-20 11:09:51 +01001163 /* process the case where the server requires the PROXY protocol to be sent */
1164 srv_conn->send_proxy_ofs = 0;
Willy Tarreau7b004922015-08-04 19:34:21 +02001165 if (srv && srv->pp_opts) {
Willy Tarreau387ebf82015-08-04 19:24:13 +02001166 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauff605db2013-12-20 11:09:51 +01001167 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001168 cli_conn = objt_conn(strm_orig(s));
Willy Tarreauff605db2013-12-20 11:09:51 +01001169 if (cli_conn)
1170 conn_get_to_addr(cli_conn);
1171 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001172
Willy Tarreau350f4872014-11-28 14:42:25 +01001173 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001174
Willy Tarreauff605db2013-12-20 11:09:51 +01001175 assign_tproxy_address(s);
1176 }
1177 else {
1178 /* the connection is being reused, just re-attach it */
Willy Tarreau350f4872014-11-28 14:42:25 +01001179 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 s->flags |= SF_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001181 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001182
William Lallemandb7ff6a32012-03-02 14:35:21 +01001183 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001184 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Willy Tarreau350f4872014-11-28 14:42:25 +01001185 s->si[1].flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001186
Willy Tarreau14f8e862012-08-30 22:23:13 +02001187 /* disable lingering */
1188 if (s->be->options & PR_O_TCP_NOLING)
Willy Tarreau350f4872014-11-28 14:42:25 +01001189 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001190
Willy Tarreau350f4872014-11-28 14:42:25 +01001191 err = si_connect(&s->si[1]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 if (err != SF_ERR_NONE)
Willy Tarreau9650f372009-08-16 14:02:45 +02001194 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001195
Willy Tarreau14f8e862012-08-30 22:23:13 +02001196 /* set connect timeout */
Willy Tarreau350f4872014-11-28 14:42:25 +01001197 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001198
Willy Tarreau827aee92011-03-10 16:55:02 +01001199 if (srv) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02001200 int count;
1201
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 s->flags |= SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02001203 count = HA_ATOMIC_ADD(&srv->cur_sess, 1);
1204 HA_ATOMIC_UPDATE_MAX(&srv->counters.cur_sess_max, count);
Willy Tarreau51406232008-03-10 22:04:20 +01001205 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001206 s->be->lbprm.server_take_conn(srv);
Willy Tarreau732eac42015-07-09 11:40:25 +02001207
1208#ifdef USE_OPENSSL
1209 if (srv->ssl_ctx.sni) {
1210 struct sample *smp;
1211 int rewind;
1212
1213 /* Tricky case : we have already scheduled the pending
1214 * HTTP request or TCP data for leaving. So in HTTP we
1215 * rewind exactly the headers, otherwise we rewind the
1216 * output data.
1217 */
1218 rewind = s->txn ? http_hdr_rewind(&s->txn->req) : s->req.buf->o;
1219 b_rew(s->req.buf, rewind);
1220
1221 smp = sample_fetch_as_type(s->be, s->sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, srv->ssl_ctx.sni, SMP_T_STR);
1222
1223 /* restore the pointers */
1224 b_adv(s->req.buf, rewind);
1225
Willy Tarreau2e0565c2016-08-09 11:55:21 +02001226 if (smp_make_safe(smp)) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001227 ssl_sock_set_servername(srv_conn, smp->data.u.str.str);
Willy Tarreau387ebf82015-08-04 19:24:13 +02001228 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau732eac42015-07-09 11:40:25 +02001229 }
1230 }
1231#endif /* USE_OPENSSL */
1232
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 }
1234
Willy Tarreaue7dff022015-04-03 01:14:29 +02001235 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001236}
1237
1238
Willy Tarreaubaaee002006-06-26 02:48:02 +02001239/* This function performs the "redispatch" part of a connection attempt. It
1240 * will assign a server if required, queue the connection if required, and
1241 * handle errors that might arise at this level. It can change the server
1242 * state. It will return 1 if it encounters an error, switches the server
1243 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1244 * that the connection is ready to use.
1245 */
1246
Willy Tarreau87b09662015-04-03 00:22:06 +02001247int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248{
Willy Tarreau827aee92011-03-10 16:55:02 +01001249 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001250 int conn_err;
1251
1252 /* We know that we don't have any connection pending, so we will
1253 * try to get a new one, and wait in this state if it's queued
1254 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001255 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001256 conn_err = assign_server_and_queue(s);
1257 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001258
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259 switch (conn_err) {
1260 case SRV_STATUS_OK:
1261 break;
1262
Willy Tarreau7c669d72008-06-20 15:04:11 +02001263 case SRV_STATUS_FULL:
1264 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1265 * and we can redispatch to another server, or it is not and we return
1266 * 503. This only makes sense in DIRECT mode however, because normal LB
1267 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001268 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001269 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001270 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001271 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001272 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001273 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001274 goto redispatch;
1275 }
1276
Willy Tarreau350f4872014-11-28 14:42:25 +01001277 if (!s->si[1].err_type) {
1278 s->si[1].err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001279 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001280
Christopher Faulet29f77e82017-06-08 14:04:45 +02001281 HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001282 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001283 return 1;
1284
Willy Tarreaubaaee002006-06-26 02:48:02 +02001285 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001286 /* note: it is guaranteed that srv == NULL here */
Willy Tarreau350f4872014-11-28 14:42:25 +01001287 if (!s->si[1].err_type) {
1288 s->si[1].err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001289 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001290
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001291 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292 return 1;
1293
1294 case SRV_STATUS_QUEUED:
Willy Tarreau350f4872014-11-28 14:42:25 +01001295 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1296 s->si[1].state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001297 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298 return 1;
1299
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 case SRV_STATUS_INTERNAL:
1301 default:
Willy Tarreau350f4872014-11-28 14:42:25 +01001302 if (!s->si[1].err_type) {
1303 s->si[1].err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001304 }
1305
Willy Tarreau827aee92011-03-10 16:55:02 +01001306 if (srv)
1307 srv_inc_sess_ctr(srv);
1308 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001309 srv_set_sess_last(srv);
1310 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02001311 HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001312 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313
Willy Tarreau87b09662015-04-03 00:22:06 +02001314 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001315 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001316 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317 return 1;
1318 }
1319 /* if we get here, it's because we got SRV_STATUS_OK, which also
1320 * means that the connection has not been queued.
1321 */
1322 return 0;
1323}
1324
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001325/* sends a log message when a backend goes down, and also sets last
1326 * change date.
1327 */
1328void set_backend_down(struct proxy *be)
1329{
1330 be->last_change = now.tv_sec;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001331 HA_ATOMIC_ADD(&be->down_trans, 1);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001332
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001333 if (!(global.mode & MODE_STARTING)) {
1334 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1335 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1336 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001337}
1338
Willy Tarreau87b09662015-04-03 00:22:06 +02001339/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001340 * tries to extract an RDP cookie from the request buffer, and look for the
1341 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02001342 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001343 * list. Nothing is performed if a server was already assigned.
1344 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001345int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001346{
1347 struct proxy *px = s->be;
1348 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001349 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001350 struct server *srv = px->srv;
Willy Tarreau04276f32017-01-06 17:41:29 +01001351 uint16_t port;
1352 uint32_t addr;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001353 char *p;
1354
Willy Tarreau87b09662015-04-03 00:22:06 +02001355 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 +02001356 now_ms, __FUNCTION__,
1357 s,
1358 req,
1359 req->rex, req->wex,
1360 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001361 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001362 req->analysers);
1363
Willy Tarreaue7dff022015-04-03 01:14:29 +02001364 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001365 goto no_cookie;
1366
Willy Tarreau37406352012-04-23 16:16:37 +02001367 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001368
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001369 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001370 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001371 goto no_cookie;
1372
Willy Tarreau04276f32017-01-06 17:41:29 +01001373 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return.
1374 * The cookie format is <ip> "." <port> where "ip" is the integer corresponding to the
1375 * server's IP address in network order, and "port" is the integer corresponding to the
1376 * server's port in network order. Comments please Emeric.
1377 */
1378 addr = strtoul(smp.data.u.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001379 if (*p != '.')
1380 goto no_cookie;
1381 p++;
Willy Tarreau04276f32017-01-06 17:41:29 +01001382
1383 port = ntohs(strtoul(p, &p, 10));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001384 if (*p != '.')
1385 goto no_cookie;
1386
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001387 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001388 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001389 if (srv->addr.ss_family == AF_INET &&
Willy Tarreau04276f32017-01-06 17:41:29 +01001390 port == srv->svc_port &&
1391 addr == ((struct sockaddr_in *)&srv->addr)->sin_addr.s_addr) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001392 if ((srv->cur_state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001393 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001394 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001395 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001396 break;
1397 }
1398 }
1399 srv = srv->next;
1400 }
1401
1402no_cookie:
1403 req->analysers &= ~an_bit;
1404 req->analyse_exp = TICK_ETERNITY;
1405 return 1;
1406}
1407
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001408int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001409 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001410 return px->down_time;
1411
1412 return now.tv_sec - px->last_change + px->down_time;
1413}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001414
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001415/*
1416 * This function returns a string containing the balancing
1417 * mode of the proxy in a format suitable for stats.
1418 */
1419
1420const char *backend_lb_algo_str(int algo) {
1421
1422 if (algo == BE_LB_ALGO_RR)
1423 return "roundrobin";
1424 else if (algo == BE_LB_ALGO_SRR)
1425 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001426 else if (algo == BE_LB_ALGO_FAS)
1427 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001428 else if (algo == BE_LB_ALGO_LC)
1429 return "leastconn";
1430 else if (algo == BE_LB_ALGO_SH)
1431 return "source";
1432 else if (algo == BE_LB_ALGO_UH)
1433 return "uri";
1434 else if (algo == BE_LB_ALGO_PH)
1435 return "url_param";
1436 else if (algo == BE_LB_ALGO_HH)
1437 return "hdr";
1438 else if (algo == BE_LB_ALGO_RCH)
1439 return "rdp-cookie";
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001440 else if (algo == BE_LB_ALGO_NONE)
1441 return "none";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001442 else
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001443 return "unknown";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001444}
1445
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001446/* This function parses a "balance" statement in a backend section describing
1447 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001448 * returns -1, it will write an error message into the <err> buffer which will
1449 * automatically be allocated and must be passed as NULL. The trailing '\n'
1450 * will not be written. The function must be called with <args> pointing to the
1451 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001452 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001453int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001454{
1455 if (!*(args[0])) {
1456 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001457 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1458 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001459 return 0;
1460 }
1461
1462 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001463 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1464 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001465 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001466 else if (!strcmp(args[0], "static-rr")) {
1467 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1468 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1469 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001470 else if (!strcmp(args[0], "first")) {
1471 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1472 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1473 }
Willy Tarreau51406232008-03-10 22:04:20 +01001474 else if (!strcmp(args[0], "leastconn")) {
1475 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1476 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1477 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001478 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001479 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1480 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001481 }
1482 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001483 int arg = 1;
1484
Willy Tarreau31682232007-11-29 15:38:04 +01001485 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1486 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001487
Oskar Stolc8dc41842012-05-19 10:19:54 +01001488 curproxy->uri_whole = 0;
1489
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001490 while (*args[arg]) {
1491 if (!strcmp(args[arg], "len")) {
1492 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001493 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001494 return -1;
1495 }
1496 curproxy->uri_len_limit = atoi(args[arg+1]);
1497 arg += 2;
1498 }
1499 else if (!strcmp(args[arg], "depth")) {
1500 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001501 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001502 return -1;
1503 }
1504 /* hint: we store the position of the ending '/' (depth+1) so
1505 * that we avoid a comparison while computing the hash.
1506 */
1507 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1508 arg += 2;
1509 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001510 else if (!strcmp(args[arg], "whole")) {
1511 curproxy->uri_whole = 1;
1512 arg += 1;
1513 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001514 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001515 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001516 return -1;
1517 }
1518 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001519 }
Willy Tarreau01732802007-11-01 22:48:15 +01001520 else if (!strcmp(args[0], "url_param")) {
1521 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001522 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001523 return -1;
1524 }
Willy Tarreau31682232007-11-29 15:38:04 +01001525 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1526 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001527
1528 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001529 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001530 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001531 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001532 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001533 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001534 return -1;
1535 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001536 }
Benoitaffb4812009-03-25 13:02:10 +01001537 }
1538 else if (!strncmp(args[0], "hdr(", 4)) {
1539 const char *beg, *end;
1540
1541 beg = args[0] + 4;
1542 end = strchr(beg, ')');
1543
1544 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001545 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001546 return -1;
1547 }
1548
1549 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1550 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1551
1552 free(curproxy->hh_name);
1553 curproxy->hh_len = end - beg;
1554 curproxy->hh_name = my_strndup(beg, end - beg);
1555 curproxy->hh_match_domain = 0;
1556
1557 if (*args[1]) {
1558 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001559 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001560 return -1;
1561 }
1562 curproxy->hh_match_domain = 1;
1563 }
Emeric Brun736aa232009-06-30 17:56:00 +02001564 }
1565 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1566 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1567 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1568
1569 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1570 const char *beg, *end;
1571
1572 beg = args[0] + 11;
1573 end = strchr(beg, ')');
1574
1575 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001576 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001577 return -1;
1578 }
1579
1580 free(curproxy->hh_name);
1581 curproxy->hh_name = my_strndup(beg, end - beg);
1582 curproxy->hh_len = end - beg;
1583 }
1584 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1585 free(curproxy->hh_name);
1586 curproxy->hh_name = strdup("mstshash");
1587 curproxy->hh_len = strlen(curproxy->hh_name);
1588 }
1589 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001590 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001591 return -1;
1592 }
Willy Tarreau01732802007-11-01 22:48:15 +01001593 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001594 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001595 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 +01001596 return -1;
1597 }
1598 return 0;
1599}
1600
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001601
1602/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001603/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001604/************************************************************************/
1605
Willy Tarreau34db1082012-04-19 17:16:54 +02001606/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001607 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001608 * undefined behaviour.
1609 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001610static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001611smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001612{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001613 struct proxy *px;
1614
Willy Tarreau37406352012-04-23 16:16:37 +02001615 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001616 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001617 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001618
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01001619 smp->data.u.sint = be_usable_srv(px);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001620
1621 return 1;
1622}
1623
Willy Tarreau37406352012-04-23 16:16:37 +02001624/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001625 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001626 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1627 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001628 */
1629static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001630smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001631{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001632 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001633
Willy Tarreau37406352012-04-23 16:16:37 +02001634 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001635 smp->data.type = SMP_T_BOOL;
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 if (!(srv->cur_admin & SRV_ADMF_MAINT) &&
1637 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->cur_state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001638 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001639 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001640 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001641 return 1;
1642}
1643
Willy Tarreau34db1082012-04-19 17:16:54 +02001644/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001645 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001646 * undefined behaviour.
1647 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001648static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001649smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001650{
1651 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001652
Willy Tarreau37406352012-04-23 16:16:37 +02001653 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001654 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001655 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001656
Willy Tarreau24e32d82012-04-23 23:55:44 +02001657 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001658 if (iterator->cur_state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001659 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001660
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001661 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001662 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001663 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001664 return 1;
1665 }
1666
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001667 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001668 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001669 }
1670
1671 return 1;
1672}
1673
Willy Tarreaua5e37562011-12-16 17:06:15 +01001674/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001675static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001676smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001677{
Willy Tarreaube508f12016-03-10 11:47:01 +01001678 if (!smp->strm)
1679 return 0;
1680
Willy Tarreauf853c462012-04-23 18:53:56 +02001681 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001682 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001683 smp->data.u.sint = smp->strm->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001684 return 1;
1685}
1686
Marcin Deranekd2471c22016-12-12 14:08:05 +01001687/* set string to the name of the backend */
1688static int
1689smp_fetch_be_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1690{
1691 if (!smp->strm)
1692 return 0;
1693
1694 smp->data.u.str.str = (char *)smp->strm->be->id;
1695 if (!smp->data.u.str.str)
1696 return 0;
1697
1698 smp->data.type = SMP_T_STR;
1699 smp->flags = SMP_F_CONST;
1700 smp->data.u.str.len = strlen(smp->data.u.str.str);
1701
1702 return 1;
1703}
1704
Willy Tarreaua5e37562011-12-16 17:06:15 +01001705/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001707smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001708{
Willy Tarreaube508f12016-03-10 11:47:01 +01001709 if (!smp->strm)
1710 return 0;
1711
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001712 if (!objt_server(smp->strm->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001713 return 0;
1714
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001715 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001716 smp->data.u.sint = objt_server(smp->strm->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001717
1718 return 1;
1719}
1720
Willy Tarreau34db1082012-04-19 17:16:54 +02001721/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001722 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001723 * undefined behaviour.
1724 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001725static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001726smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001727{
Willy Tarreau37406352012-04-23 16:16:37 +02001728 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001729 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001730 smp->data.u.sint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001731 return 1;
1732}
1733
Willy Tarreau34db1082012-04-19 17:16:54 +02001734/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001735 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001736 * undefined behaviour.
1737 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001738static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001739smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001740{
Willy Tarreau37406352012-04-23 16:16:37 +02001741 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001742 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001743 smp->data.u.sint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001744 return 1;
1745}
1746
Willy Tarreau34db1082012-04-19 17:16:54 +02001747/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001748 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001749 * undefined behaviour.
1750 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001751static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001752smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001753{
Willy Tarreau37406352012-04-23 16:16:37 +02001754 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001755 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001756 smp->data.u.sint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001757 return 1;
1758}
1759
Willy Tarreaua5e37562011-12-16 17:06:15 +01001760/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001761 * by the number of running servers and rounded up. If there is no running
1762 * server, we return twice the total, just as if we had half a running server.
1763 * This is more or less correct anyway, since we expect the last server to come
1764 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001765 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001766 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001767 */
1768static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001769smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001770{
1771 int nbsrv;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001772 struct proxy *px;
Willy Tarreaua36af912009-10-10 12:02:45 +02001773
Willy Tarreau37406352012-04-23 16:16:37 +02001774 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001775 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001776 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001777
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01001778 nbsrv = be_usable_srv(px);
Willy Tarreaua36af912009-10-10 12:02:45 +02001779
1780 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001781 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001782 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001783 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001784
1785 return 1;
1786}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001787
Willy Tarreau34db1082012-04-19 17:16:54 +02001788/* set temp integer to the number of concurrent connections on the server in the backend.
1789 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1790 * undefined behaviour.
1791 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001792static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001793smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001794{
Willy Tarreauf853c462012-04-23 18:53:56 +02001795 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001796 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001797 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001798 return 1;
1799}
1800
Willy Tarreauff2b7af2017-10-13 11:46:26 +02001801/* set temp integer to the number of connections pending in the server's queue.
1802 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1803 * undefined behaviour.
1804 */
1805static int
1806smp_fetch_srv_queue(const struct arg *args, struct sample *smp, const char *kw, void *private)
1807{
1808 smp->flags = SMP_F_VOL_TEST;
1809 smp->data.type = SMP_T_SINT;
1810 smp->data.u.sint = args->data.srv->nbpend;
1811 return 1;
1812}
1813
Tait Clarridge7896d522012-12-05 21:39:31 -05001814/* set temp integer to the number of enabled servers on the proxy.
1815 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1816 * undefined behaviour.
1817 */
1818static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001819smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05001820{
1821 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001822 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001823 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05001824 return 1;
1825}
1826
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001827static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
1828{
1829
1830 struct proxy *px;
1831
1832 if (!smp_make_safe(smp))
1833 return 0;
1834
1835 px = proxy_find_by_name(smp->data.u.str.str, PR_CAP_BE, 0);
1836 if (!px)
1837 return 0;
1838
1839 smp->data.type = SMP_T_SINT;
1840 smp->data.u.sint = be_usable_srv(px);
1841
1842 return 1;
1843}
1844
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001845
1846/* Note: must not be declared <const> as its list will be overwritten.
1847 * Please take care of keeping this list alphabetically sorted.
1848 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001849static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001850 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1851 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1852 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
Marcin Deranekd2471c22016-12-12 14:08:05 +01001853 { "be_name", smp_fetch_be_name, 0, NULL, SMP_T_STR, SMP_USE_BKEND, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001854 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1855 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1856 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1857 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1858 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1859 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001860 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Willy Tarreauff2b7af2017-10-13 11:46:26 +02001861 { "srv_queue", smp_fetch_srv_queue, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001862 { "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 +01001863 { /* END */ },
1864}};
1865
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001866/* Note: must not be declared <const> as its list will be overwritten */
1867static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1868 { "nbsrv", sample_conv_nbsrv, 0, NULL, SMP_T_STR, SMP_T_SINT },
1869 { /* END */ },
1870}};
1871
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001872
Willy Tarreau61612d42012-04-19 18:42:05 +02001873/* Note: must not be declared <const> as its list will be overwritten.
1874 * Please take care of keeping this list alphabetically sorted.
1875 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001876static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001877 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001878}};
1879
1880
1881__attribute__((constructor))
1882static void __backend_init(void)
1883{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001884 sample_register_fetches(&smp_kws);
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01001885 sample_register_convs(&sample_conv_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001886 acl_register_keywords(&acl_kws);
1887}
1888
Willy Tarreaubaaee002006-06-26 02:48:02 +02001889/*
1890 * Local variables:
1891 * c-indent-level: 8
1892 * c-basic-offset: 8
1893 * End:
1894 */