blob: faf872c0b16345b305c257a592625bfeb292a805 [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 Tarreau75bf2c92012-08-20 17:01:35 +020054#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010055#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/task.h>
57
Willy Tarreau732eac42015-07-09 11:40:25 +020058#ifdef USE_OPENSSL
59#include <proto/ssl_sock.h>
60#endif /* USE_OPENSSL */
61
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050062int be_lastsession(const struct proxy *be)
63{
64 if (be->be_counters.last_sess)
65 return now.tv_sec - be->be_counters.last_sess;
66
67 return -1;
68}
69
Bhaskar98634f02013-10-29 23:30:51 -040070/* helper function to invoke the correct hash method */
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070071static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040072{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070073 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040074
75 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
76 case BE_LB_HFCN_DJB2:
77 hash = hash_djb2(key, len);
78 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010079 case BE_LB_HFCN_WT6:
80 hash = hash_wt6(key, len);
81 break;
Willy Tarreau324f07f2015-01-20 19:44:50 +010082 case BE_LB_HFCN_CRC32:
83 hash = hash_crc32(key, len);
84 break;
Bhaskar98634f02013-10-29 23:30:51 -040085 case BE_LB_HFCN_SDBM:
86 /* this is the default hash function */
87 default:
88 hash = hash_sdbm(key, len);
89 break;
90 }
91
92 return hash;
93}
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/*
96 * This function recounts the number of usable active and backup servers for
97 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010098 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010099 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200102void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103{
104 struct server *srv;
105
Willy Tarreau20697042007-11-15 23:26:18 +0100106 px->srv_act = px->srv_bck = 0;
107 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100108 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200110 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100111 continue;
112
Willy Tarreauc93cd162014-05-13 15:54:22 +0200113 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100114 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100115 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100116 px->lbprm.fbck = srv;
117 px->srv_bck++;
118 px->lbprm.tot_wbck += srv->eweight;
119 } else {
120 px->srv_act++;
121 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 }
123 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100124}
Willy Tarreau20697042007-11-15 23:26:18 +0100125
Willy Tarreaub625a082007-11-26 01:15:43 +0100126/* This function simply updates the backend's tot_weight and tot_used values
127 * after servers weights have been updated. It is designed to be used after
128 * recount_servers() or equivalent.
129 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200130void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100131{
Willy Tarreau20697042007-11-15 23:26:18 +0100132 if (px->srv_act) {
133 px->lbprm.tot_weight = px->lbprm.tot_wact;
134 px->lbprm.tot_used = px->srv_act;
135 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100136 else if (px->lbprm.fbck) {
137 /* use only the first backup server */
138 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
139 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100140 }
141 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100142 px->lbprm.tot_weight = px->lbprm.tot_wbck;
143 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100144 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100145}
146
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200147/*
148 * This function tries to find a running server for the proxy <px> following
149 * the source hash method. Depending on the number of active/backup servers,
150 * it will either look for active servers, or for backup servers.
151 * If any server is found, it will be returned. If no valid server is found,
152 * NULL is returned.
153 */
154struct server *get_server_sh(struct proxy *px, const char *addr, int len)
155{
156 unsigned int h, l;
157
158 if (px->lbprm.tot_weight == 0)
159 return NULL;
160
161 l = h = 0;
162
163 /* note: we won't hash if there's only one server left */
164 if (px->lbprm.tot_used == 1)
165 goto hash_done;
166
167 while ((l + sizeof (int)) <= len) {
168 h ^= ntohl(*(unsigned int *)(&addr[l]));
169 l += sizeof (int);
170 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500171 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100172 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200173 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200174 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
175 return chash_get_server_hash(px, h);
176 else
177 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200178}
179
180/*
181 * This function tries to find a running server for the proxy <px> following
182 * the URI hash method. In order to optimize cache hits, the hash computation
183 * ends at the question mark. Depending on the number of active/backup servers,
184 * it will either look for active servers, or for backup servers.
185 * If any server is found, it will be returned. If no valid server is found,
186 * NULL is returned.
187 *
188 * This code was contributed by Guillaume Dallaire, who also selected this hash
189 * algorithm out of a tens because it gave him the best results.
190 *
191 */
192struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
193{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700194 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200195 int c;
196 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400197 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200198
199 if (px->lbprm.tot_weight == 0)
200 return NULL;
201
202 /* note: we won't hash if there's only one server left */
203 if (px->lbprm.tot_used == 1)
204 goto hash_done;
205
206 if (px->uri_len_limit)
207 uri_len = MIN(uri_len, px->uri_len_limit);
208
Bhaskar98634f02013-10-29 23:30:51 -0400209 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200210 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200211 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200212 if (c == '/') {
213 slashes++;
214 if (slashes == px->uri_dirs_depth1) /* depth+1 */
215 break;
216 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100217 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200218 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200219 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200220 }
Bhaskar98634f02013-10-29 23:30:51 -0400221
222 hash = gen_hash(px, start, (end - start));
223
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500224 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100225 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200226 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200227 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
228 return chash_get_server_hash(px, hash);
229 else
230 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200231}
232
Willy Tarreau01732802007-11-01 22:48:15 +0100233/*
234 * This function tries to find a running server for the proxy <px> following
235 * the URL parameter hash method. It looks for a specific parameter in the
236 * URL and hashes it to compute the server ID. This is useful to optimize
237 * performance by avoiding bounces between servers in contexts where sessions
238 * are shared but cookies are not usable. If the parameter is not found, NULL
239 * is returned. If any server is found, it will be returned. If no valid server
240 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100241 */
242struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
243{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700244 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400245 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200246 const char *p;
247 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100248 int plen;
249
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100251 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100252 return NULL;
253
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 if ((p = memchr(uri, '?', uri_len)) == NULL)
255 return NULL;
256
Willy Tarreau01732802007-11-01 22:48:15 +0100257 p++;
258
259 uri_len -= (p - uri);
260 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100262
263 while (uri_len > plen) {
264 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200265 if (params[plen] == '=') {
266 if (memcmp(params, px->url_param_name, plen) == 0) {
267 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100268 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100270 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400272 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200273 uri_len -= plen + 1;
274
Bhaskar98634f02013-10-29 23:30:51 -0400275 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100276 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400277 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100278 }
Bhaskar98634f02013-10-29 23:30:51 -0400279 hash = gen_hash(px, start, (end - start));
280
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500281 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100282 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400283
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200284 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
285 return chash_get_server_hash(px, hash);
286 else
287 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100288 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200289 }
290 /* skip to next parameter */
291 p = memchr(params, '&', uri_len);
292 if (!p)
293 return NULL;
294 p++;
295 uri_len -= (p - params);
296 params = p;
297 }
298 return NULL;
299}
300
301/*
302 * this does the same as the previous server_ph, but check the body contents
303 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200304struct server *get_server_ph_post(struct stream *s)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700306 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200307 struct http_txn *txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100308 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 struct http_msg *msg = &txn->req;
310 struct proxy *px = s->be;
311 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200312 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200313 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100314 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400315 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200316
Willy Tarreau157dd632009-12-06 19:18:09 +0100317 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200318 return NULL;
319
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200320 if (len > req->buf->data + req->buf->size - p)
321 len = req->buf->data + req->buf->size - p;
322
Willy Tarreau157dd632009-12-06 19:18:09 +0100323 if (px->lbprm.tot_weight == 0)
324 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200325
326 while (len > plen) {
327 /* Look for the parameter name followed by an equal symbol */
328 if (params[plen] == '=') {
329 if (memcmp(params, px->url_param_name, plen) == 0) {
330 /* OK, we have the parameter here at <params>, and
331 * the value after the equal sign, at <p>
332 * skip the equal symbol
333 */
334 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400335 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200336 len -= plen + 1;
337
Bhaskar98634f02013-10-29 23:30:51 -0400338 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200339 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100340 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400341 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100342 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200343 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
344 break;
345 return NULL; /* oh, no; this is not uri-encoded.
346 * This body does not contain parameters.
347 */
348 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200349 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400350 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200351 /* should we break if vlen exceeds limit? */
352 }
Bhaskar98634f02013-10-29 23:30:51 -0400353 hash = gen_hash(px, start, (end - start));
354
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500355 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100356 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400357
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200358 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
359 return chash_get_server_hash(px, hash);
360 else
361 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200362 }
363 }
Willy Tarreau01732802007-11-01 22:48:15 +0100364 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200365 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100366 if (!p)
367 return NULL;
368 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200369 len -= (p - params);
370 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100371 }
372 return NULL;
373}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200375
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376/*
Benoitaffb4812009-03-25 13:02:10 +0100377 * This function tries to find a running server for the proxy <px> following
378 * the Header parameter hash method. It looks for a specific parameter in the
379 * URL and hashes it to compute the server ID. This is useful to optimize
380 * performance by avoiding bounces between servers in contexts where sessions
381 * are shared but cookies are not usable. If the parameter is not found, NULL
382 * is returned. If any server is found, it will be returned. If no valid server
383 * is found, NULL is returned.
384 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200385struct server *get_server_hh(struct stream *s)
Benoitaffb4812009-03-25 13:02:10 +0100386{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700387 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200388 struct http_txn *txn = s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100389 struct proxy *px = s->be;
390 unsigned int plen = px->hh_len;
391 unsigned long len;
392 struct hdr_ctx ctx;
393 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400394 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100395
396 /* tot_weight appears to mean srv_count */
397 if (px->lbprm.tot_weight == 0)
398 return NULL;
399
Benoitaffb4812009-03-25 13:02:10 +0100400 ctx.idx = 0;
401
402 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100403 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 +0100404
405 /* if the header is not found or empty, let's fallback to round robin */
406 if (!ctx.idx || !ctx.vlen)
407 return NULL;
408
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200409 /* note: we won't hash if there's only one server left */
410 if (px->lbprm.tot_used == 1)
411 goto hash_done;
412
Benoitaffb4812009-03-25 13:02:10 +0100413 /* Found a the hh_name in the headers.
414 * we will compute the hash based on this value ctx.val.
415 */
416 len = ctx.vlen;
417 p = (char *)ctx.line + ctx.val;
418 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400419 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100420 } else {
421 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100422 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100423 /* special computation, use only main domain name, not tld/host
424 * going back from the end of string, start hashing at first
425 * dot stop at next.
426 * This is designed to work with the 'Host' header, and requires
427 * a special option to activate this.
428 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100429 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100430 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100431 if (dohash) {
432 /* Rewind the pointer until the previous char
433 * is a dot, this will allow to set the start
434 * position of the domain. */
435 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100436 break;
Benoitaffb4812009-03-25 13:02:10 +0100437 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100438 else if (*p == '.') {
439 /* The pointer is rewinded to the dot before the
440 * tld, we memorize the end of the domain and
441 * can enter the domain processing. */
442 end = p;
443 dohash = 1;
444 }
Benoitaffb4812009-03-25 13:02:10 +0100445 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100446 len--;
Benoitaffb4812009-03-25 13:02:10 +0100447 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100448 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400449 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100450 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500451 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100452 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200453 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200454 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
455 return chash_get_server_hash(px, hash);
456 else
457 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100458}
459
Willy Tarreau34db1082012-04-19 17:16:54 +0200460/* RDP Cookie HASH. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200461struct server *get_server_rch(struct stream *s)
Emeric Brun736aa232009-06-30 17:56:00 +0200462{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700463 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200464 struct proxy *px = s->be;
465 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200466 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200467 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200468 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200469
470 /* tot_weight appears to mean srv_count */
471 if (px->lbprm.tot_weight == 0)
472 return NULL;
473
Willy Tarreau37406352012-04-23 16:16:37 +0200474 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 b_rew(s->req.buf, rewind = s->req.buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200477
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200478 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200479 len = smp.data.u.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100480
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100481 b_adv(s->req.buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200482
Willy Tarreau37406352012-04-23 16:16:37 +0200483 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200484 return NULL;
485
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200486 /* note: we won't hash if there's only one server left */
487 if (px->lbprm.tot_used == 1)
488 goto hash_done;
489
Emeric Brun736aa232009-06-30 17:56:00 +0200490 /* Found a the hh_name in the headers.
491 * we will compute the hash based on this value ctx.val.
492 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200493 hash = gen_hash(px, smp.data.u.str.str, len);
Bhaskar98634f02013-10-29 23:30:51 -0400494
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500495 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100496 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200497 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200498 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
499 return chash_get_server_hash(px, hash);
500 else
501 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200502}
Benoitaffb4812009-03-25 13:02:10 +0100503
504/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200505 * This function applies the load-balancing algorithm to the stream, as
506 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200507 * 'assigned'.
508 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200509 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200510 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100511 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200512 * The function tries to keep the original connection slot if it reconnects to
513 * the same server, otherwise it releases it and tries to offer it.
514 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200515 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516 *
517 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100518 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200519 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
520 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521 * SRV_STATUS_INTERNAL for other unrecoverable errors.
522 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200523 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100524 * it does not need to be called anymore. This means that target_srv(&s->target)
525 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 *
527 */
528
Willy Tarreau87b09662015-04-03 00:22:06 +0200529int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200531 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200532 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100533 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100535
Simon Horman8effd3d2011-08-13 08:03:52 +0900536 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau7c669d72008-06-20 15:04:11 +0200538 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200539 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200540 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100541
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100542 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200543 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544
Willy Tarreau7c669d72008-06-20 15:04:11 +0200545 /* We have to release any connection slot before applying any LB algo,
546 * otherwise we may erroneously end up with no available slot.
547 */
548 if (conn_slot)
549 sess_change_server(s, NULL);
550
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100551 /* We will now try to find the good server and store it into <objt_server(s->target)>.
552 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200553 * as well as if no server is available (check error code).
554 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100555
Willy Tarreau827aee92011-03-10 16:55:02 +0100556 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100557 s->target = NULL;
Willy Tarreau350f4872014-11-28 14:42:25 +0100558 conn = objt_conn(s->si[1].end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100559
Willy Tarreau068621e2013-12-23 15:11:25 +0100560 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100561 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100562 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreaueee5b512015-04-03 23:46:31 +0200563 ((s->txn && s->txn->flags & TX_PREFER_LAST) ||
Willy Tarreauc35362a2014-04-25 13:58:37 +0200564 ((s->be->options & PR_O_PREF_LAST) &&
565 (!s->be->max_ka_queue ||
566 server_has_room(__objt_server(conn->target)) ||
567 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200568 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200569 /* This stream was relying on a server in a previous request
Godbachf2dd68d2014-12-10 10:21:30 +0800570 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100571 * let's try to reuse the same server.
572 */
573 srv = __objt_server(conn->target);
574 s->target = &srv->obj_type;
575 }
576 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200577 /* we must check if we have at least one server available */
578 if (!s->be->lbprm.tot_weight) {
579 err = SRV_STATUS_NOSRV;
580 goto out;
581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583 /* First check whether we need to fetch some data or simply call
584 * the LB lookup function. Only the hashing functions will need
585 * some input data in fact, and will support multiple algorithms.
586 */
587 switch (s->be->lbprm.algo & BE_LB_LKUP) {
588 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100589 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200590 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591
Willy Tarreauf09c6602012-02-13 17:12:08 +0100592 case BE_LB_LKUP_FSTREE:
593 srv = fas_get_next_server(s->be, prev_srv);
594 break;
595
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100597 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200598 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200599
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200600 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200602 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200603 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100604 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200607 break;
608 }
609 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200610 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200611 err = SRV_STATUS_INTERNAL;
612 goto out;
613 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200614
615 switch (s->be->lbprm.algo & BE_LB_PARM) {
616 case BE_LB_HASH_SRC:
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200617 conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200618 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200619 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200620 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200621 4);
622 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200623 else if (conn && conn->addr.from.ss_family == AF_INET6) {
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_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200626 16);
627 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200628 else {
629 /* unknown IP family */
630 err = SRV_STATUS_INTERNAL;
631 goto out;
632 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200633 break;
634
635 case BE_LB_HASH_URI:
636 /* URI hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200637 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100638 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100639 srv = get_server_uh(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200640 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
641 s->txn->req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200642 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200643
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200644 case BE_LB_HASH_PRM:
645 /* URL Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200646 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100647 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100648
Willy Tarreau827aee92011-03-10 16:55:02 +0100649 srv = get_server_ph(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200650 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
651 s->txn->req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100652
Willy Tarreaueee5b512015-04-03 23:46:31 +0200653 if (!srv && s->txn->meth == HTTP_METH_POST)
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200655 break;
Benoitaffb4812009-03-25 13:02:10 +0100656
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200657 case BE_LB_HASH_HDR:
658 /* Header Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200659 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100660 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100661 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200662 break;
663
664 case BE_LB_HASH_RDP:
665 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100666 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667 break;
668
669 default:
670 /* unknown balancing algorithm */
671 err = SRV_STATUS_INTERNAL;
672 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100673 }
Emeric Brun736aa232009-06-30 17:56:00 +0200674
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200675 /* If the hashing parameter was not found, let's fall
676 * back to round robin on the map.
677 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100678 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200679 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100680 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200681 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100682 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200683 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200684
685 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200686 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687
Willy Tarreau7c669d72008-06-20 15:04:11 +0200688 default:
689 /* unknown balancing algorithm */
690 err = SRV_STATUS_INTERNAL;
691 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200693
Willy Tarreau827aee92011-03-10 16:55:02 +0100694 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200695 err = SRV_STATUS_FULL;
696 goto out;
697 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100698 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100699 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100700 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100701 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100702 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200703 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200704 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100705 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100707 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau350f4872014-11-28 14:42:25 +0100708 (conn = objt_conn(s->si[1].end)) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200709 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100710 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100711 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100712 }
713 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200714 err = SRV_STATUS_NOSRV;
715 goto out;
716 }
717
Willy Tarreaue7dff022015-04-03 01:14:29 +0200718 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200719 err = SRV_STATUS_OK;
720 out:
721
722 /* Either we take back our connection slot, or we offer it to someone
723 * else if we don't need it anymore.
724 */
725 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100726 if (conn_slot == srv) {
727 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200728 } else {
729 if (may_dequeue_tasks(conn_slot, s->be))
730 process_srv_queue(conn_slot);
731 }
732 }
733
734 out_err:
735 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736}
737
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738/*
Willy Tarreaue7dff022015-04-03 01:14:29 +0200739 * This function assigns a server address to a stream, and sets SF_ADDR_SET.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 * The address is taken from the currently assigned server, or from the
741 * dispatch or transparent address.
742 *
743 * It may return :
744 * SRV_STATUS_OK if everything is OK.
745 * SRV_STATUS_INTERNAL for other unrecoverable errors.
746 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200747 * Upon successful return, the stream flag SF_ADDR_SET is set. This flag is
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 * not cleared, so it's to the caller to clear it if required.
749 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200750 * The caller is responsible for having already assigned a connection
751 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200752 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200754int assign_server_address(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200756 struct connection *cli_conn = objt_conn(strm_orig(s));
Willy Tarreau350f4872014-11-28 14:42:25 +0100757 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759#ifdef DEBUG_FULL
760 fprintf(stderr,"assign_server_address : s=%p\n",s);
761#endif
762
Willy Tarreaue7dff022015-04-03 01:14:29 +0200763 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200764 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200765 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 return SRV_STATUS_INTERNAL;
767
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200770 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200771 /* if the server has no address, we use the same address
772 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200773 * locally on multiple addresses at once. Nothing is done
774 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200775 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200776 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200777
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200778 if (cli_conn->addr.to.ss_family == AF_INET) {
779 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
780 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
781 ((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 +0200782 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200783 }
784
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 /* if this server remaps proxied ports, we'll use
786 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200787 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100788 int base_port;
789
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200790 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100791
792 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200793 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100794
795 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200796 base_port += get_host_port(&srv_conn->addr.to);
797 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 }
799 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200800 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200802 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200804 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200806 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200808 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
809 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100811 else if (s->be->options & PR_O_HTTP_PROXY) {
812 /* If HTTP PROXY option is set, then server is already assigned
813 * during incoming client request parsing. */
814 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100815 else {
816 /* no server and no LB algorithm ! */
817 return SRV_STATUS_INTERNAL;
818 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100820 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100821 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100822
Willy Tarreaue7dff022015-04-03 01:14:29 +0200823 s->flags |= SF_ADDR_SET;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200824 return SRV_STATUS_OK;
825}
826
Willy Tarreau87b09662015-04-03 00:22:06 +0200827/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200829 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200830 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 * be called before any connection and after any retry or redispatch occurs.
832 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200833 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 *
835 * Returns :
836 *
837 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100838 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 * SRV_STATUS_QUEUED if the connection has been queued.
840 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100841 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200842 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 * SRV_STATUS_INTERNAL for other unrecoverable errors.
844 *
845 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200846int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847{
848 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100849 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 int err;
851
852 if (s->pend_pos)
853 return SRV_STATUS_INTERNAL;
854
Willy Tarreau7c669d72008-06-20 15:04:11 +0200855 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200856 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100857 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100858
Willy Tarreau7c669d72008-06-20 15:04:11 +0200859 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100860 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200861 /* This stream was previously assigned to a server. We have to
862 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200863 * - if the server changed :
864 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200865 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866 * - increment srv->redispatches and be->redispatches
867 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100868 */
869
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100870 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200871 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
872 s->txn->flags &= ~TX_CK_MASK;
873 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200874 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200875 s->flags |= SF_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100876 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100877 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200878 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100879 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100880 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100881 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100882 }
883 }
884
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 switch (err) {
886 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200887 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100888 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100889 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200890 return SRV_STATUS_OK; /* dispatch or proxy mode */
891
892 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100893 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200894 return SRV_STATUS_OK;
895
Willy Tarreau87b09662015-04-03 00:22:06 +0200896 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200897 * connection slot yet. Either it is a redispatch, or it was
898 * assigned from persistence information (direct mode).
899 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200900 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901 /* server scheduled for redirection, and already assigned. We
902 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100903 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100904 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100905 return SRV_STATUS_OK;
906 }
907
Willy Tarreau87b09662015-04-03 00:22:06 +0200908 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200909 * We know we have to queue it into the server's queue, so if a maxqueue
910 * is set on the server, we must also check that the server's queue is
911 * not full, in which case we have to return FULL.
912 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100913 if (srv->maxconn &&
914 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200915
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200917 return SRV_STATUS_FULL;
918
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 p = pendconn_add(s);
920 if (p)
921 return SRV_STATUS_QUEUED;
922 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200923 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200925
926 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100927 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 return SRV_STATUS_OK;
929
930 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +0200931 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 p = pendconn_add(s);
933 if (p)
934 return SRV_STATUS_QUEUED;
935 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200936 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
938 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200939 return err;
940
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941 case SRV_STATUS_INTERNAL:
942 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200943
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944 default:
945 return SRV_STATUS_INTERNAL;
946 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100947}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948
Willy Tarreaub1d67742010-03-29 19:36:59 +0200949/* If an explicit source binding is specified on the server and/or backend, and
950 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau87b09662015-04-03 00:22:06 +0200951 * assigned to the stream's pending connection. This function assumes that an
Willy Tarreau350f4872014-11-28 14:42:25 +0100952 * outgoing connection has already been assigned to s->si[1].end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200953 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200954static void assign_tproxy_address(struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +0200955{
Willy Tarreau29fbe512015-08-20 19:35:14 +0200956#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100957 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100958 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200959 struct connection *cli_conn;
Willy Tarreau350f4872014-11-28 14:42:25 +0100960 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100961
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100962 if (srv && srv->conn_src.opts & CO_SRC_BIND)
963 src = &srv->conn_src;
964 else if (s->be->conn_src.opts & CO_SRC_BIND)
965 src = &s->be->conn_src;
966 else
967 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100968
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100969 switch (src->opts & CO_SRC_TPROXY_MASK) {
970 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200971 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100972 break;
973 case CO_SRC_TPROXY_CLI:
974 case CO_SRC_TPROXY_CIP:
975 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200976 cli_conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200977 if (cli_conn)
978 srv_conn->addr.from = cli_conn->addr.from;
979 else
980 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100981 break;
982 case CO_SRC_TPROXY_DYN:
Willy Tarreaueee5b512015-04-03 23:46:31 +0200983 if (src->bind_hdr_occ && s->txn) {
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100984 char *vptr;
985 int vlen;
986 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100987
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100988 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200989 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
990 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
991 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100992
Willy Tarreaueee5b512015-04-03 23:46:31 +0200993 b_rew(s->req.buf, rewind = http_hdr_rewind(&s->txn->req));
994 if (http_get_hdr(&s->txn->req, src->bind_hdr_name, src->bind_hdr_len,
995 &s->txn->hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200996 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100997 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200998 }
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100999 b_adv(s->req.buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001000 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001001 break;
1002 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001003 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001004 }
1005#endif
1006}
1007
1008
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001010 * This function initiates a connection to the server assigned to this stream
Willy Tarreau350f4872014-11-28 14:42:25 +01001011 * (s->target, s->si[1].addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001012 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001014 * - SF_ERR_NONE if everything's OK
1015 * - SF_ERR_SRVTO if there are no more servers
1016 * - SF_ERR_SRVCL if the connection was refused by the server
1017 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1018 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1019 * - SF_ERR_INTERNAL for any other purely internal errors
1020 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001021 * The server-facing stream interface is expected to hold a pre-allocated connection
Willy Tarreau350f4872014-11-28 14:42:25 +01001022 * in s->si[1].conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001024int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001026 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001027 struct connection *srv_conn;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001028 struct connection *old_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001029 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001030 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001031 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032
Willy Tarreau7b004922015-08-04 19:34:21 +02001033 srv = objt_server(s->target);
Willy Tarreau350f4872014-11-28 14:42:25 +01001034 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau34601a82013-12-15 16:33:46 +01001035 if (srv_conn)
1036 reuse = s->target == srv_conn->target;
1037
Willy Tarreau8dff9982015-08-04 20:45:52 +02001038 if (srv && !reuse) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001039 old_conn = srv_conn;
1040 if (old_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001041 srv_conn = NULL;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001042 old_conn->owner = NULL;
1043 si_detach_endpoint(&s->si[1]);
1044 /* note: if the connection was in a server's idle
1045 * queue, it doesn't get dequeued.
1046 */
Willy Tarreau8dff9982015-08-04 20:45:52 +02001047 }
1048
Willy Tarreau449d74a2015-08-05 17:16:33 +02001049 /* Below we pick connections from the safe or idle lists based
1050 * on the strategy, the fact that this is a first or second
1051 * (retryable) request, with the indicated priority (1 or 2) :
1052 *
1053 * SAFE AGGR ALWS
1054 *
1055 * +-----+-----+ +-----+-----+ +-----+-----+
1056 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1057 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1058 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1059 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1060 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1061 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1062 */
1063
1064 if (!LIST_ISEMPTY(&srv->idle_conns) &&
1065 ((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR &&
1066 s->txn && (s->txn->flags & TX_NOT_FIRST))) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001067 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001068 }
1069 else if (!LIST_ISEMPTY(&srv->safe_conns) &&
1070 ((s->txn && (s->txn->flags & TX_NOT_FIRST)) ||
1071 (s->be->options & PR_O_REUSE_MASK) >= PR_O_REUSE_AGGR)) {
1072 srv_conn = LIST_ELEM(srv->safe_conns.n, struct connection *, list);
1073 }
1074 else if (!LIST_ISEMPTY(&srv->idle_conns) &&
1075 (s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) {
1076 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
1077 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001078
Willy Tarreau449d74a2015-08-05 17:16:33 +02001079 /* If we've picked a connection from the pool, we now have to
1080 * detach it. We may have to get rid of the previous idle
1081 * connection we had, so for this we try to swap it with the
1082 * other owner's. That way it may remain alive for others to
1083 * pick.
1084 */
1085 if (srv_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001086 LIST_DEL(&srv_conn->list);
1087 LIST_INIT(&srv_conn->list);
1088
Willy Tarreauefb90f92015-08-06 11:37:10 +02001089 if (srv_conn->owner) {
1090 si_detach_endpoint(srv_conn->owner);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001091 if (old_conn && !(old_conn->flags & CO_FL_PRIVATE)) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001092 si_attach_conn(srv_conn->owner, old_conn);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001093 si_idle_conn(srv_conn->owner, NULL);
1094 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001095 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001096 si_attach_conn(&s->si[1], srv_conn);
1097 reuse = 1;
1098 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001099
Willy Tarreau449d74a2015-08-05 17:16:33 +02001100 /* we may have to release our connection if we couldn't swap it */
Willy Tarreauefb90f92015-08-06 11:37:10 +02001101 if (old_conn && !old_conn->owner) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001102 LIST_DEL(&old_conn->list);
1103 conn_force_close(old_conn);
1104 conn_free(old_conn);
1105 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001106 }
1107
Willy Tarreau34601a82013-12-15 16:33:46 +01001108 if (reuse) {
1109 /* Disable connection reuse if a dynamic source is used.
1110 * As long as we don't share connections between servers,
1111 * we don't need to disable connection reuse on no-idempotent
1112 * requests nor when PROXY protocol is used.
1113 */
Willy Tarreau34601a82013-12-15 16:33:46 +01001114 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1115 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1116 reuse = 0;
1117 }
1118 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1119 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1120 reuse = 0;
1121 }
1122 }
1123
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001124 if (!reuse)
Willy Tarreau973a5422015-08-05 21:47:23 +02001125 srv_conn = si_alloc_conn(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02001126 else {
1127 /* reusing our connection, take it out of the idle list */
1128 LIST_DEL(&srv_conn->list);
1129 LIST_INIT(&srv_conn->list);
1130 }
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001131
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001132 if (!srv_conn)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001133 return SF_ERR_RESOURCE;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001134
Willy Tarreaue7dff022015-04-03 01:14:29 +02001135 if (!(s->flags & SF_ADDR_SET)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136 err = assign_server_address(s);
1137 if (err != SRV_STATUS_OK)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001138 return SF_ERR_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139 }
1140
Willy Tarreauff605db2013-12-20 11:09:51 +01001141 if (!conn_xprt_ready(srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001142 /* the target was only on the stream, assign it to the SI now */
Willy Tarreauff605db2013-12-20 11:09:51 +01001143 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001144
Willy Tarreauff605db2013-12-20 11:09:51 +01001145 /* set the correct protocol on the output stream interface */
Willy Tarreau7b004922015-08-04 19:34:21 +02001146 if (srv) {
1147 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), srv->xprt);
Willy Tarreauff605db2013-12-20 11:09:51 +01001148 }
1149 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1150 /* proxies exclusively run on raw_sock right now */
1151 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
Willy Tarreau350f4872014-11-28 14:42:25 +01001152 if (!objt_conn(s->si[1].end) || !objt_conn(s->si[1].end)->ctrl)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001153 return SF_ERR_INTERNAL;
Willy Tarreauff605db2013-12-20 11:09:51 +01001154 }
1155 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001156 return SF_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001157
Willy Tarreauff605db2013-12-20 11:09:51 +01001158 /* process the case where the server requires the PROXY protocol to be sent */
1159 srv_conn->send_proxy_ofs = 0;
Willy Tarreau7b004922015-08-04 19:34:21 +02001160 if (srv && srv->pp_opts) {
Willy Tarreau387ebf82015-08-04 19:24:13 +02001161 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauff605db2013-12-20 11:09:51 +01001162 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001163 cli_conn = objt_conn(strm_orig(s));
Willy Tarreauff605db2013-12-20 11:09:51 +01001164 if (cli_conn)
1165 conn_get_to_addr(cli_conn);
1166 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001167
Willy Tarreau350f4872014-11-28 14:42:25 +01001168 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001169
Willy Tarreauff605db2013-12-20 11:09:51 +01001170 assign_tproxy_address(s);
1171 }
1172 else {
1173 /* the connection is being reused, just re-attach it */
Willy Tarreau350f4872014-11-28 14:42:25 +01001174 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 s->flags |= SF_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001176 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001177
William Lallemandb7ff6a32012-03-02 14:35:21 +01001178 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001179 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Willy Tarreau350f4872014-11-28 14:42:25 +01001180 s->si[1].flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001181
Willy Tarreau14f8e862012-08-30 22:23:13 +02001182 /* disable lingering */
1183 if (s->be->options & PR_O_TCP_NOLING)
Willy Tarreau350f4872014-11-28 14:42:25 +01001184 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001185
Willy Tarreau350f4872014-11-28 14:42:25 +01001186 err = si_connect(&s->si[1]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 if (err != SF_ERR_NONE)
Willy Tarreau9650f372009-08-16 14:02:45 +02001189 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001190
Willy Tarreau14f8e862012-08-30 22:23:13 +02001191 /* set connect timeout */
Willy Tarreau350f4872014-11-28 14:42:25 +01001192 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001193
Willy Tarreau827aee92011-03-10 16:55:02 +01001194 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001195 s->flags |= SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001196 srv->cur_sess++;
1197 if (srv->cur_sess > srv->counters.cur_sess_max)
1198 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001199 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001200 s->be->lbprm.server_take_conn(srv);
Willy Tarreau732eac42015-07-09 11:40:25 +02001201
1202#ifdef USE_OPENSSL
1203 if (srv->ssl_ctx.sni) {
1204 struct sample *smp;
1205 int rewind;
1206
1207 /* Tricky case : we have already scheduled the pending
1208 * HTTP request or TCP data for leaving. So in HTTP we
1209 * rewind exactly the headers, otherwise we rewind the
1210 * output data.
1211 */
1212 rewind = s->txn ? http_hdr_rewind(&s->txn->req) : s->req.buf->o;
1213 b_rew(s->req.buf, rewind);
1214
1215 smp = sample_fetch_as_type(s->be, s->sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, srv->ssl_ctx.sni, SMP_T_STR);
1216
1217 /* restore the pointers */
1218 b_adv(s->req.buf, rewind);
1219
Willy Tarreau2e0565c2016-08-09 11:55:21 +02001220 if (smp_make_safe(smp)) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001221 ssl_sock_set_servername(srv_conn, smp->data.u.str.str);
Willy Tarreau387ebf82015-08-04 19:24:13 +02001222 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau732eac42015-07-09 11:40:25 +02001223 }
1224 }
1225#endif /* USE_OPENSSL */
1226
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 }
1228
Willy Tarreaue7dff022015-04-03 01:14:29 +02001229 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230}
1231
1232
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233/* This function performs the "redispatch" part of a connection attempt. It
1234 * will assign a server if required, queue the connection if required, and
1235 * handle errors that might arise at this level. It can change the server
1236 * state. It will return 1 if it encounters an error, switches the server
1237 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1238 * that the connection is ready to use.
1239 */
1240
Willy Tarreau87b09662015-04-03 00:22:06 +02001241int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242{
Willy Tarreau827aee92011-03-10 16:55:02 +01001243 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 int conn_err;
1245
1246 /* We know that we don't have any connection pending, so we will
1247 * try to get a new one, and wait in this state if it's queued
1248 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001249 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001250 conn_err = assign_server_and_queue(s);
1251 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001252
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253 switch (conn_err) {
1254 case SRV_STATUS_OK:
1255 break;
1256
Willy Tarreau7c669d72008-06-20 15:04:11 +02001257 case SRV_STATUS_FULL:
1258 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1259 * and we can redispatch to another server, or it is not and we return
1260 * 503. This only makes sense in DIRECT mode however, because normal LB
1261 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001262 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001263 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001264 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001265 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001266 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001267 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001268 goto redispatch;
1269 }
1270
Willy Tarreau350f4872014-11-28 14:42:25 +01001271 if (!s->si[1].err_type) {
1272 s->si[1].err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001273 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001274
Willy Tarreau827aee92011-03-10 16:55:02 +01001275 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001276 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001277 return 1;
1278
Willy Tarreaubaaee002006-06-26 02:48:02 +02001279 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001280 /* note: it is guaranteed that srv == NULL here */
Willy Tarreau350f4872014-11-28 14:42:25 +01001281 if (!s->si[1].err_type) {
1282 s->si[1].err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001283 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001284
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001285 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001286 return 1;
1287
1288 case SRV_STATUS_QUEUED:
Willy Tarreau350f4872014-11-28 14:42:25 +01001289 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1290 s->si[1].state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001291 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292 return 1;
1293
Willy Tarreaubaaee002006-06-26 02:48:02 +02001294 case SRV_STATUS_INTERNAL:
1295 default:
Willy Tarreau350f4872014-11-28 14:42:25 +01001296 if (!s->si[1].err_type) {
1297 s->si[1].err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001298 }
1299
Willy Tarreau827aee92011-03-10 16:55:02 +01001300 if (srv)
1301 srv_inc_sess_ctr(srv);
1302 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001303 srv_set_sess_last(srv);
1304 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001305 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001306 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307
Willy Tarreau87b09662015-04-03 00:22:06 +02001308 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001309 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001310 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311 return 1;
1312 }
1313 /* if we get here, it's because we got SRV_STATUS_OK, which also
1314 * means that the connection has not been queued.
1315 */
1316 return 0;
1317}
1318
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001319/* sends a log message when a backend goes down, and also sets last
1320 * change date.
1321 */
1322void set_backend_down(struct proxy *be)
1323{
1324 be->last_change = now.tv_sec;
1325 be->down_trans++;
1326
1327 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1328 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1329}
1330
Willy Tarreau87b09662015-04-03 00:22:06 +02001331/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001332 * tries to extract an RDP cookie from the request buffer, and look for the
1333 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02001334 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001335 * list. Nothing is performed if a server was already assigned.
1336 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001337int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001338{
1339 struct proxy *px = s->be;
1340 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001341 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001342 struct server *srv = px->srv;
1343 struct sockaddr_in addr;
1344 char *p;
1345
Willy Tarreau87b09662015-04-03 00:22:06 +02001346 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 +02001347 now_ms, __FUNCTION__,
1348 s,
1349 req,
1350 req->rex, req->wex,
1351 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001352 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001353 req->analysers);
1354
Willy Tarreaue7dff022015-04-03 01:14:29 +02001355 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001356 goto no_cookie;
1357
Willy Tarreau37406352012-04-23 16:16:37 +02001358 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001359
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001360 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001361 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001362 goto no_cookie;
1363
1364 memset(&addr, 0, sizeof(addr));
1365 addr.sin_family = AF_INET;
1366
Willy Tarreau664092c2011-12-16 19:11:42 +01001367 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001368 addr.sin_addr.s_addr = strtoul(smp.data.u.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001369 if (*p != '.')
1370 goto no_cookie;
1371 p++;
1372 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1373 if (*p != '.')
1374 goto no_cookie;
1375
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001376 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001377 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001378 if (srv->addr.ss_family == AF_INET &&
1379 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001380 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001381 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001382 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001383 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001384 break;
1385 }
1386 }
1387 srv = srv->next;
1388 }
1389
1390no_cookie:
1391 req->analysers &= ~an_bit;
1392 req->analyse_exp = TICK_ETERNITY;
1393 return 1;
1394}
1395
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001396int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001397 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001398 return px->down_time;
1399
1400 return now.tv_sec - px->last_change + px->down_time;
1401}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001402
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001403/*
1404 * This function returns a string containing the balancing
1405 * mode of the proxy in a format suitable for stats.
1406 */
1407
1408const char *backend_lb_algo_str(int algo) {
1409
1410 if (algo == BE_LB_ALGO_RR)
1411 return "roundrobin";
1412 else if (algo == BE_LB_ALGO_SRR)
1413 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001414 else if (algo == BE_LB_ALGO_FAS)
1415 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001416 else if (algo == BE_LB_ALGO_LC)
1417 return "leastconn";
1418 else if (algo == BE_LB_ALGO_SH)
1419 return "source";
1420 else if (algo == BE_LB_ALGO_UH)
1421 return "uri";
1422 else if (algo == BE_LB_ALGO_PH)
1423 return "url_param";
1424 else if (algo == BE_LB_ALGO_HH)
1425 return "hdr";
1426 else if (algo == BE_LB_ALGO_RCH)
1427 return "rdp-cookie";
1428 else
1429 return NULL;
1430}
1431
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001432/* This function parses a "balance" statement in a backend section describing
1433 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001434 * returns -1, it will write an error message into the <err> buffer which will
1435 * automatically be allocated and must be passed as NULL. The trailing '\n'
1436 * will not be written. The function must be called with <args> pointing to the
1437 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001438 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001439int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001440{
1441 if (!*(args[0])) {
1442 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001443 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1444 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001445 return 0;
1446 }
1447
1448 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001449 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1450 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001451 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001452 else if (!strcmp(args[0], "static-rr")) {
1453 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1454 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1455 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001456 else if (!strcmp(args[0], "first")) {
1457 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1458 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1459 }
Willy Tarreau51406232008-03-10 22:04:20 +01001460 else if (!strcmp(args[0], "leastconn")) {
1461 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1462 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1463 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001464 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001465 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1466 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001467 }
1468 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001469 int arg = 1;
1470
Willy Tarreau31682232007-11-29 15:38:04 +01001471 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1472 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001473
Oskar Stolc8dc41842012-05-19 10:19:54 +01001474 curproxy->uri_whole = 0;
1475
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001476 while (*args[arg]) {
1477 if (!strcmp(args[arg], "len")) {
1478 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001479 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001480 return -1;
1481 }
1482 curproxy->uri_len_limit = atoi(args[arg+1]);
1483 arg += 2;
1484 }
1485 else if (!strcmp(args[arg], "depth")) {
1486 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001487 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001488 return -1;
1489 }
1490 /* hint: we store the position of the ending '/' (depth+1) so
1491 * that we avoid a comparison while computing the hash.
1492 */
1493 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1494 arg += 2;
1495 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001496 else if (!strcmp(args[arg], "whole")) {
1497 curproxy->uri_whole = 1;
1498 arg += 1;
1499 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001500 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001501 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001502 return -1;
1503 }
1504 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001505 }
Willy Tarreau01732802007-11-01 22:48:15 +01001506 else if (!strcmp(args[0], "url_param")) {
1507 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001508 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001509 return -1;
1510 }
Willy Tarreau31682232007-11-29 15:38:04 +01001511 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1512 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001513
1514 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001515 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001516 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001517 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001518 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001519 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001520 return -1;
1521 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001522 }
Benoitaffb4812009-03-25 13:02:10 +01001523 }
1524 else if (!strncmp(args[0], "hdr(", 4)) {
1525 const char *beg, *end;
1526
1527 beg = args[0] + 4;
1528 end = strchr(beg, ')');
1529
1530 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001531 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001532 return -1;
1533 }
1534
1535 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1536 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1537
1538 free(curproxy->hh_name);
1539 curproxy->hh_len = end - beg;
1540 curproxy->hh_name = my_strndup(beg, end - beg);
1541 curproxy->hh_match_domain = 0;
1542
1543 if (*args[1]) {
1544 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001545 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001546 return -1;
1547 }
1548 curproxy->hh_match_domain = 1;
1549 }
Emeric Brun736aa232009-06-30 17:56:00 +02001550 }
1551 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1552 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1553 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1554
1555 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1556 const char *beg, *end;
1557
1558 beg = args[0] + 11;
1559 end = strchr(beg, ')');
1560
1561 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001562 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001563 return -1;
1564 }
1565
1566 free(curproxy->hh_name);
1567 curproxy->hh_name = my_strndup(beg, end - beg);
1568 curproxy->hh_len = end - beg;
1569 }
1570 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1571 free(curproxy->hh_name);
1572 curproxy->hh_name = strdup("mstshash");
1573 curproxy->hh_len = strlen(curproxy->hh_name);
1574 }
1575 else { /* syntax */
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 }
Willy Tarreau01732802007-11-01 22:48:15 +01001579 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001580 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001581 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 +01001582 return -1;
1583 }
1584 return 0;
1585}
1586
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001587
1588/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001589/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001590/************************************************************************/
1591
Willy Tarreau34db1082012-04-19 17:16:54 +02001592/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001593 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001594 * undefined behaviour.
1595 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001596static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001597smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001598{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001599 struct proxy *px;
1600
Willy Tarreau37406352012-04-23 16:16:37 +02001601 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001602 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001603 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001604
1605 if (px->srv_act)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001606 smp->data.u.sint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001607 else if (px->lbprm.fbck)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001608 smp->data.u.sint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001609 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001610 smp->data.u.sint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001611
1612 return 1;
1613}
1614
Willy Tarreau37406352012-04-23 16:16:37 +02001615/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001616 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001617 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1618 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001619 */
1620static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001621smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001622{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001623 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001624
Willy Tarreau37406352012-04-23 16:16:37 +02001625 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001626 smp->data.type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001627 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001628 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001629 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001630 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001631 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001632 return 1;
1633}
1634
Willy Tarreau34db1082012-04-19 17:16:54 +02001635/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001636 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001637 * undefined behaviour.
1638 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001639static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001640smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001641{
1642 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001643
Willy Tarreau37406352012-04-23 16:16:37 +02001644 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001645 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001646 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001647
Willy Tarreau24e32d82012-04-23 23:55:44 +02001648 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001649 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001650 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001651
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001652 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001653 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001654 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001655 return 1;
1656 }
1657
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001658 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001659 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001660 }
1661
1662 return 1;
1663}
1664
Willy Tarreaua5e37562011-12-16 17:06:15 +01001665/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001666static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001667smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001668{
Willy Tarreaube508f12016-03-10 11:47:01 +01001669 if (!smp->strm)
1670 return 0;
1671
Willy Tarreauf853c462012-04-23 18:53:56 +02001672 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001673 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001674 smp->data.u.sint = smp->strm->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001675 return 1;
1676}
1677
Willy Tarreaua5e37562011-12-16 17:06:15 +01001678/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001679static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001680smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001681{
Willy Tarreaube508f12016-03-10 11:47:01 +01001682 if (!smp->strm)
1683 return 0;
1684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001685 if (!objt_server(smp->strm->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001686 return 0;
1687
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001688 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001689 smp->data.u.sint = objt_server(smp->strm->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001690
1691 return 1;
1692}
1693
Willy Tarreau34db1082012-04-19 17:16:54 +02001694/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001695 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001696 * undefined behaviour.
1697 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001698static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001699smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001700{
Willy Tarreau37406352012-04-23 16:16:37 +02001701 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001702 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001703 smp->data.u.sint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001704 return 1;
1705}
1706
Willy Tarreau34db1082012-04-19 17:16:54 +02001707/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001708 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001709 * undefined behaviour.
1710 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001712smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001713{
Willy Tarreau37406352012-04-23 16:16:37 +02001714 smp->flags = SMP_F_VOL_TEST;
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 = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001717 return 1;
1718}
1719
Willy Tarreau34db1082012-04-19 17:16:54 +02001720/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001721 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001722 * undefined behaviour.
1723 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001724static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001725smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001726{
Willy Tarreau37406352012-04-23 16:16:37 +02001727 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001728 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001729 smp->data.u.sint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001730 return 1;
1731}
1732
Willy Tarreaua5e37562011-12-16 17:06:15 +01001733/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001734 * by the number of running servers and rounded up. If there is no running
1735 * server, we return twice the total, just as if we had half a running server.
1736 * This is more or less correct anyway, since we expect the last server to come
1737 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001738 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001739 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001740 */
1741static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001742smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001743{
1744 int nbsrv;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001745 struct proxy *px;
Willy Tarreaua36af912009-10-10 12:02:45 +02001746
Willy Tarreau37406352012-04-23 16:16:37 +02001747 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001748 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001749 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001750
1751 if (px->srv_act)
1752 nbsrv = px->srv_act;
1753 else if (px->lbprm.fbck)
1754 nbsrv = 1;
1755 else
1756 nbsrv = px->srv_bck;
1757
1758 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001759 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001760 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001761 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001762
1763 return 1;
1764}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001765
Willy Tarreau34db1082012-04-19 17:16:54 +02001766/* set temp integer to the number of concurrent connections on the server in the backend.
1767 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1768 * undefined behaviour.
1769 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001770static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001771smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001772{
Willy Tarreauf853c462012-04-23 18:53:56 +02001773 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001774 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001775 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001776 return 1;
1777}
1778
Tait Clarridge7896d522012-12-05 21:39:31 -05001779/* set temp integer to the number of enabled servers on the proxy.
1780 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1781 * undefined behaviour.
1782 */
1783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001784smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05001785{
1786 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001787 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001788 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05001789 return 1;
1790}
1791
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001792
1793/* Note: must not be declared <const> as its list will be overwritten.
1794 * Please take care of keeping this list alphabetically sorted.
1795 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001796static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001797 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1798 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1799 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
1800 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1801 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1802 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1803 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1804 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1805 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001806 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001807 { "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 +01001808 { /* END */ },
1809}};
1810
1811
Willy Tarreau61612d42012-04-19 18:42:05 +02001812/* Note: must not be declared <const> as its list will be overwritten.
1813 * Please take care of keeping this list alphabetically sorted.
1814 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001815static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001816 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001817}};
1818
1819
1820__attribute__((constructor))
1821static void __backend_init(void)
1822{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001823 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001824 acl_register_keywords(&acl_kws);
1825}
1826
Willy Tarreaubaaee002006-06-26 02:48:02 +02001827/*
1828 * Local variables:
1829 * c-indent-level: 8
1830 * c-basic-offset: 8
1831 * End:
1832 */