blob: 5ddb88c4de21369cf508e1587bfb19e914bcccba [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010032#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020033#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020037#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010038#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020039#include <proto/lb_fwlc.h>
40#include <proto/lb_fwrr.h>
41#include <proto/lb_map.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042#include <proto/log.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010043#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010044#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020047#include <proto/proto_tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010050#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020052#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020053#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010054#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/task.h>
56
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050057int be_lastsession(const struct proxy *be)
58{
59 if (be->be_counters.last_sess)
60 return now.tv_sec - be->be_counters.last_sess;
61
62 return -1;
63}
64
Bhaskar98634f02013-10-29 23:30:51 -040065/* helper function to invoke the correct hash method */
66static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len)
67{
68 unsigned long hash;
69
70 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
71 case BE_LB_HFCN_DJB2:
72 hash = hash_djb2(key, len);
73 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010074 case BE_LB_HFCN_WT6:
75 hash = hash_wt6(key, len);
76 break;
Bhaskar98634f02013-10-29 23:30:51 -040077 case BE_LB_HFCN_SDBM:
78 /* this is the default hash function */
79 default:
80 hash = hash_sdbm(key, len);
81 break;
82 }
83
84 return hash;
85}
86
Willy Tarreaubaaee002006-06-26 02:48:02 +020087/*
88 * This function recounts the number of usable active and backup servers for
89 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010090 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010091 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010092 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020093 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020094void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020095{
96 struct server *srv;
97
Willy Tarreau20697042007-11-15 23:26:18 +010098 px->srv_act = px->srv_bck = 0;
99 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200102 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100103 continue;
104
Willy Tarreauc93cd162014-05-13 15:54:22 +0200105 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100106 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100107 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100108 px->lbprm.fbck = srv;
109 px->srv_bck++;
110 px->lbprm.tot_wbck += srv->eweight;
111 } else {
112 px->srv_act++;
113 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 }
115 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100116}
Willy Tarreau20697042007-11-15 23:26:18 +0100117
Willy Tarreaub625a082007-11-26 01:15:43 +0100118/* This function simply updates the backend's tot_weight and tot_used values
119 * after servers weights have been updated. It is designed to be used after
120 * recount_servers() or equivalent.
121 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200122void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100123{
Willy Tarreau20697042007-11-15 23:26:18 +0100124 if (px->srv_act) {
125 px->lbprm.tot_weight = px->lbprm.tot_wact;
126 px->lbprm.tot_used = px->srv_act;
127 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100128 else if (px->lbprm.fbck) {
129 /* use only the first backup server */
130 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
131 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100132 }
133 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100134 px->lbprm.tot_weight = px->lbprm.tot_wbck;
135 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100136 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100137}
138
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200139/*
140 * This function tries to find a running server for the proxy <px> following
141 * the source hash method. Depending on the number of active/backup servers,
142 * it will either look for active servers, or for backup servers.
143 * If any server is found, it will be returned. If no valid server is found,
144 * NULL is returned.
145 */
146struct server *get_server_sh(struct proxy *px, const char *addr, int len)
147{
148 unsigned int h, l;
149
150 if (px->lbprm.tot_weight == 0)
151 return NULL;
152
153 l = h = 0;
154
155 /* note: we won't hash if there's only one server left */
156 if (px->lbprm.tot_used == 1)
157 goto hash_done;
158
159 while ((l + sizeof (int)) <= len) {
160 h ^= ntohl(*(unsigned int *)(&addr[l]));
161 l += sizeof (int);
162 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500163 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100164 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200165 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200166 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
167 return chash_get_server_hash(px, h);
168 else
169 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200170}
171
172/*
173 * This function tries to find a running server for the proxy <px> following
174 * the URI hash method. In order to optimize cache hits, the hash computation
175 * ends at the question mark. Depending on the number of active/backup servers,
176 * it will either look for active servers, or for backup servers.
177 * If any server is found, it will be returned. If no valid server is found,
178 * NULL is returned.
179 *
180 * This code was contributed by Guillaume Dallaire, who also selected this hash
181 * algorithm out of a tens because it gave him the best results.
182 *
183 */
184struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
185{
186 unsigned long hash = 0;
187 int c;
188 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400189 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200190
191 if (px->lbprm.tot_weight == 0)
192 return NULL;
193
194 /* note: we won't hash if there's only one server left */
195 if (px->lbprm.tot_used == 1)
196 goto hash_done;
197
198 if (px->uri_len_limit)
199 uri_len = MIN(uri_len, px->uri_len_limit);
200
Bhaskar98634f02013-10-29 23:30:51 -0400201 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200202 while (uri_len--) {
Bhaskar98634f02013-10-29 23:30:51 -0400203 c = *end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200204 if (c == '/') {
205 slashes++;
206 if (slashes == px->uri_dirs_depth1) /* depth+1 */
207 break;
208 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100209 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200210 break;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200211 }
Bhaskar98634f02013-10-29 23:30:51 -0400212
213 hash = gen_hash(px, start, (end - start));
214
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500215 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100216 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200217 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200218 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
219 return chash_get_server_hash(px, hash);
220 else
221 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200222}
223
Willy Tarreau01732802007-11-01 22:48:15 +0100224/*
225 * This function tries to find a running server for the proxy <px> following
226 * the URL parameter hash method. It looks for a specific parameter in the
227 * URL and hashes it to compute the server ID. This is useful to optimize
228 * performance by avoiding bounces between servers in contexts where sessions
229 * are shared but cookies are not usable. If the parameter is not found, NULL
230 * is returned. If any server is found, it will be returned. If no valid server
231 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100232 */
233struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
234{
235 unsigned long hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400236 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200237 const char *p;
238 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100239 int plen;
240
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200241 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100242 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100243 return NULL;
244
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200245 if ((p = memchr(uri, '?', uri_len)) == NULL)
246 return NULL;
247
Willy Tarreau01732802007-11-01 22:48:15 +0100248 p++;
249
250 uri_len -= (p - uri);
251 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200252 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100253
254 while (uri_len > plen) {
255 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200256 if (params[plen] == '=') {
257 if (memcmp(params, px->url_param_name, plen) == 0) {
258 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100259 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100261 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400263 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 uri_len -= plen + 1;
265
Bhaskar98634f02013-10-29 23:30:51 -0400266 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100267 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400268 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100269 }
Bhaskar98634f02013-10-29 23:30:51 -0400270 hash = gen_hash(px, start, (end - start));
271
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500272 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100273 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400274
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200275 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
276 return chash_get_server_hash(px, hash);
277 else
278 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100279 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200280 }
281 /* skip to next parameter */
282 p = memchr(params, '&', uri_len);
283 if (!p)
284 return NULL;
285 p++;
286 uri_len -= (p - params);
287 params = p;
288 }
289 return NULL;
290}
291
292/*
293 * this does the same as the previous server_ph, but check the body contents
294 */
295struct server *get_server_ph_post(struct session *s)
296{
297 unsigned long hash = 0;
298 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200299 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 struct http_msg *msg = &txn->req;
301 struct proxy *px = s->be;
302 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200303 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200304 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100305 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400306 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307
Willy Tarreau157dd632009-12-06 19:18:09 +0100308 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 return NULL;
310
Willy Tarreau157dd632009-12-06 19:18:09 +0100311 if (px->lbprm.tot_weight == 0)
312 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200313
314 while (len > plen) {
315 /* Look for the parameter name followed by an equal symbol */
316 if (params[plen] == '=') {
317 if (memcmp(params, px->url_param_name, plen) == 0) {
318 /* OK, we have the parameter here at <params>, and
319 * the value after the equal sign, at <p>
320 * skip the equal symbol
321 */
322 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400323 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200324 len -= plen + 1;
325
Bhaskar98634f02013-10-29 23:30:51 -0400326 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100328 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400329 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100330 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200331 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
332 break;
333 return NULL; /* oh, no; this is not uri-encoded.
334 * This body does not contain parameters.
335 */
336 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200337 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400338 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200339 /* should we break if vlen exceeds limit? */
340 }
Bhaskar98634f02013-10-29 23:30:51 -0400341 hash = gen_hash(px, start, (end - start));
342
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500343 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100344 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400345
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200346 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
347 return chash_get_server_hash(px, hash);
348 else
349 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200350 }
351 }
Willy Tarreau01732802007-11-01 22:48:15 +0100352 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200353 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100354 if (!p)
355 return NULL;
356 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200357 len -= (p - params);
358 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100359 }
360 return NULL;
361}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200363
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364/*
Benoitaffb4812009-03-25 13:02:10 +0100365 * This function tries to find a running server for the proxy <px> following
366 * the Header parameter hash method. It looks for a specific parameter in the
367 * URL and hashes it to compute the server ID. This is useful to optimize
368 * performance by avoiding bounces between servers in contexts where sessions
369 * are shared but cookies are not usable. If the parameter is not found, NULL
370 * is returned. If any server is found, it will be returned. If no valid server
371 * is found, NULL is returned.
372 */
373struct server *get_server_hh(struct session *s)
374{
375 unsigned long hash = 0;
376 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100377 struct proxy *px = s->be;
378 unsigned int plen = px->hh_len;
379 unsigned long len;
380 struct hdr_ctx ctx;
381 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400382 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100383
384 /* tot_weight appears to mean srv_count */
385 if (px->lbprm.tot_weight == 0)
386 return NULL;
387
Benoitaffb4812009-03-25 13:02:10 +0100388 ctx.idx = 0;
389
390 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200391 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 +0100392
393 /* if the header is not found or empty, let's fallback to round robin */
394 if (!ctx.idx || !ctx.vlen)
395 return NULL;
396
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200397 /* note: we won't hash if there's only one server left */
398 if (px->lbprm.tot_used == 1)
399 goto hash_done;
400
Benoitaffb4812009-03-25 13:02:10 +0100401 /* Found a the hh_name in the headers.
402 * we will compute the hash based on this value ctx.val.
403 */
404 len = ctx.vlen;
405 p = (char *)ctx.line + ctx.val;
406 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400407 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100408 } else {
409 int dohash = 0;
410 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400411 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100412 /* special computation, use only main domain name, not tld/host
413 * going back from the end of string, start hashing at first
414 * dot stop at next.
415 * This is designed to work with the 'Host' header, and requires
416 * a special option to activate this.
417 */
418 while (len) {
419 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400420 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100421 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400422 start = end = p - 1;
423 }
Benoitaffb4812009-03-25 13:02:10 +0100424 else
425 break;
426 } else {
427 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400428 start--;
Benoitaffb4812009-03-25 13:02:10 +0100429 }
430 len--;
431 p--;
432 }
Bhaskar98634f02013-10-29 23:30:51 -0400433 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100434 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500435 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100436 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200437 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200438 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
439 return chash_get_server_hash(px, hash);
440 else
441 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100442}
443
Willy Tarreau34db1082012-04-19 17:16:54 +0200444/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200445struct server *get_server_rch(struct session *s)
446{
447 unsigned long hash = 0;
448 struct proxy *px = s->be;
449 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200450 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200451 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200452 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200453
454 /* tot_weight appears to mean srv_count */
455 if (px->lbprm.tot_weight == 0)
456 return NULL;
457
Willy Tarreau37406352012-04-23 16:16:37 +0200458 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200459
Willy Tarreau9b28e032012-10-12 23:49:43 +0200460 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200461
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200462 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200463 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100464
Willy Tarreau9b28e032012-10-12 23:49:43 +0200465 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200466
Willy Tarreau37406352012-04-23 16:16:37 +0200467 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200468 return NULL;
469
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200470 /* note: we won't hash if there's only one server left */
471 if (px->lbprm.tot_used == 1)
472 goto hash_done;
473
Emeric Brun736aa232009-06-30 17:56:00 +0200474 /* Found a the hh_name in the headers.
475 * we will compute the hash based on this value ctx.val.
476 */
Bhaskar98634f02013-10-29 23:30:51 -0400477 hash = gen_hash(px, smp.data.str.str, len);
478
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500479 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100480 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200481 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200482 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
483 return chash_get_server_hash(px, hash);
484 else
485 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200486}
Benoitaffb4812009-03-25 13:02:10 +0100487
488/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200489 * This function applies the load-balancing algorithm to the session, as
490 * defined by the backend it is assigned to. The session is then marked as
491 * 'assigned'.
492 *
493 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
494 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100495 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 * The function tries to keep the original connection slot if it reconnects to
497 * the same server, otherwise it releases it and tries to offer it.
498 *
499 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200500 *
501 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100502 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200503 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
504 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505 * SRV_STATUS_INTERNAL for other unrecoverable errors.
506 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200507 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100508 * it does not need to be called anymore. This means that target_srv(&s->target)
509 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510 *
511 */
512
513int assign_server(struct session *s)
514{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200515 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200516 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100517 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200518 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100519
Simon Horman8effd3d2011-08-13 08:03:52 +0900520 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 err = SRV_STATUS_INTERNAL;
523 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
524 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100525
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100526 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200527 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreau7c669d72008-06-20 15:04:11 +0200529 /* We have to release any connection slot before applying any LB algo,
530 * otherwise we may erroneously end up with no available slot.
531 */
532 if (conn_slot)
533 sess_change_server(s, NULL);
534
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100535 /* We will now try to find the good server and store it into <objt_server(s->target)>.
536 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 * as well as if no server is available (check error code).
538 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100539
Willy Tarreau827aee92011-03-10 16:55:02 +0100540 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100541 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100542 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100543
Willy Tarreau068621e2013-12-23 15:11:25 +0100544 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100545 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100546 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200547 ((s->txn.flags & TX_PREFER_LAST) ||
548 ((s->be->options & PR_O_PREF_LAST) &&
549 (!s->be->max_ka_queue ||
550 server_has_room(__objt_server(conn->target)) ||
551 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200552 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100553 /* This session was relying on a server in a previous request
554 * and the proxy has "option prefer-current-server" set, so
555 * let's try to reuse the same server.
556 */
557 srv = __objt_server(conn->target);
558 s->target = &srv->obj_type;
559 }
560 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200561 /* we must check if we have at least one server available */
562 if (!s->be->lbprm.tot_weight) {
563 err = SRV_STATUS_NOSRV;
564 goto out;
565 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200567 /* First check whether we need to fetch some data or simply call
568 * the LB lookup function. Only the hashing functions will need
569 * some input data in fact, and will support multiple algorithms.
570 */
571 switch (s->be->lbprm.algo & BE_LB_LKUP) {
572 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100573 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200574 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200575
Willy Tarreauf09c6602012-02-13 17:12:08 +0100576 case BE_LB_LKUP_FSTREE:
577 srv = fas_get_next_server(s->be, prev_srv);
578 break;
579
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200580 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100581 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200582 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200584 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200586 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200587 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100588 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200589 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100590 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200591 break;
592 }
593 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200595 err = SRV_STATUS_INTERNAL;
596 goto out;
597 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200598
599 switch (s->be->lbprm.algo & BE_LB_PARM) {
600 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200601 conn = objt_conn(s->req->prod->end);
602 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200603 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200604 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200605 4);
606 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200607 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200608 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200609 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200610 16);
611 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200612 else {
613 /* unknown IP family */
614 err = SRV_STATUS_INTERNAL;
615 goto out;
616 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200617 break;
618
619 case BE_LB_HASH_URI:
620 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100621 if (s->txn.req.msg_state < HTTP_MSG_BODY)
622 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200624 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200626 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200627
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200628 case BE_LB_HASH_PRM:
629 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100630 if (s->txn.req.msg_state < HTTP_MSG_BODY)
631 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100632
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200634 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100636
Willy Tarreau827aee92011-03-10 16:55:02 +0100637 if (!srv && s->txn.meth == HTTP_METH_POST)
638 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200639 break;
Benoitaffb4812009-03-25 13:02:10 +0100640
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200641 case BE_LB_HASH_HDR:
642 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100643 if (s->txn.req.msg_state < HTTP_MSG_BODY)
644 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100645 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200646 break;
647
648 case BE_LB_HASH_RDP:
649 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100650 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200651 break;
652
653 default:
654 /* unknown balancing algorithm */
655 err = SRV_STATUS_INTERNAL;
656 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100657 }
Emeric Brun736aa232009-06-30 17:56:00 +0200658
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200659 /* If the hashing parameter was not found, let's fall
660 * back to round robin on the map.
661 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100662 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200663 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100664 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200665 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100666 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200667 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200668
669 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200670 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200671
Willy Tarreau7c669d72008-06-20 15:04:11 +0200672 default:
673 /* unknown balancing algorithm */
674 err = SRV_STATUS_INTERNAL;
675 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200677
Willy Tarreau827aee92011-03-10 16:55:02 +0100678 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200679 err = SRV_STATUS_FULL;
680 goto out;
681 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100682 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100683 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100684 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100685 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100686 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200687 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200688 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100689 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100691 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200692 (conn = objt_conn(s->req->cons->end)) &&
693 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100694 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100695 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100696 }
697 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200698 err = SRV_STATUS_NOSRV;
699 goto out;
700 }
701
702 s->flags |= SN_ASSIGNED;
703 err = SRV_STATUS_OK;
704 out:
705
706 /* Either we take back our connection slot, or we offer it to someone
707 * else if we don't need it anymore.
708 */
709 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100710 if (conn_slot == srv) {
711 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200712 } else {
713 if (may_dequeue_tasks(conn_slot, s->be))
714 process_srv_queue(conn_slot);
715 }
716 }
717
718 out_err:
719 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720}
721
722
723/*
724 * This function assigns a server address to a session, and sets SN_ADDR_SET.
725 * The address is taken from the currently assigned server, or from the
726 * dispatch or transparent address.
727 *
728 * It may return :
729 * SRV_STATUS_OK if everything is OK.
730 * SRV_STATUS_INTERNAL for other unrecoverable errors.
731 *
732 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
733 * not cleared, so it's to the caller to clear it if required.
734 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200735 * The caller is responsible for having already assigned a connection
736 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200737 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 */
739int assign_server_address(struct session *s)
740{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200741 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200742 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200743
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744#ifdef DEBUG_FULL
745 fprintf(stderr,"assign_server_address : s=%p\n",s);
746#endif
747
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200748 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749 /* A server is necessarily known for this session */
750 if (!(s->flags & SN_ASSIGNED))
751 return SRV_STATUS_INTERNAL;
752
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200753 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200755 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200756 /* if the server has no address, we use the same address
757 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200758 * locally on multiple addresses at once. Nothing is done
759 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200760 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200761 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200762
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200763 if (cli_conn->addr.to.ss_family == AF_INET) {
764 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
765 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
766 ((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 +0200767 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200768 }
769
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 /* if this server remaps proxied ports, we'll use
771 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200772 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100773 int base_port;
774
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200775 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100776
777 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200778 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100779
780 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200781 base_port += get_host_port(&srv_conn->addr.to);
782 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 }
784 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200785 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200787 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200789 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200791 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200793 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
794 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100796 else if (s->be->options & PR_O_HTTP_PROXY) {
797 /* If HTTP PROXY option is set, then server is already assigned
798 * during incoming client request parsing. */
799 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100800 else {
801 /* no server and no LB algorithm ! */
802 return SRV_STATUS_INTERNAL;
803 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804
805 s->flags |= SN_ADDR_SET;
806 return SRV_STATUS_OK;
807}
808
809
810/* This function assigns a server to session <s> if required, and can add the
811 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200812 * If ->srv_conn is set, the session is first released from the server.
813 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
814 * be called before any connection and after any retry or redispatch occurs.
815 *
816 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 *
818 * Returns :
819 *
820 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100821 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 * SRV_STATUS_QUEUED if the connection has been queued.
823 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100824 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200825 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 * SRV_STATUS_INTERNAL for other unrecoverable errors.
827 *
828 */
829int assign_server_and_queue(struct session *s)
830{
831 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100832 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 int err;
834
835 if (s->pend_pos)
836 return SRV_STATUS_INTERNAL;
837
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838 err = SRV_STATUS_OK;
839 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100840 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100841
Willy Tarreau7c669d72008-06-20 15:04:11 +0200842 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100843 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844 /* This session was previously assigned to a server. We have to
845 * update the session's and the server's stats :
846 * - if the server changed :
847 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
848 * - set SN_REDISP if it was successfully redispatched
849 * - increment srv->redispatches and be->redispatches
850 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100851 */
852
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100853 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200854 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
855 s->txn.flags &= ~TX_CK_MASK;
856 s->txn.flags |= TX_CK_DOWN;
857 }
858 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100859 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100860 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100862 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100863 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100864 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100865 }
866 }
867
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868 switch (err) {
869 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200870 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100871 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100872 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200873 return SRV_STATUS_OK; /* dispatch or proxy mode */
874
875 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100876 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200877 return SRV_STATUS_OK;
878
879 /* OK, this session already has an assigned server, but no
880 * connection slot yet. Either it is a redispatch, or it was
881 * assigned from persistence information (direct mode).
882 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100883 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200884 /* server scheduled for redirection, and already assigned. We
885 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100886 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100887 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100888 return SRV_STATUS_OK;
889 }
890
Willy Tarreau7c669d72008-06-20 15:04:11 +0200891 /* We might have to queue this session if the assigned server is full.
892 * We know we have to queue it into the server's queue, so if a maxqueue
893 * is set on the server, we must also check that the server's queue is
894 * not full, in which case we have to return FULL.
895 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100896 if (srv->maxconn &&
897 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200898
Willy Tarreau827aee92011-03-10 16:55:02 +0100899 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200900 return SRV_STATUS_FULL;
901
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902 p = pendconn_add(s);
903 if (p)
904 return SRV_STATUS_QUEUED;
905 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200906 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200908
909 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100910 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911 return SRV_STATUS_OK;
912
913 case SRV_STATUS_FULL:
914 /* queue this session into the proxy's queue */
915 p = pendconn_add(s);
916 if (p)
917 return SRV_STATUS_QUEUED;
918 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920
921 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200922 return err;
923
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 case SRV_STATUS_INTERNAL:
925 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200926
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927 default:
928 return SRV_STATUS_INTERNAL;
929 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100930}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931
Willy Tarreaub1d67742010-03-29 19:36:59 +0200932/* If an explicit source binding is specified on the server and/or backend, and
933 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200934 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200935 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200936 */
937static void assign_tproxy_address(struct session *s)
938{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200939#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100940 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100941 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200942 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200943 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100944
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100945 if (srv && srv->conn_src.opts & CO_SRC_BIND)
946 src = &srv->conn_src;
947 else if (s->be->conn_src.opts & CO_SRC_BIND)
948 src = &s->be->conn_src;
949 else
950 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100951
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100952 switch (src->opts & CO_SRC_TPROXY_MASK) {
953 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200954 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100955 break;
956 case CO_SRC_TPROXY_CLI:
957 case CO_SRC_TPROXY_CIP:
958 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200959 cli_conn = objt_conn(s->req->prod->end);
960 if (cli_conn)
961 srv_conn->addr.from = cli_conn->addr.from;
962 else
963 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100964 break;
965 case CO_SRC_TPROXY_DYN:
966 if (src->bind_hdr_occ) {
967 char *vptr;
968 int vlen;
969 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100970
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100971 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200972 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
973 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
974 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100975
Willy Tarreau211cdec2014-04-17 20:18:08 +0200976 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100977 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
978 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200979 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100980 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200981 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100982 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200983 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100984 break;
985 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200986 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200987 }
988#endif
989}
990
991
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992/*
993 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200994 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100995 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 * It can return one of :
997 * - SN_ERR_NONE if everything's OK
998 * - SN_ERR_SRVTO if there are no more servers
999 * - SN_ERR_SRVCL if the connection was refused by the server
1000 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1001 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1002 * - SN_ERR_INTERNAL for any other purely internal errors
1003 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001004 * The server-facing stream interface is expected to hold a pre-allocated connection
1005 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 */
1007int connect_server(struct session *s)
1008{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001009 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001010 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001011 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001012 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001013 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014
Willy Tarreau34601a82013-12-15 16:33:46 +01001015 srv_conn = objt_conn(s->req->cons->end);
1016 if (srv_conn)
1017 reuse = s->target == srv_conn->target;
1018
1019 if (reuse) {
1020 /* Disable connection reuse if a dynamic source is used.
1021 * As long as we don't share connections between servers,
1022 * we don't need to disable connection reuse on no-idempotent
1023 * requests nor when PROXY protocol is used.
1024 */
1025 srv = objt_server(s->target);
1026 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1027 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1028 reuse = 0;
1029 }
1030 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1031 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1032 reuse = 0;
1033 }
1034 }
1035
1036 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001037 if (!srv_conn)
1038 return SN_ERR_RESOURCE;
1039
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040 if (!(s->flags & SN_ADDR_SET)) {
1041 err = assign_server_address(s);
1042 if (err != SRV_STATUS_OK)
1043 return SN_ERR_INTERNAL;
1044 }
1045
Willy Tarreauff605db2013-12-20 11:09:51 +01001046 if (!conn_xprt_ready(srv_conn)) {
1047 /* the target was only on the session, assign it to the SI now */
1048 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001049
Willy Tarreauff605db2013-12-20 11:09:51 +01001050 /* set the correct protocol on the output stream interface */
1051 if (objt_server(s->target)) {
1052 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1053 }
1054 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1055 /* proxies exclusively run on raw_sock right now */
1056 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1057 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1058 return SN_ERR_INTERNAL;
1059 }
1060 else
1061 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001062
Willy Tarreauff605db2013-12-20 11:09:51 +01001063 /* process the case where the server requires the PROXY protocol to be sent */
1064 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001065 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001066 srv_conn->send_proxy_ofs = 1; /* must compute size */
1067 cli_conn = objt_conn(s->req->prod->end);
1068 if (cli_conn)
1069 conn_get_to_addr(cli_conn);
1070 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001071
Willy Tarreauff605db2013-12-20 11:09:51 +01001072 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001073
Willy Tarreauff605db2013-12-20 11:09:51 +01001074 assign_tproxy_address(s);
1075 }
1076 else {
1077 /* the connection is being reused, just re-attach it */
1078 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001079 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001080 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001081
William Lallemandb7ff6a32012-03-02 14:35:21 +01001082 /* flag for logging source ip/port */
1083 if (s->fe->options2 & PR_O2_SRC_ADDR)
1084 s->req->cons->flags |= SI_FL_SRC_ADDR;
1085
Willy Tarreau14f8e862012-08-30 22:23:13 +02001086 /* disable lingering */
1087 if (s->be->options & PR_O_TCP_NOLING)
1088 s->req->cons->flags |= SI_FL_NOLINGER;
1089
Willy Tarreau73b013b2012-05-21 16:31:45 +02001090 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091
Willy Tarreau9650f372009-08-16 14:02:45 +02001092 if (err != SN_ERR_NONE)
1093 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001094
Willy Tarreau14f8e862012-08-30 22:23:13 +02001095 /* set connect timeout */
1096 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1097
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001098 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001099 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001100 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001101 srv->cur_sess++;
1102 if (srv->cur_sess > srv->counters.cur_sess_max)
1103 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001104 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001105 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106 }
1107
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 return SN_ERR_NONE; /* connection is OK */
1109}
1110
1111
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112/* This function performs the "redispatch" part of a connection attempt. It
1113 * will assign a server if required, queue the connection if required, and
1114 * handle errors that might arise at this level. It can change the server
1115 * state. It will return 1 if it encounters an error, switches the server
1116 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1117 * that the connection is ready to use.
1118 */
1119
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001120int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121{
Willy Tarreau827aee92011-03-10 16:55:02 +01001122 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001123 int conn_err;
1124
1125 /* We know that we don't have any connection pending, so we will
1126 * try to get a new one, and wait in this state if it's queued
1127 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001128 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001129 conn_err = assign_server_and_queue(s);
1130 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001131
Willy Tarreaubaaee002006-06-26 02:48:02 +02001132 switch (conn_err) {
1133 case SRV_STATUS_OK:
1134 break;
1135
Willy Tarreau7c669d72008-06-20 15:04:11 +02001136 case SRV_STATUS_FULL:
1137 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1138 * and we can redispatch to another server, or it is not and we return
1139 * 503. This only makes sense in DIRECT mode however, because normal LB
1140 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001141 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001142 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001143 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001144 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1145 (s->be->options & PR_O_REDISP)) {
1146 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001147 goto redispatch;
1148 }
1149
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001150 if (!s->req->cons->err_type) {
1151 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001152 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001153
Willy Tarreau827aee92011-03-10 16:55:02 +01001154 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001155 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001156 return 1;
1157
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001159 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001160 if (!s->req->cons->err_type) {
1161 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001162 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001163
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001164 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 return 1;
1166
1167 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001168 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1169 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170 /* do nothing else and do not wake any other session up */
1171 return 1;
1172
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173 case SRV_STATUS_INTERNAL:
1174 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001175 if (!s->req->cons->err_type) {
1176 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001177 }
1178
Willy Tarreau827aee92011-03-10 16:55:02 +01001179 if (srv)
1180 srv_inc_sess_ctr(srv);
1181 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001182 srv_set_sess_last(srv);
1183 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001184 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001185 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186
1187 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001188 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001189 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001190 return 1;
1191 }
1192 /* if we get here, it's because we got SRV_STATUS_OK, which also
1193 * means that the connection has not been queued.
1194 */
1195 return 0;
1196}
1197
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001198/* sends a log message when a backend goes down, and also sets last
1199 * change date.
1200 */
1201void set_backend_down(struct proxy *be)
1202{
1203 be->last_change = now.tv_sec;
1204 be->down_trans++;
1205
1206 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1207 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1208}
1209
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001210/* Apply RDP cookie persistence to the current session. For this, the function
1211 * tries to extract an RDP cookie from the request buffer, and look for the
1212 * matching server in the list. If the server is found, it is assigned to the
1213 * session. This always returns 1, and the analyser removes itself from the
1214 * list. Nothing is performed if a server was already assigned.
1215 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001216int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001217{
1218 struct proxy *px = s->be;
1219 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001220 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001221 struct server *srv = px->srv;
1222 struct sockaddr_in addr;
1223 char *p;
1224
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001225 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001226 now_ms, __FUNCTION__,
1227 s,
1228 req,
1229 req->rex, req->wex,
1230 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001231 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001232 req->analysers);
1233
1234 if (s->flags & SN_ASSIGNED)
1235 goto no_cookie;
1236
Willy Tarreau37406352012-04-23 16:16:37 +02001237 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001238
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001239 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001240 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001241 goto no_cookie;
1242
1243 memset(&addr, 0, sizeof(addr));
1244 addr.sin_family = AF_INET;
1245
Willy Tarreau664092c2011-12-16 19:11:42 +01001246 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001247 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001248 if (*p != '.')
1249 goto no_cookie;
1250 p++;
1251 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1252 if (*p != '.')
1253 goto no_cookie;
1254
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001255 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001256 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001257 if (srv->addr.ss_family == AF_INET &&
1258 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001259 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001260 /* we found the server and it is usable */
1261 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001262 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001263 break;
1264 }
1265 }
1266 srv = srv->next;
1267 }
1268
1269no_cookie:
1270 req->analysers &= ~an_bit;
1271 req->analyse_exp = TICK_ETERNITY;
1272 return 1;
1273}
1274
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001275int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001276 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001277 return px->down_time;
1278
1279 return now.tv_sec - px->last_change + px->down_time;
1280}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001281
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001282/*
1283 * This function returns a string containing the balancing
1284 * mode of the proxy in a format suitable for stats.
1285 */
1286
1287const char *backend_lb_algo_str(int algo) {
1288
1289 if (algo == BE_LB_ALGO_RR)
1290 return "roundrobin";
1291 else if (algo == BE_LB_ALGO_SRR)
1292 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001293 else if (algo == BE_LB_ALGO_FAS)
1294 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001295 else if (algo == BE_LB_ALGO_LC)
1296 return "leastconn";
1297 else if (algo == BE_LB_ALGO_SH)
1298 return "source";
1299 else if (algo == BE_LB_ALGO_UH)
1300 return "uri";
1301 else if (algo == BE_LB_ALGO_PH)
1302 return "url_param";
1303 else if (algo == BE_LB_ALGO_HH)
1304 return "hdr";
1305 else if (algo == BE_LB_ALGO_RCH)
1306 return "rdp-cookie";
1307 else
1308 return NULL;
1309}
1310
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001311/* This function parses a "balance" statement in a backend section describing
1312 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001313 * returns -1, it will write an error message into the <err> buffer which will
1314 * automatically be allocated and must be passed as NULL. The trailing '\n'
1315 * will not be written. The function must be called with <args> pointing to the
1316 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001317 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001318int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001319{
1320 if (!*(args[0])) {
1321 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001322 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1323 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001324 return 0;
1325 }
1326
1327 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001328 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1329 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001330 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001331 else if (!strcmp(args[0], "static-rr")) {
1332 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1333 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1334 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001335 else if (!strcmp(args[0], "first")) {
1336 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1337 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1338 }
Willy Tarreau51406232008-03-10 22:04:20 +01001339 else if (!strcmp(args[0], "leastconn")) {
1340 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1341 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1342 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001343 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001344 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1345 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001346 }
1347 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001348 int arg = 1;
1349
Willy Tarreau31682232007-11-29 15:38:04 +01001350 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1351 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001352
Oskar Stolc8dc41842012-05-19 10:19:54 +01001353 curproxy->uri_whole = 0;
1354
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001355 while (*args[arg]) {
1356 if (!strcmp(args[arg], "len")) {
1357 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001358 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001359 return -1;
1360 }
1361 curproxy->uri_len_limit = atoi(args[arg+1]);
1362 arg += 2;
1363 }
1364 else if (!strcmp(args[arg], "depth")) {
1365 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001366 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001367 return -1;
1368 }
1369 /* hint: we store the position of the ending '/' (depth+1) so
1370 * that we avoid a comparison while computing the hash.
1371 */
1372 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1373 arg += 2;
1374 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001375 else if (!strcmp(args[arg], "whole")) {
1376 curproxy->uri_whole = 1;
1377 arg += 1;
1378 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001379 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001380 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001381 return -1;
1382 }
1383 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001384 }
Willy Tarreau01732802007-11-01 22:48:15 +01001385 else if (!strcmp(args[0], "url_param")) {
1386 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001387 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001388 return -1;
1389 }
Willy Tarreau31682232007-11-29 15:38:04 +01001390 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1391 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001392
1393 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001394 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001395 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001396 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001397 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001398 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001399 return -1;
1400 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001401 }
Benoitaffb4812009-03-25 13:02:10 +01001402 }
1403 else if (!strncmp(args[0], "hdr(", 4)) {
1404 const char *beg, *end;
1405
1406 beg = args[0] + 4;
1407 end = strchr(beg, ')');
1408
1409 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001410 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001411 return -1;
1412 }
1413
1414 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1415 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1416
1417 free(curproxy->hh_name);
1418 curproxy->hh_len = end - beg;
1419 curproxy->hh_name = my_strndup(beg, end - beg);
1420 curproxy->hh_match_domain = 0;
1421
1422 if (*args[1]) {
1423 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001424 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001425 return -1;
1426 }
1427 curproxy->hh_match_domain = 1;
1428 }
Emeric Brun736aa232009-06-30 17:56:00 +02001429 }
1430 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1431 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1432 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1433
1434 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1435 const char *beg, *end;
1436
1437 beg = args[0] + 11;
1438 end = strchr(beg, ')');
1439
1440 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001441 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001442 return -1;
1443 }
1444
1445 free(curproxy->hh_name);
1446 curproxy->hh_name = my_strndup(beg, end - beg);
1447 curproxy->hh_len = end - beg;
1448 }
1449 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1450 free(curproxy->hh_name);
1451 curproxy->hh_name = strdup("mstshash");
1452 curproxy->hh_len = strlen(curproxy->hh_name);
1453 }
1454 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001455 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001456 return -1;
1457 }
Willy Tarreau01732802007-11-01 22:48:15 +01001458 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001459 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001460 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 +01001461 return -1;
1462 }
1463 return 0;
1464}
1465
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001466
1467/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001468/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001469/************************************************************************/
1470
Willy Tarreau34db1082012-04-19 17:16:54 +02001471/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001472 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001473 * undefined behaviour.
1474 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001475static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001476smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001477 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001478{
Willy Tarreau37406352012-04-23 16:16:37 +02001479 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001480 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001481 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001482
1483 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001484 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001485 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001486 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001487 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001488 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001489
1490 return 1;
1491}
1492
Willy Tarreau37406352012-04-23 16:16:37 +02001493/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001494 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001495 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1496 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001497 */
1498static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001499smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001500 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001501{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001502 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001503
Willy Tarreau37406352012-04-23 16:16:37 +02001504 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001505 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001506 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001507 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001508 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001509 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001510 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001511 return 1;
1512}
1513
Willy Tarreau34db1082012-04-19 17:16:54 +02001514/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001515 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001516 * undefined behaviour.
1517 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001518static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001519smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001520 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001521{
1522 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001523
Willy Tarreau37406352012-04-23 16:16:37 +02001524 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001525 smp->type = SMP_T_UINT;
1526 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001527
Willy Tarreau24e32d82012-04-23 23:55:44 +02001528 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001529 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001530 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001531
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001532 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001533 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001534 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001535 return 1;
1536 }
1537
Willy Tarreauf853c462012-04-23 18:53:56 +02001538 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001539 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001540 }
1541
1542 return 1;
1543}
1544
Willy Tarreaua5e37562011-12-16 17:06:15 +01001545/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001546static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001547smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001548 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001549{
Willy Tarreauf853c462012-04-23 18:53:56 +02001550 smp->flags = SMP_F_VOL_TXN;
1551 smp->type = SMP_T_UINT;
1552 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001553 return 1;
1554}
1555
Willy Tarreaua5e37562011-12-16 17:06:15 +01001556/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001557static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001558smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001559 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001560{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001561 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001562 return 0;
1563
Willy Tarreauf853c462012-04-23 18:53:56 +02001564 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001565 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001566
1567 return 1;
1568}
1569
Willy Tarreau34db1082012-04-19 17:16:54 +02001570/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001571 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001572 * undefined behaviour.
1573 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001574static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001575smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001576 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001577{
Willy Tarreau37406352012-04-23 16:16:37 +02001578 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001579 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001580 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001581 return 1;
1582}
1583
Willy Tarreau34db1082012-04-19 17:16:54 +02001584/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001585 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001586 * undefined behaviour.
1587 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001588static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001589smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001590 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001591{
Willy Tarreau37406352012-04-23 16:16:37 +02001592 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001593 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001594 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001595 return 1;
1596}
1597
Willy Tarreau34db1082012-04-19 17:16:54 +02001598/* set temp integer to the total number of queued connections on the backend.
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 Tarreaua36af912009-10-10 12:02:45 +02001602static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001603smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001604 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001605{
Willy Tarreau37406352012-04-23 16:16:37 +02001606 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001607 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001608 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001609 return 1;
1610}
1611
Willy Tarreaua5e37562011-12-16 17:06:15 +01001612/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001613 * by the number of running servers and rounded up. If there is no running
1614 * server, we return twice the total, just as if we had half a running server.
1615 * This is more or less correct anyway, since we expect the last server to come
1616 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001617 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001618 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001619 */
1620static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001621smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001622 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001623{
1624 int nbsrv;
1625
Willy Tarreau37406352012-04-23 16:16:37 +02001626 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001627 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001628 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001629
1630 if (px->srv_act)
1631 nbsrv = px->srv_act;
1632 else if (px->lbprm.fbck)
1633 nbsrv = 1;
1634 else
1635 nbsrv = px->srv_bck;
1636
1637 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001638 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001639 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001640 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001641
1642 return 1;
1643}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001644
Willy Tarreau34db1082012-04-19 17:16:54 +02001645/* set temp integer to the number of concurrent connections on the server in the backend.
1646 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1647 * undefined behaviour.
1648 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001649static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001650smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001651 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001652{
Willy Tarreauf853c462012-04-23 18:53:56 +02001653 smp->flags = SMP_F_VOL_TEST;
1654 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001655 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001656 return 1;
1657}
1658
Tait Clarridge7896d522012-12-05 21:39:31 -05001659/* set temp integer to the number of enabled servers on the proxy.
1660 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1661 * undefined behaviour.
1662 */
1663static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001664smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001665 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001666{
1667 smp->flags = SMP_F_VOL_TEST;
1668 smp->type = SMP_T_UINT;
1669 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1670 return 1;
1671}
1672
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001673
1674/* Note: must not be declared <const> as its list will be overwritten.
1675 * Please take care of keeping this list alphabetically sorted.
1676 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001677static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001678 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1679 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1680 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1681 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1682 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1683 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1684 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1685 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1686 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1687 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1688 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1689 { /* END */ },
1690}};
1691
1692
Willy Tarreau61612d42012-04-19 18:42:05 +02001693/* Note: must not be declared <const> as its list will be overwritten.
1694 * Please take care of keeping this list alphabetically sorted.
1695 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001696static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001697 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001698}};
1699
1700
1701__attribute__((constructor))
1702static void __backend_init(void)
1703{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001704 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001705 acl_register_keywords(&acl_kws);
1706}
1707
Willy Tarreaubaaee002006-06-26 02:48:02 +02001708/*
1709 * Local variables:
1710 * c-indent-level: 8
1711 * c-basic-offset: 8
1712 * End:
1713 */