blob: e0e53ffa04a08133d854161fbd64e067d8720d42 [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++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400118 srv->cumulative_weight = px->lbprm.tot_wbck;
Willy Tarreaub625a082007-11-26 01:15:43 +0100119 px->lbprm.tot_wbck += srv->eweight;
120 } else {
121 px->srv_act++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400122 srv->cumulative_weight = px->lbprm.tot_wact;
Willy Tarreaub625a082007-11-26 01:15:43 +0100123 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 }
125 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100126}
Willy Tarreau20697042007-11-15 23:26:18 +0100127
Willy Tarreaub625a082007-11-26 01:15:43 +0100128/* This function simply updates the backend's tot_weight and tot_used values
129 * after servers weights have been updated. It is designed to be used after
130 * recount_servers() or equivalent.
131 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200132void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100133{
Willy Tarreau20697042007-11-15 23:26:18 +0100134 if (px->srv_act) {
135 px->lbprm.tot_weight = px->lbprm.tot_wact;
136 px->lbprm.tot_used = px->srv_act;
137 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100138 else if (px->lbprm.fbck) {
139 /* use only the first backup server */
140 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
141 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100142 }
143 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100144 px->lbprm.tot_weight = px->lbprm.tot_wbck;
145 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100146 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100147}
148
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200149/*
150 * This function tries to find a running server for the proxy <px> following
151 * the source hash method. Depending on the number of active/backup servers,
152 * it will either look for active servers, or for backup servers.
153 * If any server is found, it will be returned. If no valid server is found,
154 * NULL is returned.
155 */
156struct server *get_server_sh(struct proxy *px, const char *addr, int len)
157{
158 unsigned int h, l;
159
160 if (px->lbprm.tot_weight == 0)
161 return NULL;
162
163 l = h = 0;
164
165 /* note: we won't hash if there's only one server left */
166 if (px->lbprm.tot_used == 1)
167 goto hash_done;
168
169 while ((l + sizeof (int)) <= len) {
170 h ^= ntohl(*(unsigned int *)(&addr[l]));
171 l += sizeof (int);
172 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500173 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100174 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200175 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200176 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
177 return chash_get_server_hash(px, h);
178 else
179 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200180}
181
182/*
183 * This function tries to find a running server for the proxy <px> following
184 * the URI hash method. In order to optimize cache hits, the hash computation
185 * ends at the question mark. Depending on the number of active/backup servers,
186 * it will either look for active servers, or for backup servers.
187 * If any server is found, it will be returned. If no valid server is found,
188 * NULL is returned.
189 *
190 * This code was contributed by Guillaume Dallaire, who also selected this hash
191 * algorithm out of a tens because it gave him the best results.
192 *
193 */
194struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
195{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700196 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200197 int c;
198 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400199 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200200
201 if (px->lbprm.tot_weight == 0)
202 return NULL;
203
204 /* note: we won't hash if there's only one server left */
205 if (px->lbprm.tot_used == 1)
206 goto hash_done;
207
208 if (px->uri_len_limit)
209 uri_len = MIN(uri_len, px->uri_len_limit);
210
Bhaskar98634f02013-10-29 23:30:51 -0400211 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200212 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200213 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200214 if (c == '/') {
215 slashes++;
216 if (slashes == px->uri_dirs_depth1) /* depth+1 */
217 break;
218 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100219 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200220 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200221 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200222 }
Bhaskar98634f02013-10-29 23:30:51 -0400223
224 hash = gen_hash(px, start, (end - start));
225
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500226 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100227 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200228 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200229 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
230 return chash_get_server_hash(px, hash);
231 else
232 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200233}
234
Willy Tarreau01732802007-11-01 22:48:15 +0100235/*
236 * This function tries to find a running server for the proxy <px> following
237 * the URL parameter hash method. It looks for a specific parameter in the
238 * URL and hashes it to compute the server ID. This is useful to optimize
239 * performance by avoiding bounces between servers in contexts where sessions
240 * are shared but cookies are not usable. If the parameter is not found, NULL
241 * is returned. If any server is found, it will be returned. If no valid server
242 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100243 */
244struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
245{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700246 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400247 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200248 const char *p;
249 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100250 int plen;
251
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200252 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100253 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100254 return NULL;
255
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200256 if ((p = memchr(uri, '?', uri_len)) == NULL)
257 return NULL;
258
Willy Tarreau01732802007-11-01 22:48:15 +0100259 p++;
260
261 uri_len -= (p - uri);
262 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100264
265 while (uri_len > plen) {
266 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267 if (params[plen] == '=') {
268 if (memcmp(params, px->url_param_name, plen) == 0) {
269 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100270 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100272 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200273 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400274 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200275 uri_len -= plen + 1;
276
Bhaskar98634f02013-10-29 23:30:51 -0400277 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100278 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400279 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100280 }
Bhaskar98634f02013-10-29 23:30:51 -0400281 hash = gen_hash(px, start, (end - start));
282
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500283 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100284 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400285
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200286 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
287 return chash_get_server_hash(px, hash);
288 else
289 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100290 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200291 }
292 /* skip to next parameter */
293 p = memchr(params, '&', uri_len);
294 if (!p)
295 return NULL;
296 p++;
297 uri_len -= (p - params);
298 params = p;
299 }
300 return NULL;
301}
302
303/*
304 * this does the same as the previous server_ph, but check the body contents
305 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200306struct server *get_server_ph_post(struct stream *s)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700308 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200309 struct http_txn *txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100310 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 struct http_msg *msg = &txn->req;
312 struct proxy *px = s->be;
313 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200314 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200315 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100316 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400317 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200318
Willy Tarreau157dd632009-12-06 19:18:09 +0100319 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200320 return NULL;
321
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200322 if (len > req->buf->data + req->buf->size - p)
323 len = req->buf->data + req->buf->size - p;
324
Willy Tarreau157dd632009-12-06 19:18:09 +0100325 if (px->lbprm.tot_weight == 0)
326 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327
328 while (len > plen) {
329 /* Look for the parameter name followed by an equal symbol */
330 if (params[plen] == '=') {
331 if (memcmp(params, px->url_param_name, plen) == 0) {
332 /* OK, we have the parameter here at <params>, and
333 * the value after the equal sign, at <p>
334 * skip the equal symbol
335 */
336 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400337 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200338 len -= plen + 1;
339
Bhaskar98634f02013-10-29 23:30:51 -0400340 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200341 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100342 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400343 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100344 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200345 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
346 break;
347 return NULL; /* oh, no; this is not uri-encoded.
348 * This body does not contain parameters.
349 */
350 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200351 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400352 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200353 /* should we break if vlen exceeds limit? */
354 }
Bhaskar98634f02013-10-29 23:30:51 -0400355 hash = gen_hash(px, start, (end - start));
356
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500357 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100358 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400359
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200360 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
361 return chash_get_server_hash(px, hash);
362 else
363 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200364 }
365 }
Willy Tarreau01732802007-11-01 22:48:15 +0100366 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200367 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100368 if (!p)
369 return NULL;
370 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200371 len -= (p - params);
372 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100373 }
374 return NULL;
375}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200377
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378/*
Benoitaffb4812009-03-25 13:02:10 +0100379 * This function tries to find a running server for the proxy <px> following
380 * the Header parameter hash method. It looks for a specific parameter in the
381 * URL and hashes it to compute the server ID. This is useful to optimize
382 * performance by avoiding bounces between servers in contexts where sessions
383 * are shared but cookies are not usable. If the parameter is not found, NULL
384 * is returned. If any server is found, it will be returned. If no valid server
385 * is found, NULL is returned.
386 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200387struct server *get_server_hh(struct stream *s)
Benoitaffb4812009-03-25 13:02:10 +0100388{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700389 unsigned int hash = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200390 struct http_txn *txn = s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100391 struct proxy *px = s->be;
392 unsigned int plen = px->hh_len;
393 unsigned long len;
394 struct hdr_ctx ctx;
395 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400396 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100397
398 /* tot_weight appears to mean srv_count */
399 if (px->lbprm.tot_weight == 0)
400 return NULL;
401
Benoitaffb4812009-03-25 13:02:10 +0100402 ctx.idx = 0;
403
404 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100405 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 +0100406
407 /* if the header is not found or empty, let's fallback to round robin */
408 if (!ctx.idx || !ctx.vlen)
409 return NULL;
410
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200411 /* note: we won't hash if there's only one server left */
412 if (px->lbprm.tot_used == 1)
413 goto hash_done;
414
Benoitaffb4812009-03-25 13:02:10 +0100415 /* Found a the hh_name in the headers.
416 * we will compute the hash based on this value ctx.val.
417 */
418 len = ctx.vlen;
419 p = (char *)ctx.line + ctx.val;
420 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400421 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100422 } else {
423 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100424 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100425 /* special computation, use only main domain name, not tld/host
426 * going back from the end of string, start hashing at first
427 * dot stop at next.
428 * This is designed to work with the 'Host' header, and requires
429 * a special option to activate this.
430 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100431 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100432 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100433 if (dohash) {
434 /* Rewind the pointer until the previous char
435 * is a dot, this will allow to set the start
436 * position of the domain. */
437 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100438 break;
Benoitaffb4812009-03-25 13:02:10 +0100439 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100440 else if (*p == '.') {
441 /* The pointer is rewinded to the dot before the
442 * tld, we memorize the end of the domain and
443 * can enter the domain processing. */
444 end = p;
445 dohash = 1;
446 }
Benoitaffb4812009-03-25 13:02:10 +0100447 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100448 len--;
Benoitaffb4812009-03-25 13:02:10 +0100449 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100450 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400451 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100452 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500453 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100454 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200455 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200456 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
457 return chash_get_server_hash(px, hash);
458 else
459 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100460}
461
Willy Tarreau34db1082012-04-19 17:16:54 +0200462/* RDP Cookie HASH. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200463struct server *get_server_rch(struct stream *s)
Emeric Brun736aa232009-06-30 17:56:00 +0200464{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700465 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200466 struct proxy *px = s->be;
467 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200468 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200469 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200470 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200471
472 /* tot_weight appears to mean srv_count */
473 if (px->lbprm.tot_weight == 0)
474 return NULL;
475
Willy Tarreau37406352012-04-23 16:16:37 +0200476 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200477
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100478 b_rew(s->req.buf, rewind = s->req.buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200479
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200480 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200481 len = smp.data.u.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100483 b_adv(s->req.buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200484
Willy Tarreau37406352012-04-23 16:16:37 +0200485 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200486 return NULL;
487
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200488 /* note: we won't hash if there's only one server left */
489 if (px->lbprm.tot_used == 1)
490 goto hash_done;
491
Emeric Brun736aa232009-06-30 17:56:00 +0200492 /* Found a the hh_name in the headers.
493 * we will compute the hash based on this value ctx.val.
494 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200495 hash = gen_hash(px, smp.data.u.str.str, len);
Bhaskar98634f02013-10-29 23:30:51 -0400496
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500497 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100498 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200499 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200500 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
501 return chash_get_server_hash(px, hash);
502 else
503 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200504}
Benoitaffb4812009-03-25 13:02:10 +0100505
506/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200507 * This function applies the load-balancing algorithm to the stream, as
508 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200509 * 'assigned'.
510 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200511 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200512 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100513 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 * The function tries to keep the original connection slot if it reconnects to
515 * the same server, otherwise it releases it and tries to offer it.
516 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200517 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518 *
519 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100520 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200521 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
522 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 * SRV_STATUS_INTERNAL for other unrecoverable errors.
524 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200525 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100526 * it does not need to be called anymore. This means that target_srv(&s->target)
527 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528 *
529 */
530
Willy Tarreau87b09662015-04-03 00:22:06 +0200531int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200533 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100535 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200536 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100537
Simon Horman8effd3d2011-08-13 08:03:52 +0900538 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539
Willy Tarreau7c669d72008-06-20 15:04:11 +0200540 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200541 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200542 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100543
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100544 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200545 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546
Willy Tarreau7c669d72008-06-20 15:04:11 +0200547 /* We have to release any connection slot before applying any LB algo,
548 * otherwise we may erroneously end up with no available slot.
549 */
550 if (conn_slot)
551 sess_change_server(s, NULL);
552
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100553 /* We will now try to find the good server and store it into <objt_server(s->target)>.
554 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200555 * as well as if no server is available (check error code).
556 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100557
Willy Tarreau827aee92011-03-10 16:55:02 +0100558 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100559 s->target = NULL;
Willy Tarreau350f4872014-11-28 14:42:25 +0100560 conn = objt_conn(s->si[1].end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100561
Willy Tarreau068621e2013-12-23 15:11:25 +0100562 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100563 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100564 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreaueee5b512015-04-03 23:46:31 +0200565 ((s->txn && s->txn->flags & TX_PREFER_LAST) ||
Willy Tarreauc35362a2014-04-25 13:58:37 +0200566 ((s->be->options & PR_O_PREF_LAST) &&
567 (!s->be->max_ka_queue ||
568 server_has_room(__objt_server(conn->target)) ||
569 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200570 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200571 /* This stream was relying on a server in a previous request
Godbachf2dd68d2014-12-10 10:21:30 +0800572 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100573 * let's try to reuse the same server.
574 */
575 srv = __objt_server(conn->target);
576 s->target = &srv->obj_type;
577 }
578 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200579 /* we must check if we have at least one server available */
580 if (!s->be->lbprm.tot_weight) {
581 err = SRV_STATUS_NOSRV;
582 goto out;
583 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585 /* First check whether we need to fetch some data or simply call
586 * the LB lookup function. Only the hashing functions will need
587 * some input data in fact, and will support multiple algorithms.
588 */
589 switch (s->be->lbprm.algo & BE_LB_LKUP) {
590 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100591 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200592 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593
Willy Tarreauf09c6602012-02-13 17:12:08 +0100594 case BE_LB_LKUP_FSTREE:
595 srv = fas_get_next_server(s->be, prev_srv);
596 break;
597
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200598 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100599 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200600 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200602 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200603 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200604 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200607 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200609 break;
610 }
611 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200612 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200613 err = SRV_STATUS_INTERNAL;
614 goto out;
615 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
617 switch (s->be->lbprm.algo & BE_LB_PARM) {
618 case BE_LB_HASH_SRC:
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200619 conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200620 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200621 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200622 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200623 4);
624 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200625 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200626 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200627 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200628 16);
629 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200630 else {
631 /* unknown IP family */
632 err = SRV_STATUS_INTERNAL;
633 goto out;
634 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200635 break;
636
637 case BE_LB_HASH_URI:
638 /* URI hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200639 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100640 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 srv = get_server_uh(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200642 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
643 s->txn->req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200644 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200645
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200646 case BE_LB_HASH_PRM:
647 /* URL Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200648 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100649 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100650
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 srv = get_server_ph(s->be,
Willy Tarreaueee5b512015-04-03 23:46:31 +0200652 b_ptr(s->req.buf, -http_uri_rewind(&s->txn->req)),
653 s->txn->req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100654
Willy Tarreaueee5b512015-04-03 23:46:31 +0200655 if (!srv && s->txn->meth == HTTP_METH_POST)
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200657 break;
Benoitaffb4812009-03-25 13:02:10 +0100658
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200659 case BE_LB_HASH_HDR:
660 /* Header Parameter hashing */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200661 if (!s->txn || s->txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100662 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100663 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200664 break;
665
666 case BE_LB_HASH_RDP:
667 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100668 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200669 break;
670
671 default:
672 /* unknown balancing algorithm */
673 err = SRV_STATUS_INTERNAL;
674 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100675 }
Emeric Brun736aa232009-06-30 17:56:00 +0200676
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200677 /* If the hashing parameter was not found, let's fall
678 * back to round robin on the map.
679 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100680 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200681 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100682 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200683 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100684 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200685 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200686
687 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200688 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200689
Willy Tarreau7c669d72008-06-20 15:04:11 +0200690 default:
691 /* unknown balancing algorithm */
692 err = SRV_STATUS_INTERNAL;
693 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200695
Willy Tarreau827aee92011-03-10 16:55:02 +0100696 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200697 err = SRV_STATUS_FULL;
698 goto out;
699 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100700 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100701 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100702 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100703 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100704 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200705 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200706 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100707 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100709 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau350f4872014-11-28 14:42:25 +0100710 (conn = objt_conn(s->si[1].end)) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200711 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100712 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100713 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100714 }
715 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200716 err = SRV_STATUS_NOSRV;
717 goto out;
718 }
719
Willy Tarreaue7dff022015-04-03 01:14:29 +0200720 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200721 err = SRV_STATUS_OK;
722 out:
723
724 /* Either we take back our connection slot, or we offer it to someone
725 * else if we don't need it anymore.
726 */
727 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100728 if (conn_slot == srv) {
729 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200730 } else {
731 if (may_dequeue_tasks(conn_slot, s->be))
732 process_srv_queue(conn_slot);
733 }
734 }
735
736 out_err:
737 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738}
739
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740/*
Willy Tarreaue7dff022015-04-03 01:14:29 +0200741 * This function assigns a server address to a stream, and sets SF_ADDR_SET.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742 * The address is taken from the currently assigned server, or from the
743 * dispatch or transparent address.
744 *
745 * It may return :
746 * SRV_STATUS_OK if everything is OK.
747 * SRV_STATUS_INTERNAL for other unrecoverable errors.
748 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200749 * Upon successful return, the stream flag SF_ADDR_SET is set. This flag is
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750 * not cleared, so it's to the caller to clear it if required.
751 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200752 * The caller is responsible for having already assigned a connection
753 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200754 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200756int assign_server_address(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200758 struct connection *cli_conn = objt_conn(strm_orig(s));
Willy Tarreau350f4872014-11-28 14:42:25 +0100759 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200760
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761#ifdef DEBUG_FULL
762 fprintf(stderr,"assign_server_address : s=%p\n",s);
763#endif
764
Willy Tarreaue7dff022015-04-03 01:14:29 +0200765 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200766 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200767 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 return SRV_STATUS_INTERNAL;
769
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200770 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200772 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200773 /* if the server has no address, we use the same address
774 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200775 * locally on multiple addresses at once. Nothing is done
776 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200777 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200778 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200779
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 if (cli_conn->addr.to.ss_family == AF_INET) {
781 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
782 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
783 ((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 +0200784 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200785 }
786
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 /* if this server remaps proxied ports, we'll use
788 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200789 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100790 int base_port;
791
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200792 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100793
794 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200795 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100796
797 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200798 base_port += get_host_port(&srv_conn->addr.to);
799 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800 }
801 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200802 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200804 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200806 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200808 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200810 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
811 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100813 else if (s->be->options & PR_O_HTTP_PROXY) {
814 /* If HTTP PROXY option is set, then server is already assigned
815 * during incoming client request parsing. */
816 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100817 else {
818 /* no server and no LB algorithm ! */
819 return SRV_STATUS_INTERNAL;
820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100822 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100823 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100824
Willy Tarreaue7dff022015-04-03 01:14:29 +0200825 s->flags |= SF_ADDR_SET;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 return SRV_STATUS_OK;
827}
828
Willy Tarreau87b09662015-04-03 00:22:06 +0200829/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200831 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200832 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200833 * be called before any connection and after any retry or redispatch occurs.
834 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200835 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 *
837 * Returns :
838 *
839 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100840 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 * SRV_STATUS_QUEUED if the connection has been queued.
842 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100843 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 * SRV_STATUS_INTERNAL for other unrecoverable errors.
846 *
847 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200848int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849{
850 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100851 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 int err;
853
854 if (s->pend_pos)
855 return SRV_STATUS_INTERNAL;
856
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100859 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100860
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100862 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200863 /* This stream was previously assigned to a server. We have to
864 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865 * - if the server changed :
866 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200867 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200868 * - increment srv->redispatches and be->redispatches
869 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100870 */
871
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100872 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200873 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
874 s->txn->flags &= ~TX_CK_MASK;
875 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200876 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200877 s->flags |= SF_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100878 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100879 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100881 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100882 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100883 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100884 }
885 }
886
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887 switch (err) {
888 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100890 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100891 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200892 return SRV_STATUS_OK; /* dispatch or proxy mode */
893
894 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200896 return SRV_STATUS_OK;
897
Willy Tarreau87b09662015-04-03 00:22:06 +0200898 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200899 * connection slot yet. Either it is a redispatch, or it was
900 * assigned from persistence information (direct mode).
901 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200902 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200903 /* server scheduled for redirection, and already assigned. We
904 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100905 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100906 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100907 return SRV_STATUS_OK;
908 }
909
Willy Tarreau87b09662015-04-03 00:22:06 +0200910 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200911 * We know we have to queue it into the server's queue, so if a maxqueue
912 * is set on the server, we must also check that the server's queue is
913 * not full, in which case we have to return FULL.
914 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100915 if (srv->maxconn &&
916 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200917
Willy Tarreau827aee92011-03-10 16:55:02 +0100918 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919 return SRV_STATUS_FULL;
920
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 p = pendconn_add(s);
922 if (p)
923 return SRV_STATUS_QUEUED;
924 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200925 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200927
928 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100929 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 return SRV_STATUS_OK;
931
932 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +0200933 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 p = pendconn_add(s);
935 if (p)
936 return SRV_STATUS_QUEUED;
937 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200938 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939
940 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200941 return err;
942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 case SRV_STATUS_INTERNAL:
944 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946 default:
947 return SRV_STATUS_INTERNAL;
948 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100949}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950
Willy Tarreaub1d67742010-03-29 19:36:59 +0200951/* If an explicit source binding is specified on the server and/or backend, and
952 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau87b09662015-04-03 00:22:06 +0200953 * assigned to the stream's pending connection. This function assumes that an
Willy Tarreau350f4872014-11-28 14:42:25 +0100954 * outgoing connection has already been assigned to s->si[1].end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200955 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200956static void assign_tproxy_address(struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +0200957{
Willy Tarreau29fbe512015-08-20 19:35:14 +0200958#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100959 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100960 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200961 struct connection *cli_conn;
Willy Tarreau350f4872014-11-28 14:42:25 +0100962 struct connection *srv_conn = objt_conn(s->si[1].end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100963
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100964 if (srv && srv->conn_src.opts & CO_SRC_BIND)
965 src = &srv->conn_src;
966 else if (s->be->conn_src.opts & CO_SRC_BIND)
967 src = &s->be->conn_src;
968 else
969 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100970
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100971 switch (src->opts & CO_SRC_TPROXY_MASK) {
972 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200973 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100974 break;
975 case CO_SRC_TPROXY_CLI:
976 case CO_SRC_TPROXY_CIP:
977 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200978 cli_conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200979 if (cli_conn)
980 srv_conn->addr.from = cli_conn->addr.from;
981 else
982 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100983 break;
984 case CO_SRC_TPROXY_DYN:
Willy Tarreaueee5b512015-04-03 23:46:31 +0200985 if (src->bind_hdr_occ && s->txn) {
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100986 char *vptr;
987 int vlen;
988 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100989
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100990 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200991 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
992 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
993 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100994
Willy Tarreaueee5b512015-04-03 23:46:31 +0200995 b_rew(s->req.buf, rewind = http_hdr_rewind(&s->txn->req));
996 if (http_get_hdr(&s->txn->req, src->bind_hdr_name, src->bind_hdr_len,
997 &s->txn->hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200998 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100999 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +02001000 }
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001001 b_adv(s->req.buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001002 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001003 break;
1004 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001006 }
1007#endif
1008}
1009
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001012 * This function initiates a connection to the server assigned to this stream
Willy Tarreau350f4872014-11-28 14:42:25 +01001013 * (s->target, s->si[1].addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001014 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001016 * - SF_ERR_NONE if everything's OK
1017 * - SF_ERR_SRVTO if there are no more servers
1018 * - SF_ERR_SRVCL if the connection was refused by the server
1019 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1020 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1021 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001022 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001023 * The server-facing stream interface is expected to hold a pre-allocated connection
Willy Tarreau350f4872014-11-28 14:42:25 +01001024 * in s->si[1].conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001026int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001028 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001029 struct connection *srv_conn;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001030 struct connection *old_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001031 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001032 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001033 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034
Willy Tarreau7b004922015-08-04 19:34:21 +02001035 srv = objt_server(s->target);
Willy Tarreau350f4872014-11-28 14:42:25 +01001036 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau34601a82013-12-15 16:33:46 +01001037 if (srv_conn)
1038 reuse = s->target == srv_conn->target;
1039
Willy Tarreau8dff9982015-08-04 20:45:52 +02001040 if (srv && !reuse) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001041 old_conn = srv_conn;
1042 if (old_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001043 srv_conn = NULL;
Willy Tarreauefb90f92015-08-06 11:37:10 +02001044 old_conn->owner = NULL;
1045 si_detach_endpoint(&s->si[1]);
1046 /* note: if the connection was in a server's idle
1047 * queue, it doesn't get dequeued.
1048 */
Willy Tarreau8dff9982015-08-04 20:45:52 +02001049 }
1050
Willy Tarreau449d74a2015-08-05 17:16:33 +02001051 /* Below we pick connections from the safe or idle lists based
1052 * on the strategy, the fact that this is a first or second
1053 * (retryable) request, with the indicated priority (1 or 2) :
1054 *
1055 * SAFE AGGR ALWS
1056 *
1057 * +-----+-----+ +-----+-----+ +-----+-----+
1058 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1059 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1060 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1061 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1062 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1063 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1064 */
1065
1066 if (!LIST_ISEMPTY(&srv->idle_conns) &&
1067 ((s->be->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR &&
1068 s->txn && (s->txn->flags & TX_NOT_FIRST))) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001069 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
Willy Tarreau449d74a2015-08-05 17:16:33 +02001070 }
1071 else if (!LIST_ISEMPTY(&srv->safe_conns) &&
1072 ((s->txn && (s->txn->flags & TX_NOT_FIRST)) ||
1073 (s->be->options & PR_O_REUSE_MASK) >= PR_O_REUSE_AGGR)) {
1074 srv_conn = LIST_ELEM(srv->safe_conns.n, struct connection *, list);
1075 }
1076 else if (!LIST_ISEMPTY(&srv->idle_conns) &&
1077 (s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) {
1078 srv_conn = LIST_ELEM(srv->idle_conns.n, struct connection *, list);
1079 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001080
Willy Tarreau449d74a2015-08-05 17:16:33 +02001081 /* If we've picked a connection from the pool, we now have to
1082 * detach it. We may have to get rid of the previous idle
1083 * connection we had, so for this we try to swap it with the
1084 * other owner's. That way it may remain alive for others to
1085 * pick.
1086 */
1087 if (srv_conn) {
Willy Tarreau8dff9982015-08-04 20:45:52 +02001088 LIST_DEL(&srv_conn->list);
1089 LIST_INIT(&srv_conn->list);
1090
Willy Tarreauefb90f92015-08-06 11:37:10 +02001091 if (srv_conn->owner) {
1092 si_detach_endpoint(srv_conn->owner);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001093 if (old_conn && !(old_conn->flags & CO_FL_PRIVATE)) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001094 si_attach_conn(srv_conn->owner, old_conn);
Willy Tarreau0aae4802016-02-02 18:29:05 +01001095 si_idle_conn(srv_conn->owner, NULL);
1096 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001097 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001098 si_attach_conn(&s->si[1], srv_conn);
1099 reuse = 1;
1100 }
Willy Tarreauefb90f92015-08-06 11:37:10 +02001101
Willy Tarreau449d74a2015-08-05 17:16:33 +02001102 /* we may have to release our connection if we couldn't swap it */
Willy Tarreauefb90f92015-08-06 11:37:10 +02001103 if (old_conn && !old_conn->owner) {
Willy Tarreauefb90f92015-08-06 11:37:10 +02001104 LIST_DEL(&old_conn->list);
1105 conn_force_close(old_conn);
1106 conn_free(old_conn);
1107 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001108 }
1109
Willy Tarreau34601a82013-12-15 16:33:46 +01001110 if (reuse) {
1111 /* Disable connection reuse if a dynamic source is used.
1112 * As long as we don't share connections between servers,
1113 * we don't need to disable connection reuse on no-idempotent
1114 * requests nor when PROXY protocol is used.
1115 */
Willy Tarreau34601a82013-12-15 16:33:46 +01001116 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1117 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1118 reuse = 0;
1119 }
1120 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1121 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1122 reuse = 0;
1123 }
1124 }
1125
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001126 if (!reuse)
Willy Tarreau973a5422015-08-05 21:47:23 +02001127 srv_conn = si_alloc_conn(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02001128 else {
1129 /* reusing our connection, take it out of the idle list */
1130 LIST_DEL(&srv_conn->list);
1131 LIST_INIT(&srv_conn->list);
1132 }
Willy Tarreauc12b5e62015-08-04 19:45:36 +02001133
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001134 if (!srv_conn)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001135 return SF_ERR_RESOURCE;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001136
Willy Tarreaue7dff022015-04-03 01:14:29 +02001137 if (!(s->flags & SF_ADDR_SET)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001138 err = assign_server_address(s);
1139 if (err != SRV_STATUS_OK)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001140 return SF_ERR_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 }
1142
Willy Tarreauff605db2013-12-20 11:09:51 +01001143 if (!conn_xprt_ready(srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001144 /* the target was only on the stream, assign it to the SI now */
Willy Tarreauff605db2013-12-20 11:09:51 +01001145 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001146
Willy Tarreauff605db2013-12-20 11:09:51 +01001147 /* set the correct protocol on the output stream interface */
Willy Tarreau7b004922015-08-04 19:34:21 +02001148 if (srv) {
1149 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), srv->xprt);
Willy Tarreauff605db2013-12-20 11:09:51 +01001150 }
1151 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1152 /* proxies exclusively run on raw_sock right now */
1153 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
Willy Tarreau350f4872014-11-28 14:42:25 +01001154 if (!objt_conn(s->si[1].end) || !objt_conn(s->si[1].end)->ctrl)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001155 return SF_ERR_INTERNAL;
Willy Tarreauff605db2013-12-20 11:09:51 +01001156 }
1157 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 return SF_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001159
Willy Tarreauff605db2013-12-20 11:09:51 +01001160 /* process the case where the server requires the PROXY protocol to be sent */
1161 srv_conn->send_proxy_ofs = 0;
Willy Tarreau7b004922015-08-04 19:34:21 +02001162 if (srv && srv->pp_opts) {
Willy Tarreau387ebf82015-08-04 19:24:13 +02001163 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauff605db2013-12-20 11:09:51 +01001164 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001165 cli_conn = objt_conn(strm_orig(s));
Willy Tarreauff605db2013-12-20 11:09:51 +01001166 if (cli_conn)
1167 conn_get_to_addr(cli_conn);
1168 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001169
Willy Tarreau350f4872014-11-28 14:42:25 +01001170 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001171
Willy Tarreauff605db2013-12-20 11:09:51 +01001172 assign_tproxy_address(s);
1173 }
1174 else {
1175 /* the connection is being reused, just re-attach it */
Willy Tarreau350f4872014-11-28 14:42:25 +01001176 si_attach_conn(&s->si[1], srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 s->flags |= SF_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001178 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001179
William Lallemandb7ff6a32012-03-02 14:35:21 +01001180 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001181 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Willy Tarreau350f4872014-11-28 14:42:25 +01001182 s->si[1].flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001183
Willy Tarreau14f8e862012-08-30 22:23:13 +02001184 /* disable lingering */
1185 if (s->be->options & PR_O_TCP_NOLING)
Willy Tarreau350f4872014-11-28 14:42:25 +01001186 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001187
Willy Tarreau350f4872014-11-28 14:42:25 +01001188 err = si_connect(&s->si[1]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189
Willy Tarreaue7dff022015-04-03 01:14:29 +02001190 if (err != SF_ERR_NONE)
Willy Tarreau9650f372009-08-16 14:02:45 +02001191 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001192
Willy Tarreau14f8e862012-08-30 22:23:13 +02001193 /* set connect timeout */
Willy Tarreau350f4872014-11-28 14:42:25 +01001194 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001195
Willy Tarreau827aee92011-03-10 16:55:02 +01001196 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001197 s->flags |= SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001198 srv->cur_sess++;
1199 if (srv->cur_sess > srv->counters.cur_sess_max)
1200 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001201 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001202 s->be->lbprm.server_take_conn(srv);
Willy Tarreau732eac42015-07-09 11:40:25 +02001203
1204#ifdef USE_OPENSSL
1205 if (srv->ssl_ctx.sni) {
1206 struct sample *smp;
1207 int rewind;
1208
1209 /* Tricky case : we have already scheduled the pending
1210 * HTTP request or TCP data for leaving. So in HTTP we
1211 * rewind exactly the headers, otherwise we rewind the
1212 * output data.
1213 */
1214 rewind = s->txn ? http_hdr_rewind(&s->txn->req) : s->req.buf->o;
1215 b_rew(s->req.buf, rewind);
1216
1217 smp = sample_fetch_as_type(s->be, s->sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, srv->ssl_ctx.sni, SMP_T_STR);
1218
1219 /* restore the pointers */
1220 b_adv(s->req.buf, rewind);
1221
Willy Tarreau2e0565c2016-08-09 11:55:21 +02001222 if (smp_make_safe(smp)) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001223 ssl_sock_set_servername(srv_conn, smp->data.u.str.str);
Willy Tarreau387ebf82015-08-04 19:24:13 +02001224 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau732eac42015-07-09 11:40:25 +02001225 }
1226 }
1227#endif /* USE_OPENSSL */
1228
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 }
1230
Willy Tarreaue7dff022015-04-03 01:14:29 +02001231 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232}
1233
1234
Willy Tarreaubaaee002006-06-26 02:48:02 +02001235/* This function performs the "redispatch" part of a connection attempt. It
1236 * will assign a server if required, queue the connection if required, and
1237 * handle errors that might arise at this level. It can change the server
1238 * state. It will return 1 if it encounters an error, switches the server
1239 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1240 * that the connection is ready to use.
1241 */
1242
Willy Tarreau87b09662015-04-03 00:22:06 +02001243int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244{
Willy Tarreau827aee92011-03-10 16:55:02 +01001245 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246 int conn_err;
1247
1248 /* We know that we don't have any connection pending, so we will
1249 * try to get a new one, and wait in this state if it's queued
1250 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001251 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001252 conn_err = assign_server_and_queue(s);
1253 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001254
Willy Tarreaubaaee002006-06-26 02:48:02 +02001255 switch (conn_err) {
1256 case SRV_STATUS_OK:
1257 break;
1258
Willy Tarreau7c669d72008-06-20 15:04:11 +02001259 case SRV_STATUS_FULL:
1260 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1261 * and we can redispatch to another server, or it is not and we return
1262 * 503. This only makes sense in DIRECT mode however, because normal LB
1263 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001264 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001265 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001266 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001267 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001268 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001269 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001270 goto redispatch;
1271 }
1272
Willy Tarreau350f4872014-11-28 14:42:25 +01001273 if (!s->si[1].err_type) {
1274 s->si[1].err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001275 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001276
Willy Tarreau827aee92011-03-10 16:55:02 +01001277 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001278 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001279 return 1;
1280
Willy Tarreaubaaee002006-06-26 02:48:02 +02001281 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001282 /* note: it is guaranteed that srv == NULL here */
Willy Tarreau350f4872014-11-28 14:42:25 +01001283 if (!s->si[1].err_type) {
1284 s->si[1].err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001285 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001286
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001287 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001288 return 1;
1289
1290 case SRV_STATUS_QUEUED:
Willy Tarreau350f4872014-11-28 14:42:25 +01001291 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1292 s->si[1].state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001293 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001294 return 1;
1295
Willy Tarreaubaaee002006-06-26 02:48:02 +02001296 case SRV_STATUS_INTERNAL:
1297 default:
Willy Tarreau350f4872014-11-28 14:42:25 +01001298 if (!s->si[1].err_type) {
1299 s->si[1].err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001300 }
1301
Willy Tarreau827aee92011-03-10 16:55:02 +01001302 if (srv)
1303 srv_inc_sess_ctr(srv);
1304 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001305 srv_set_sess_last(srv);
1306 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001307 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001308 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309
Willy Tarreau87b09662015-04-03 00:22:06 +02001310 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001311 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001312 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 return 1;
1314 }
1315 /* if we get here, it's because we got SRV_STATUS_OK, which also
1316 * means that the connection has not been queued.
1317 */
1318 return 0;
1319}
1320
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001321/* sends a log message when a backend goes down, and also sets last
1322 * change date.
1323 */
1324void set_backend_down(struct proxy *be)
1325{
1326 be->last_change = now.tv_sec;
1327 be->down_trans++;
1328
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001329 if (!(global.mode & MODE_STARTING)) {
1330 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1331 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1332 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001333}
1334
Willy Tarreau87b09662015-04-03 00:22:06 +02001335/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001336 * tries to extract an RDP cookie from the request buffer, and look for the
1337 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02001338 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001339 * list. Nothing is performed if a server was already assigned.
1340 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001341int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001342{
1343 struct proxy *px = s->be;
1344 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001345 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001346 struct server *srv = px->srv;
1347 struct sockaddr_in addr;
1348 char *p;
1349
Willy Tarreau87b09662015-04-03 00:22:06 +02001350 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 +02001351 now_ms, __FUNCTION__,
1352 s,
1353 req,
1354 req->rex, req->wex,
1355 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001356 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001357 req->analysers);
1358
Willy Tarreaue7dff022015-04-03 01:14:29 +02001359 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001360 goto no_cookie;
1361
Willy Tarreau37406352012-04-23 16:16:37 +02001362 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001363
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001364 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001365 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001366 goto no_cookie;
1367
1368 memset(&addr, 0, sizeof(addr));
1369 addr.sin_family = AF_INET;
1370
Willy Tarreau664092c2011-12-16 19:11:42 +01001371 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001372 addr.sin_addr.s_addr = strtoul(smp.data.u.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001373 if (*p != '.')
1374 goto no_cookie;
1375 p++;
1376 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1377 if (*p != '.')
1378 goto no_cookie;
1379
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001380 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001381 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001382 if (srv->addr.ss_family == AF_INET &&
1383 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001384 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001385 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001386 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001387 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001388 break;
1389 }
1390 }
1391 srv = srv->next;
1392 }
1393
1394no_cookie:
1395 req->analysers &= ~an_bit;
1396 req->analyse_exp = TICK_ETERNITY;
1397 return 1;
1398}
1399
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001400int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001401 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001402 return px->down_time;
1403
1404 return now.tv_sec - px->last_change + px->down_time;
1405}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001407/*
1408 * This function returns a string containing the balancing
1409 * mode of the proxy in a format suitable for stats.
1410 */
1411
1412const char *backend_lb_algo_str(int algo) {
1413
1414 if (algo == BE_LB_ALGO_RR)
1415 return "roundrobin";
1416 else if (algo == BE_LB_ALGO_SRR)
1417 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001418 else if (algo == BE_LB_ALGO_FAS)
1419 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001420 else if (algo == BE_LB_ALGO_LC)
1421 return "leastconn";
1422 else if (algo == BE_LB_ALGO_SH)
1423 return "source";
1424 else if (algo == BE_LB_ALGO_UH)
1425 return "uri";
1426 else if (algo == BE_LB_ALGO_PH)
1427 return "url_param";
1428 else if (algo == BE_LB_ALGO_HH)
1429 return "hdr";
1430 else if (algo == BE_LB_ALGO_RCH)
1431 return "rdp-cookie";
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001432 else if (algo == BE_LB_ALGO_NONE)
1433 return "none";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001434 else
Willy Tarreaub3e111b2016-11-26 15:52:04 +01001435 return "unknown";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001436}
1437
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001438/* This function parses a "balance" statement in a backend section describing
1439 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001440 * returns -1, it will write an error message into the <err> buffer which will
1441 * automatically be allocated and must be passed as NULL. The trailing '\n'
1442 * will not be written. The function must be called with <args> pointing to the
1443 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001444 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001445int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001446{
1447 if (!*(args[0])) {
1448 /* if no option is set, use round-robin by default */
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 return 0;
1452 }
1453
1454 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001455 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1456 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001457 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001458 else if (!strcmp(args[0], "static-rr")) {
1459 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1460 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1461 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001462 else if (!strcmp(args[0], "first")) {
1463 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1464 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1465 }
Willy Tarreau51406232008-03-10 22:04:20 +01001466 else if (!strcmp(args[0], "leastconn")) {
1467 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1468 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1469 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001470 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001471 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1472 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001473 }
1474 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001475 int arg = 1;
1476
Willy Tarreau31682232007-11-29 15:38:04 +01001477 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1478 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001479
Oskar Stolc8dc41842012-05-19 10:19:54 +01001480 curproxy->uri_whole = 0;
1481
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001482 while (*args[arg]) {
1483 if (!strcmp(args[arg], "len")) {
1484 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001485 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001486 return -1;
1487 }
1488 curproxy->uri_len_limit = atoi(args[arg+1]);
1489 arg += 2;
1490 }
1491 else if (!strcmp(args[arg], "depth")) {
1492 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001493 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001494 return -1;
1495 }
1496 /* hint: we store the position of the ending '/' (depth+1) so
1497 * that we avoid a comparison while computing the hash.
1498 */
1499 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1500 arg += 2;
1501 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001502 else if (!strcmp(args[arg], "whole")) {
1503 curproxy->uri_whole = 1;
1504 arg += 1;
1505 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001506 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001507 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001508 return -1;
1509 }
1510 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001511 }
Willy Tarreau01732802007-11-01 22:48:15 +01001512 else if (!strcmp(args[0], "url_param")) {
1513 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001514 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001515 return -1;
1516 }
Willy Tarreau31682232007-11-29 15:38:04 +01001517 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1518 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001519
1520 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001521 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001522 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001523 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001524 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001525 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001526 return -1;
1527 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001528 }
Benoitaffb4812009-03-25 13:02:10 +01001529 }
1530 else if (!strncmp(args[0], "hdr(", 4)) {
1531 const char *beg, *end;
1532
1533 beg = args[0] + 4;
1534 end = strchr(beg, ')');
1535
1536 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001537 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001538 return -1;
1539 }
1540
1541 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1542 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1543
1544 free(curproxy->hh_name);
1545 curproxy->hh_len = end - beg;
1546 curproxy->hh_name = my_strndup(beg, end - beg);
1547 curproxy->hh_match_domain = 0;
1548
1549 if (*args[1]) {
1550 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001551 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001552 return -1;
1553 }
1554 curproxy->hh_match_domain = 1;
1555 }
Emeric Brun736aa232009-06-30 17:56:00 +02001556 }
1557 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1558 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1559 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1560
1561 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1562 const char *beg, *end;
1563
1564 beg = args[0] + 11;
1565 end = strchr(beg, ')');
1566
1567 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001568 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001569 return -1;
1570 }
1571
1572 free(curproxy->hh_name);
1573 curproxy->hh_name = my_strndup(beg, end - beg);
1574 curproxy->hh_len = end - beg;
1575 }
1576 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1577 free(curproxy->hh_name);
1578 curproxy->hh_name = strdup("mstshash");
1579 curproxy->hh_len = strlen(curproxy->hh_name);
1580 }
1581 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001582 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001583 return -1;
1584 }
Willy Tarreau01732802007-11-01 22:48:15 +01001585 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001586 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001587 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 +01001588 return -1;
1589 }
1590 return 0;
1591}
1592
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001593
1594/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001595/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001596/************************************************************************/
1597
Willy Tarreau34db1082012-04-19 17:16:54 +02001598/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001599 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001600 * undefined behaviour.
1601 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001602static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001603smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001604{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001605 struct proxy *px;
1606
Willy Tarreau37406352012-04-23 16:16:37 +02001607 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001608 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001609 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001610
1611 if (px->srv_act)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001612 smp->data.u.sint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001613 else if (px->lbprm.fbck)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001614 smp->data.u.sint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001615 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001616 smp->data.u.sint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001617
1618 return 1;
1619}
1620
Willy Tarreau37406352012-04-23 16:16:37 +02001621/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001622 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001623 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1624 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001625 */
1626static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001627smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001628{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001629 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001630
Willy Tarreau37406352012-04-23 16:16:37 +02001631 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001632 smp->data.type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001633 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001634 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001635 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001636 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001637 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001638 return 1;
1639}
1640
Willy Tarreau34db1082012-04-19 17:16:54 +02001641/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001642 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001643 * undefined behaviour.
1644 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001645static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001646smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001647{
1648 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001649
Willy Tarreau37406352012-04-23 16:16:37 +02001650 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001651 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001652 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001653
Willy Tarreau24e32d82012-04-23 23:55:44 +02001654 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001655 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001656 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001657
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001658 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001659 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001660 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001661 return 1;
1662 }
1663
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001664 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001665 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001666 }
1667
1668 return 1;
1669}
1670
Willy Tarreaua5e37562011-12-16 17:06:15 +01001671/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001672static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001673smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001674{
Willy Tarreaube508f12016-03-10 11:47:01 +01001675 if (!smp->strm)
1676 return 0;
1677
Willy Tarreauf853c462012-04-23 18:53:56 +02001678 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001679 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001680 smp->data.u.sint = smp->strm->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001681 return 1;
1682}
1683
Marcin Deranekd2471c22016-12-12 14:08:05 +01001684/* set string to the name of the backend */
1685static int
1686smp_fetch_be_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
1687{
1688 if (!smp->strm)
1689 return 0;
1690
1691 smp->data.u.str.str = (char *)smp->strm->be->id;
1692 if (!smp->data.u.str.str)
1693 return 0;
1694
1695 smp->data.type = SMP_T_STR;
1696 smp->flags = SMP_F_CONST;
1697 smp->data.u.str.len = strlen(smp->data.u.str.str);
1698
1699 return 1;
1700}
1701
Willy Tarreaua5e37562011-12-16 17:06:15 +01001702/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001703static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001704smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02001705{
Willy Tarreaube508f12016-03-10 11:47:01 +01001706 if (!smp->strm)
1707 return 0;
1708
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001709 if (!objt_server(smp->strm->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001710 return 0;
1711
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001712 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001713 smp->data.u.sint = objt_server(smp->strm->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001714
1715 return 1;
1716}
1717
Willy Tarreau34db1082012-04-19 17:16:54 +02001718/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001719 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001720 * undefined behaviour.
1721 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001722static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001723smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001724{
Willy Tarreau37406352012-04-23 16:16:37 +02001725 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001726 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001727 smp->data.u.sint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001728 return 1;
1729}
1730
Willy Tarreau34db1082012-04-19 17:16:54 +02001731/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001732 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001733 * undefined behaviour.
1734 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001735static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001736smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001737{
Willy Tarreau37406352012-04-23 16:16:37 +02001738 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001739 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001740 smp->data.u.sint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001741 return 1;
1742}
1743
Willy Tarreau34db1082012-04-19 17:16:54 +02001744/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001745 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001746 * undefined behaviour.
1747 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001748static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001749smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001750{
Willy Tarreau37406352012-04-23 16:16:37 +02001751 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001752 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001753 smp->data.u.sint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001754 return 1;
1755}
1756
Willy Tarreaua5e37562011-12-16 17:06:15 +01001757/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001758 * by the number of running servers and rounded up. If there is no running
1759 * server, we return twice the total, just as if we had half a running server.
1760 * This is more or less correct anyway, since we expect the last server to come
1761 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001762 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001763 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001764 */
1765static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001766smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02001767{
1768 int nbsrv;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001769 struct proxy *px;
Willy Tarreaua36af912009-10-10 12:02:45 +02001770
Willy Tarreau37406352012-04-23 16:16:37 +02001771 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001772 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001773 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001774
1775 if (px->srv_act)
1776 nbsrv = px->srv_act;
1777 else if (px->lbprm.fbck)
1778 nbsrv = 1;
1779 else
1780 nbsrv = px->srv_bck;
1781
1782 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001783 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001784 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001785 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001786
1787 return 1;
1788}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001789
Willy Tarreau34db1082012-04-19 17:16:54 +02001790/* set temp integer to the number of concurrent connections on the server in the backend.
1791 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1792 * undefined behaviour.
1793 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001795smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001796{
Willy Tarreauf853c462012-04-23 18:53:56 +02001797 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001798 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001799 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001800 return 1;
1801}
1802
Tait Clarridge7896d522012-12-05 21:39:31 -05001803/* set temp integer to the number of enabled servers on the proxy.
1804 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1805 * undefined behaviour.
1806 */
1807static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001808smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05001809{
1810 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001812 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05001813 return 1;
1814}
1815
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001816
1817/* Note: must not be declared <const> as its list will be overwritten.
1818 * Please take care of keeping this list alphabetically sorted.
1819 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001820static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001821 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1822 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1823 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
Marcin Deranekd2471c22016-12-12 14:08:05 +01001824 { "be_name", smp_fetch_be_name, 0, NULL, SMP_T_STR, SMP_USE_BKEND, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001825 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1826 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1827 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1828 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1829 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
1830 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001831 { "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 +02001832 { "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 +01001833 { /* END */ },
1834}};
1835
1836
Willy Tarreau61612d42012-04-19 18:42:05 +02001837/* Note: must not be declared <const> as its list will be overwritten.
1838 * Please take care of keeping this list alphabetically sorted.
1839 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001840static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001841 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001842}};
1843
1844
1845__attribute__((constructor))
1846static void __backend_init(void)
1847{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001848 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001849 acl_register_keywords(&acl_kws);
1850}
1851
Willy Tarreaubaaee002006-06-26 02:48:02 +02001852/*
1853 * Local variables:
1854 * c-indent-level: 8
1855 * c-basic-offset: 8
1856 * End:
1857 */