blob: 5e88f62c97891dec618a71d7832e16d26e720f7f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau44b90cc2010-05-24 20:27:29 +02004 * Copyright 2000-2010 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 Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020024#include <common/debug.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020025#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010030#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/backend.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020032#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020033#include <proto/lb_chash.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020034#include <proto/lb_fwlc.h>
35#include <proto/lb_fwrr.h>
36#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020038#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010040#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020041#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010042#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020043#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/task.h>
45
Willy Tarreaubaaee002006-06-26 02:48:02 +020046/*
47 * This function recounts the number of usable active and backup servers for
48 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010049 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010050 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010051 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020053void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
55 struct server *srv;
56
Willy Tarreau20697042007-11-15 23:26:18 +010057 px->srv_act = px->srv_bck = 0;
58 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010059 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010061 if (!srv_is_usable(srv->state, srv->eweight))
62 continue;
63
64 if (srv->state & SRV_BACKUP) {
65 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010066 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010067 px->lbprm.fbck = srv;
68 px->srv_bck++;
69 px->lbprm.tot_wbck += srv->eweight;
70 } else {
71 px->srv_act++;
72 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 }
74 }
Willy Tarreaub625a082007-11-26 01:15:43 +010075}
Willy Tarreau20697042007-11-15 23:26:18 +010076
Willy Tarreaub625a082007-11-26 01:15:43 +010077/* This function simply updates the backend's tot_weight and tot_used values
78 * after servers weights have been updated. It is designed to be used after
79 * recount_servers() or equivalent.
80 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020081void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010082{
Willy Tarreau20697042007-11-15 23:26:18 +010083 if (px->srv_act) {
84 px->lbprm.tot_weight = px->lbprm.tot_wact;
85 px->lbprm.tot_used = px->srv_act;
86 }
Willy Tarreaub625a082007-11-26 01:15:43 +010087 else if (px->lbprm.fbck) {
88 /* use only the first backup server */
89 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
90 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010091 }
92 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010093 px->lbprm.tot_weight = px->lbprm.tot_wbck;
94 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010095 }
Willy Tarreaub625a082007-11-26 01:15:43 +010096}
97
Willy Tarreau39c9ba72009-10-01 21:11:15 +020098/*
99 * This function tries to find a running server for the proxy <px> following
100 * the source hash method. Depending on the number of active/backup servers,
101 * it will either look for active servers, or for backup servers.
102 * If any server is found, it will be returned. If no valid server is found,
103 * NULL is returned.
104 */
105struct server *get_server_sh(struct proxy *px, const char *addr, int len)
106{
107 unsigned int h, l;
108
109 if (px->lbprm.tot_weight == 0)
110 return NULL;
111
112 l = h = 0;
113
114 /* note: we won't hash if there's only one server left */
115 if (px->lbprm.tot_used == 1)
116 goto hash_done;
117
118 while ((l + sizeof (int)) <= len) {
119 h ^= ntohl(*(unsigned int *)(&addr[l]));
120 l += sizeof (int);
121 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100122 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
123 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200124 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200125 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
126 return chash_get_server_hash(px, h);
127 else
128 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200129}
130
131/*
132 * This function tries to find a running server for the proxy <px> following
133 * the URI hash method. In order to optimize cache hits, the hash computation
134 * ends at the question mark. Depending on the number of active/backup servers,
135 * it will either look for active servers, or for backup servers.
136 * If any server is found, it will be returned. If no valid server is found,
137 * NULL is returned.
138 *
139 * This code was contributed by Guillaume Dallaire, who also selected this hash
140 * algorithm out of a tens because it gave him the best results.
141 *
142 */
143struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
144{
145 unsigned long hash = 0;
146 int c;
147 int slashes = 0;
148
149 if (px->lbprm.tot_weight == 0)
150 return NULL;
151
152 /* note: we won't hash if there's only one server left */
153 if (px->lbprm.tot_used == 1)
154 goto hash_done;
155
156 if (px->uri_len_limit)
157 uri_len = MIN(uri_len, px->uri_len_limit);
158
159 while (uri_len--) {
160 c = *uri++;
161 if (c == '/') {
162 slashes++;
163 if (slashes == px->uri_dirs_depth1) /* depth+1 */
164 break;
165 }
166 else if (c == '?')
167 break;
168
169 hash = c + (hash << 6) + (hash << 16) - hash;
170 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100171 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
172 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200173 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200174 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
175 return chash_get_server_hash(px, hash);
176 else
177 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200178}
179
Willy Tarreau01732802007-11-01 22:48:15 +0100180/*
181 * This function tries to find a running server for the proxy <px> following
182 * the URL parameter hash method. It looks for a specific parameter in the
183 * URL and hashes it to compute the server ID. This is useful to optimize
184 * performance by avoiding bounces between servers in contexts where sessions
185 * are shared but cookies are not usable. If the parameter is not found, NULL
186 * is returned. If any server is found, it will be returned. If no valid server
187 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100188 */
189struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
190{
191 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200192 const char *p;
193 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100194 int plen;
195
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200196 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100197 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100198 return NULL;
199
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 if ((p = memchr(uri, '?', uri_len)) == NULL)
201 return NULL;
202
Willy Tarreau01732802007-11-01 22:48:15 +0100203 p++;
204
205 uri_len -= (p - uri);
206 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200207 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100208
209 while (uri_len > plen) {
210 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200211 if (params[plen] == '=') {
212 if (memcmp(params, px->url_param_name, plen) == 0) {
213 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100214 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200215 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100216 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200217 p += plen + 1;
218 uri_len -= plen + 1;
219
Willy Tarreau01732802007-11-01 22:48:15 +0100220 while (uri_len && *p != '&') {
221 hash = *p + (hash << 6) + (hash << 16) - hash;
222 uri_len--;
223 p++;
224 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100225 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
226 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200227 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
228 return chash_get_server_hash(px, hash);
229 else
230 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100231 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200232 }
233 /* skip to next parameter */
234 p = memchr(params, '&', uri_len);
235 if (!p)
236 return NULL;
237 p++;
238 uri_len -= (p - params);
239 params = p;
240 }
241 return NULL;
242}
243
244/*
245 * this does the same as the previous server_ph, but check the body contents
246 */
247struct server *get_server_ph_post(struct session *s)
248{
249 unsigned long hash = 0;
250 struct http_txn *txn = &s->txn;
251 struct buffer *req = s->req;
252 struct http_msg *msg = &txn->req;
253 struct proxy *px = s->be;
254 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100255 unsigned long len = msg->body_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100256 const char *params = req->data + msg->sov;
257 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258
Willy Tarreau7c96f672009-12-27 22:47:25 +0100259 if (len > req->l - (msg->sov - msg->som))
260 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261
Willy Tarreau157dd632009-12-06 19:18:09 +0100262 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 return NULL;
264
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 if (px->lbprm.tot_weight == 0)
266 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267
268 while (len > plen) {
269 /* Look for the parameter name followed by an equal symbol */
270 if (params[plen] == '=') {
271 if (memcmp(params, px->url_param_name, plen) == 0) {
272 /* OK, we have the parameter here at <params>, and
273 * the value after the equal sign, at <p>
274 * skip the equal symbol
275 */
276 p += plen + 1;
277 len -= plen + 1;
278
279 while (len && *p != '&') {
280 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100281 /* if in a POST, body must be URI encoded or it's not a URI.
282 * Do not interprete any possible binary data as a parameter.
283 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200284 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
285 break;
286 return NULL; /* oh, no; this is not uri-encoded.
287 * This body does not contain parameters.
288 */
289 }
290 hash = *p + (hash << 6) + (hash << 16) - hash;
291 len--;
292 p++;
293 /* should we break if vlen exceeds limit? */
294 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100295 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
296 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200297 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
298 return chash_get_server_hash(px, hash);
299 else
300 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200301 }
302 }
Willy Tarreau01732802007-11-01 22:48:15 +0100303 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100305 if (!p)
306 return NULL;
307 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308 len -= (p - params);
309 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100310 }
311 return NULL;
312}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200314
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315/*
Benoitaffb4812009-03-25 13:02:10 +0100316 * This function tries to find a running server for the proxy <px> following
317 * the Header parameter hash method. It looks for a specific parameter in the
318 * URL and hashes it to compute the server ID. This is useful to optimize
319 * performance by avoiding bounces between servers in contexts where sessions
320 * are shared but cookies are not usable. If the parameter is not found, NULL
321 * is returned. If any server is found, it will be returned. If no valid server
322 * is found, NULL is returned.
323 */
324struct server *get_server_hh(struct session *s)
325{
326 unsigned long hash = 0;
327 struct http_txn *txn = &s->txn;
328 struct http_msg *msg = &txn->req;
329 struct proxy *px = s->be;
330 unsigned int plen = px->hh_len;
331 unsigned long len;
332 struct hdr_ctx ctx;
333 const char *p;
334
335 /* tot_weight appears to mean srv_count */
336 if (px->lbprm.tot_weight == 0)
337 return NULL;
338
Benoitaffb4812009-03-25 13:02:10 +0100339 ctx.idx = 0;
340
341 /* if the message is chunked, we skip the chunk size, but use the value as len */
342 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
343
344 /* if the header is not found or empty, let's fallback to round robin */
345 if (!ctx.idx || !ctx.vlen)
346 return NULL;
347
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200348 /* note: we won't hash if there's only one server left */
349 if (px->lbprm.tot_used == 1)
350 goto hash_done;
351
Benoitaffb4812009-03-25 13:02:10 +0100352 /* Found a the hh_name in the headers.
353 * we will compute the hash based on this value ctx.val.
354 */
355 len = ctx.vlen;
356 p = (char *)ctx.line + ctx.val;
357 if (!px->hh_match_domain) {
358 while (len) {
359 hash = *p + (hash << 6) + (hash << 16) - hash;
360 len--;
361 p++;
362 }
363 } else {
364 int dohash = 0;
365 p += len - 1;
366 /* special computation, use only main domain name, not tld/host
367 * going back from the end of string, start hashing at first
368 * dot stop at next.
369 * This is designed to work with the 'Host' header, and requires
370 * a special option to activate this.
371 */
372 while (len) {
373 if (*p == '.') {
374 if (!dohash)
375 dohash = 1;
376 else
377 break;
378 } else {
379 if (dohash)
380 hash = *p + (hash << 6) + (hash << 16) - hash;
381 }
382 len--;
383 p--;
384 }
385 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100386 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
387 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200388 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200389 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
390 return chash_get_server_hash(px, hash);
391 else
392 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100393}
394
Emeric Brun736aa232009-06-30 17:56:00 +0200395struct server *get_server_rch(struct session *s)
396{
397 unsigned long hash = 0;
398 struct proxy *px = s->be;
399 unsigned long len;
400 const char *p;
401 int ret;
402 struct acl_expr expr;
403 struct acl_test test;
404
405 /* tot_weight appears to mean srv_count */
406 if (px->lbprm.tot_weight == 0)
407 return NULL;
408
Emeric Brun736aa232009-06-30 17:56:00 +0200409 memset(&expr, 0, sizeof(expr));
410 memset(&test, 0, sizeof(test));
411
412 expr.arg.str = px->hh_name;
413 expr.arg_len = px->hh_len;
414
415 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +0100416 len = temp_pattern.data.str.len;
417
418 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200419 return NULL;
420
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200421 /* note: we won't hash if there's only one server left */
422 if (px->lbprm.tot_used == 1)
423 goto hash_done;
424
Emeric Brun736aa232009-06-30 17:56:00 +0200425 /* Found a the hh_name in the headers.
426 * we will compute the hash based on this value ctx.val.
427 */
Willy Tarreau664092c2011-12-16 19:11:42 +0100428 p = temp_pattern.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200429 while (len) {
430 hash = *p + (hash << 6) + (hash << 16) - hash;
431 len--;
432 p++;
433 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100434 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
435 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200436 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200437 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
438 return chash_get_server_hash(px, hash);
439 else
440 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200441}
Benoitaffb4812009-03-25 13:02:10 +0100442
443/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200444 * This function applies the load-balancing algorithm to the session, as
445 * defined by the backend it is assigned to. The session is then marked as
446 * 'assigned'.
447 *
448 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
449 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100450 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200451 * The function tries to keep the original connection slot if it reconnects to
452 * the same server, otherwise it releases it and tries to offer it.
453 *
454 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 *
456 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100457 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200458 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
459 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 * SRV_STATUS_INTERNAL for other unrecoverable errors.
461 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200462 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100463 * it does not need to be called anymore. This means that target_srv(&s->target)
464 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 *
466 */
467
468int assign_server(struct session *s)
469{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100470
Willy Tarreau7c669d72008-06-20 15:04:11 +0200471 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100472 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200473 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100474
Simon Horman8effd3d2011-08-13 08:03:52 +0900475 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476
Willy Tarreau7c669d72008-06-20 15:04:11 +0200477 err = SRV_STATUS_INTERNAL;
478 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
479 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100480
Willy Tarreau827aee92011-03-10 16:55:02 +0100481 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483
Willy Tarreau7c669d72008-06-20 15:04:11 +0200484 /* We have to release any connection slot before applying any LB algo,
485 * otherwise we may erroneously end up with no available slot.
486 */
487 if (conn_slot)
488 sess_change_server(s, NULL);
489
Willy Tarreau827aee92011-03-10 16:55:02 +0100490 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
491 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200492 * as well as if no server is available (check error code).
493 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100494
Willy Tarreau827aee92011-03-10 16:55:02 +0100495 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100496 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100497
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200498 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200499 int len;
500 /* we must check if we have at least one server available */
501 if (!s->be->lbprm.tot_weight) {
502 err = SRV_STATUS_NOSRV;
503 goto out;
504 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200506 /* First check whether we need to fetch some data or simply call
507 * the LB lookup function. Only the hashing functions will need
508 * some input data in fact, and will support multiple algorithms.
509 */
510 switch (s->be->lbprm.algo & BE_LB_LKUP) {
511 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100512 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200514
515 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100516 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200518
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200519 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200520 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200521 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200522 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100523 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200524 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100525 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200526 break;
527 }
528 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 err = SRV_STATUS_INTERNAL;
531 goto out;
532 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200533
534 switch (s->be->lbprm.algo & BE_LB_PARM) {
535 case BE_LB_HASH_SRC:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200536 if (s->req->prod->addr.from.ss_family == AF_INET)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 len = 4;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200538 else if (s->req->prod->addr.from.ss_family == AF_INET6)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200539 len = 16;
540 else {
541 /* unknown IP family */
542 err = SRV_STATUS_INTERNAL;
543 goto out;
544 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545
Willy Tarreau827aee92011-03-10 16:55:02 +0100546 srv = get_server_sh(s->be,
Willy Tarreau6471afb2011-09-23 10:54:59 +0200547 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
Willy Tarreau827aee92011-03-10 16:55:02 +0100548 len);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200549 break;
550
551 case BE_LB_HASH_URI:
552 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100553 if (s->txn.req.msg_state < HTTP_MSG_BODY)
554 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100555 srv = get_server_uh(s->be,
556 s->txn.req.sol + s->txn.req.sl.rq.u,
557 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200558 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200559
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200560 case BE_LB_HASH_PRM:
561 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100562 if (s->txn.req.msg_state < HTTP_MSG_BODY)
563 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100564
Willy Tarreau827aee92011-03-10 16:55:02 +0100565 srv = get_server_ph(s->be,
566 s->txn.req.sol + s->txn.req.sl.rq.u,
567 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100568
Willy Tarreau827aee92011-03-10 16:55:02 +0100569 if (!srv && s->txn.meth == HTTP_METH_POST)
570 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571 break;
Benoitaffb4812009-03-25 13:02:10 +0100572
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 case BE_LB_HASH_HDR:
574 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100575 if (s->txn.req.msg_state < HTTP_MSG_BODY)
576 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100577 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200578 break;
579
580 case BE_LB_HASH_RDP:
581 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583 break;
584
585 default:
586 /* unknown balancing algorithm */
587 err = SRV_STATUS_INTERNAL;
588 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100589 }
Emeric Brun736aa232009-06-30 17:56:00 +0200590
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 /* If the hashing parameter was not found, let's fall
592 * back to round robin on the map.
593 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200595 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100596 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200597 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100598 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200599 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600
601 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200602 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200603
Willy Tarreau7c669d72008-06-20 15:04:11 +0200604 default:
605 /* unknown balancing algorithm */
606 err = SRV_STATUS_INTERNAL;
607 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200609
Willy Tarreau827aee92011-03-10 16:55:02 +0100610 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200611 err = SRV_STATUS_FULL;
612 goto out;
613 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100614 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100615 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100616 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100617 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100618 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200619 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200620 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100621 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100623 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200624 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100625 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100626 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100627 }
628 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200629 err = SRV_STATUS_NOSRV;
630 goto out;
631 }
632
633 s->flags |= SN_ASSIGNED;
634 err = SRV_STATUS_OK;
635 out:
636
637 /* Either we take back our connection slot, or we offer it to someone
638 * else if we don't need it anymore.
639 */
640 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 if (conn_slot == srv) {
642 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200643 } else {
644 if (may_dequeue_tasks(conn_slot, s->be))
645 process_srv_queue(conn_slot);
646 }
647 }
648
649 out_err:
650 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651}
652
653
654/*
655 * This function assigns a server address to a session, and sets SN_ADDR_SET.
656 * The address is taken from the currently assigned server, or from the
657 * dispatch or transparent address.
658 *
659 * It may return :
660 * SRV_STATUS_OK if everything is OK.
661 * SRV_STATUS_INTERNAL for other unrecoverable errors.
662 *
663 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
664 * not cleared, so it's to the caller to clear it if required.
665 *
666 */
667int assign_server_address(struct session *s)
668{
669#ifdef DEBUG_FULL
670 fprintf(stderr,"assign_server_address : s=%p\n",s);
671#endif
672
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200673 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 /* A server is necessarily known for this session */
675 if (!(s->flags & SN_ASSIGNED))
676 return SRV_STATUS_INTERNAL;
677
Willy Tarreau6471afb2011-09-23 10:54:59 +0200678 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreau6471afb2011-09-23 10:54:59 +0200680 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200681 /* if the server has no address, we use the same address
682 * the client asked, which is handy for remapping ports
683 * locally on multiple addresses at once.
684 */
685 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
686 get_frt_addr(s);
687
Willy Tarreau6471afb2011-09-23 10:54:59 +0200688 if (s->req->prod->addr.to.ss_family == AF_INET) {
689 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
690 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
691 ((struct sockaddr_in6 *)&s->req->cons->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&s->req->prod->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200692 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200693 }
694
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 /* if this server remaps proxied ports, we'll use
696 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100697 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100698 int base_port;
699
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100700 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200701 get_frt_addr(s);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100702
703 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200704 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100705
706 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200707 base_port += get_host_port(&s->req->cons->addr.to);
708 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 }
710 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200711 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200713 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100715 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100717 if (!(s->flags & SN_FRT_ADDR_SET))
718 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
Willy Tarreau6471afb2011-09-23 10:54:59 +0200720 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
721 memcpy(&s->req->cons->addr.to, &s->req->prod->addr.to, MIN(sizeof(s->req->cons->addr.to), sizeof(s->req->prod->addr.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200722 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100723 /* when we support IPv6 on the backend, we may add other tests */
724 //qfprintf(stderr, "Cannot get original server address.\n");
725 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100727 else if (s->be->options & PR_O_HTTP_PROXY) {
728 /* If HTTP PROXY option is set, then server is already assigned
729 * during incoming client request parsing. */
730 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100731 else {
732 /* no server and no LB algorithm ! */
733 return SRV_STATUS_INTERNAL;
734 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735
736 s->flags |= SN_ADDR_SET;
737 return SRV_STATUS_OK;
738}
739
740
741/* This function assigns a server to session <s> if required, and can add the
742 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200743 * If ->srv_conn is set, the session is first released from the server.
744 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
745 * be called before any connection and after any retry or redispatch occurs.
746 *
747 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 *
749 * Returns :
750 *
751 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100752 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 * SRV_STATUS_QUEUED if the connection has been queued.
754 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100755 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200756 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 * SRV_STATUS_INTERNAL for other unrecoverable errors.
758 *
759 */
760int assign_server_and_queue(struct session *s)
761{
762 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764 int err;
765
766 if (s->pend_pos)
767 return SRV_STATUS_INTERNAL;
768
Willy Tarreau7c669d72008-06-20 15:04:11 +0200769 err = SRV_STATUS_OK;
770 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100772
Willy Tarreau7c669d72008-06-20 15:04:11 +0200773 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100774 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200775 /* This session was previously assigned to a server. We have to
776 * update the session's and the server's stats :
777 * - if the server changed :
778 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
779 * - set SN_REDISP if it was successfully redispatched
780 * - increment srv->redispatches and be->redispatches
781 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100782 */
783
Willy Tarreau827aee92011-03-10 16:55:02 +0100784 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200785 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
786 s->txn.flags &= ~TX_CK_MASK;
787 s->txn.flags |= TX_CK_DOWN;
788 }
789 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100790 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100791 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200792 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100793 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100794 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100795 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100796 }
797 }
798
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 switch (err) {
800 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200801 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 srv = target_srv(&s->target);
803 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200804 return SRV_STATUS_OK; /* dispatch or proxy mode */
805
806 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100807 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200808 return SRV_STATUS_OK;
809
810 /* OK, this session already has an assigned server, but no
811 * connection slot yet. Either it is a redispatch, or it was
812 * assigned from persistence information (direct mode).
813 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100814 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200815 /* server scheduled for redirection, and already assigned. We
816 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100817 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100818 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819 return SRV_STATUS_OK;
820 }
821
Willy Tarreau7c669d72008-06-20 15:04:11 +0200822 /* We might have to queue this session if the assigned server is full.
823 * We know we have to queue it into the server's queue, so if a maxqueue
824 * is set on the server, we must also check that the server's queue is
825 * not full, in which case we have to return FULL.
826 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 if (srv->maxconn &&
828 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200829
Willy Tarreau827aee92011-03-10 16:55:02 +0100830 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 return SRV_STATUS_FULL;
832
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 p = pendconn_add(s);
834 if (p)
835 return SRV_STATUS_QUEUED;
836 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200839
840 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100841 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842 return SRV_STATUS_OK;
843
844 case SRV_STATUS_FULL:
845 /* queue this session into the proxy's queue */
846 p = pendconn_add(s);
847 if (p)
848 return SRV_STATUS_QUEUED;
849 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200850 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851
852 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200853 return err;
854
Willy Tarreaubaaee002006-06-26 02:48:02 +0200855 case SRV_STATUS_INTERNAL:
856 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 default:
859 return SRV_STATUS_INTERNAL;
860 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100861}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862
Willy Tarreaub1d67742010-03-29 19:36:59 +0200863/* If an explicit source binding is specified on the server and/or backend, and
864 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200865 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200866 */
867static void assign_tproxy_address(struct session *s)
868{
869#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100870 struct server *srv = target_srv(&s->target);
871
872 if (srv && srv->state & SRV_BIND_SRC) {
873 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200874 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200875 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200876 break;
877 case SRV_TPROXY_CLI:
878 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200879 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200880 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200881 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200882 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100883 if (srv->bind_hdr_occ) {
Willy Tarreaubce70882009-09-07 11:51:47 +0200884 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100885 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200886 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
887 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
David du Colombier6f5ccb12011-03-10 22:26:24 +0100888 htonl(get_ip_from_hdr2(&s->txn.req,
889 srv->bind_hdr_name,
890 srv->bind_hdr_len,
891 &s->txn.hdr_idx,
892 srv->bind_hdr_occ));
Willy Tarreaubce70882009-09-07 11:51:47 +0200893 }
894 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200895 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200896 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200897 }
898 }
899 else if (s->be->options & PR_O_BIND_SRC) {
900 switch (s->be->options & PR_O_TPXY_MASK) {
901 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200902 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200903 break;
904 case PR_O_TPXY_CLI:
905 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200906 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200907 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200908 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200909 case PR_O_TPXY_DYN:
910 if (s->be->bind_hdr_occ) {
911 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100912 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200913 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
914 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
David du Colombier6f5ccb12011-03-10 22:26:24 +0100915 htonl(get_ip_from_hdr2(&s->txn.req,
916 s->be->bind_hdr_name,
917 s->be->bind_hdr_len,
918 &s->txn.hdr_idx,
919 s->be->bind_hdr_occ));
Willy Tarreaubce70882009-09-07 11:51:47 +0200920 }
921 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200922 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200923 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200924 }
925 }
926#endif
927}
928
929
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930/*
931 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200932 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100933 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 * It can return one of :
935 * - SN_ERR_NONE if everything's OK
936 * - SN_ERR_SRVTO if there are no more servers
937 * - SN_ERR_SRVCL if the connection was refused by the server
938 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
939 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
940 * - SN_ERR_INTERNAL for any other purely internal errors
941 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
942 */
943int connect_server(struct session *s)
944{
Willy Tarreau827aee92011-03-10 16:55:02 +0100945 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200946 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947
948 if (!(s->flags & SN_ADDR_SET)) {
949 err = assign_server_address(s);
950 if (err != SRV_STATUS_OK)
951 return SN_ERR_INTERNAL;
952 }
953
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200954 /* Prepare the stream interface for a TCP connection. Later
955 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100956 * NOTE: when we later support HTTP keep-alive, we'll have to
957 * decide here if we can reuse the connection by comparing the
958 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200959 */
960 stream_sock_prepare_interface(s->req->cons);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100961 s->req->cons->connect = tcp_connect_server;
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200962 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100963 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200964
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100965 /* process the case where the server requires the PROXY protocol to be sent */
966 s->req->cons->send_proxy_ofs = 0;
967 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
968 s->req->cons->send_proxy_ofs = 1; /* must compute size */
969 if (!(s->flags & SN_FRT_ADDR_SET))
970 get_frt_addr(s);
971 }
972
Willy Tarreaub1d67742010-03-29 19:36:59 +0200973 assign_tproxy_address(s);
974
Willy Tarreauac825402011-03-04 22:04:29 +0100975 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976
Willy Tarreau9650f372009-08-16 14:02:45 +0200977 if (err != SN_ERR_NONE)
978 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200979
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 srv = target_srv(&s->target);
981 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100982 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100983 srv->cur_sess++;
984 if (srv->cur_sess > srv->counters.cur_sess_max)
985 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100986 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +0100987 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988 }
989
Willy Tarreaubaaee002006-06-26 02:48:02 +0200990 return SN_ERR_NONE; /* connection is OK */
991}
992
993
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994/* This function performs the "redispatch" part of a connection attempt. It
995 * will assign a server if required, queue the connection if required, and
996 * handle errors that might arise at this level. It can change the server
997 * state. It will return 1 if it encounters an error, switches the server
998 * state, or has to queue a connection. Otherwise, it will return 0 indicating
999 * that the connection is ready to use.
1000 */
1001
1002int srv_redispatch_connect(struct session *t)
1003{
Willy Tarreau827aee92011-03-10 16:55:02 +01001004 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001005 int conn_err;
1006
1007 /* We know that we don't have any connection pending, so we will
1008 * try to get a new one, and wait in this state if it's queued
1009 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001010 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001012 srv = target_srv(&t->target);
1013
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 switch (conn_err) {
1015 case SRV_STATUS_OK:
1016 break;
1017
Willy Tarreau7c669d72008-06-20 15:04:11 +02001018 case SRV_STATUS_FULL:
1019 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1020 * and we can redispatch to another server, or it is not and we return
1021 * 503. This only makes sense in DIRECT mode however, because normal LB
1022 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001023 * would bring us on the same server again. Note that t->target is set
1024 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001025 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001026 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1027 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001028 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001029 goto redispatch;
1030 }
1031
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001032 if (!t->req->cons->err_type) {
1033 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001034 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001035 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001036
Willy Tarreau827aee92011-03-10 16:55:02 +01001037 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001038 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001039 return 1;
1040
Willy Tarreaubaaee002006-06-26 02:48:02 +02001041 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001042 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001043 if (!t->req->cons->err_type) {
1044 t->req->cons->err_type = SI_ET_CONN_ERR;
1045 t->req->cons->err_loc = NULL;
1046 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001047
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001048 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049 return 1;
1050
1051 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001052 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001053 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 /* do nothing else and do not wake any other session up */
1055 return 1;
1056
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057 case SRV_STATUS_INTERNAL:
1058 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001059 if (!t->req->cons->err_type) {
1060 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001061 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001062 }
1063
Willy Tarreau827aee92011-03-10 16:55:02 +01001064 if (srv)
1065 srv_inc_sess_ctr(srv);
1066 if (srv)
1067 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001068 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069
1070 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001071 if (may_dequeue_tasks(srv, t->be))
1072 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073 return 1;
1074 }
1075 /* if we get here, it's because we got SRV_STATUS_OK, which also
1076 * means that the connection has not been queued.
1077 */
1078 return 0;
1079}
1080
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001081/* Apply RDP cookie persistence to the current session. For this, the function
1082 * tries to extract an RDP cookie from the request buffer, and look for the
1083 * matching server in the list. If the server is found, it is assigned to the
1084 * session. This always returns 1, and the analyser removes itself from the
1085 * list. Nothing is performed if a server was already assigned.
1086 */
1087int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1088{
1089 struct proxy *px = s->be;
1090 int ret;
1091 struct acl_expr expr;
1092 struct acl_test test;
1093 struct server *srv = px->srv;
1094 struct sockaddr_in addr;
1095 char *p;
1096
1097 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1098 now_ms, __FUNCTION__,
1099 s,
1100 req,
1101 req->rex, req->wex,
1102 req->flags,
1103 req->l,
1104 req->analysers);
1105
1106 if (s->flags & SN_ASSIGNED)
1107 goto no_cookie;
1108
1109 memset(&expr, 0, sizeof(expr));
1110 memset(&test, 0, sizeof(test));
1111
1112 expr.arg.str = s->be->rdp_cookie_name;
1113 expr.arg_len = s->be->rdp_cookie_len;
1114
1115 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +01001116 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || temp_pattern.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001117 goto no_cookie;
1118
1119 memset(&addr, 0, sizeof(addr));
1120 addr.sin_family = AF_INET;
1121
Willy Tarreau664092c2011-12-16 19:11:42 +01001122 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
1123 addr.sin_addr.s_addr = strtoul(temp_pattern.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001124 if (*p != '.')
1125 goto no_cookie;
1126 p++;
1127 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1128 if (*p != '.')
1129 goto no_cookie;
1130
Willy Tarreau9e000c62011-03-10 14:03:36 +01001131 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001132 while (srv) {
1133 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1134 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1135 /* we found the server and it is usable */
1136 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001137 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001138 break;
1139 }
1140 }
1141 srv = srv->next;
1142 }
1143
1144no_cookie:
1145 req->analysers &= ~an_bit;
1146 req->analyse_exp = TICK_ETERNITY;
1147 return 1;
1148}
1149
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001150int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001151 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001152 return px->down_time;
1153
1154 return now.tv_sec - px->last_change + px->down_time;
1155}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001156
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001157/*
1158 * This function returns a string containing the balancing
1159 * mode of the proxy in a format suitable for stats.
1160 */
1161
1162const char *backend_lb_algo_str(int algo) {
1163
1164 if (algo == BE_LB_ALGO_RR)
1165 return "roundrobin";
1166 else if (algo == BE_LB_ALGO_SRR)
1167 return "static-rr";
1168 else if (algo == BE_LB_ALGO_LC)
1169 return "leastconn";
1170 else if (algo == BE_LB_ALGO_SH)
1171 return "source";
1172 else if (algo == BE_LB_ALGO_UH)
1173 return "uri";
1174 else if (algo == BE_LB_ALGO_PH)
1175 return "url_param";
1176 else if (algo == BE_LB_ALGO_HH)
1177 return "hdr";
1178 else if (algo == BE_LB_ALGO_RCH)
1179 return "rdp-cookie";
1180 else
1181 return NULL;
1182}
1183
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001184/* This function parses a "balance" statement in a backend section describing
1185 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1186 * returns -1, it may write an error message into ther <err> buffer, for at
1187 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1188 * written. The function must be called with <args> pointing to the first word
1189 * after "balance".
1190 */
1191int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1192{
1193 if (!*(args[0])) {
1194 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001195 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1196 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001197 return 0;
1198 }
1199
1200 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001201 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1202 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001203 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001204 else if (!strcmp(args[0], "static-rr")) {
1205 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1206 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1207 }
Willy Tarreau51406232008-03-10 22:04:20 +01001208 else if (!strcmp(args[0], "leastconn")) {
1209 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1210 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1211 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001212 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001213 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1214 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001215 }
1216 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001217 int arg = 1;
1218
Willy Tarreau31682232007-11-29 15:38:04 +01001219 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1220 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001221
1222 while (*args[arg]) {
1223 if (!strcmp(args[arg], "len")) {
1224 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1225 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1226 return -1;
1227 }
1228 curproxy->uri_len_limit = atoi(args[arg+1]);
1229 arg += 2;
1230 }
1231 else if (!strcmp(args[arg], "depth")) {
1232 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1233 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1234 return -1;
1235 }
1236 /* hint: we store the position of the ending '/' (depth+1) so
1237 * that we avoid a comparison while computing the hash.
1238 */
1239 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1240 arg += 2;
1241 }
1242 else {
1243 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1244 return -1;
1245 }
1246 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001247 }
Willy Tarreau01732802007-11-01 22:48:15 +01001248 else if (!strcmp(args[0], "url_param")) {
1249 if (!*args[1]) {
1250 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1251 return -1;
1252 }
Willy Tarreau31682232007-11-29 15:38:04 +01001253 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1254 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001255
1256 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001257 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001258 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001259 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001260 if (strcmp(args[2], "check_post")) {
1261 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1262 return -1;
1263 }
1264 if (*args[3]) {
1265 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1266 curproxy->url_param_post_limit = str2ui(args[3]);
1267 }
1268 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1269 if (!curproxy->url_param_post_limit)
1270 curproxy->url_param_post_limit = 48;
1271 else if ( curproxy->url_param_post_limit < 3 )
1272 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1273 }
Benoitaffb4812009-03-25 13:02:10 +01001274 }
1275 else if (!strncmp(args[0], "hdr(", 4)) {
1276 const char *beg, *end;
1277
1278 beg = args[0] + 4;
1279 end = strchr(beg, ')');
1280
1281 if (!end || end == beg) {
1282 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1283 return -1;
1284 }
1285
1286 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1287 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1288
1289 free(curproxy->hh_name);
1290 curproxy->hh_len = end - beg;
1291 curproxy->hh_name = my_strndup(beg, end - beg);
1292 curproxy->hh_match_domain = 0;
1293
1294 if (*args[1]) {
1295 if (strcmp(args[1], "use_domain_only")) {
1296 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1297 return -1;
1298 }
1299 curproxy->hh_match_domain = 1;
1300 }
1301
Emeric Brun736aa232009-06-30 17:56:00 +02001302 }
1303 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1304 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1305 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1306
1307 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1308 const char *beg, *end;
1309
1310 beg = args[0] + 11;
1311 end = strchr(beg, ')');
1312
1313 if (!end || end == beg) {
1314 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1315 return -1;
1316 }
1317
1318 free(curproxy->hh_name);
1319 curproxy->hh_name = my_strndup(beg, end - beg);
1320 curproxy->hh_len = end - beg;
1321 }
1322 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1323 free(curproxy->hh_name);
1324 curproxy->hh_name = strdup("mstshash");
1325 curproxy->hh_len = strlen(curproxy->hh_name);
1326 }
1327 else { /* syntax */
1328 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1329 return -1;
1330 }
Willy Tarreau01732802007-11-01 22:48:15 +01001331 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001332 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001333 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001334 return -1;
1335 }
1336 return 0;
1337}
1338
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001339
1340/************************************************************************/
1341/* All supported keywords must be declared here. */
1342/************************************************************************/
1343
Willy Tarreaua5e37562011-12-16 17:06:15 +01001344/* set temp integer to the number of enabled servers on the proxy */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001345static int
1346acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1347 struct acl_expr *expr, struct acl_test *test)
1348{
1349 test->flags = ACL_TEST_F_VOL_TEST;
1350 if (expr->arg_len) {
1351 /* another proxy was designated, we must look for it */
1352 for (px = proxy; px; px = px->next)
1353 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1354 break;
1355 }
1356 if (!px)
1357 return 0;
1358
1359 if (px->srv_act)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001360 temp_pattern.data.integer = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001361 else if (px->lbprm.fbck)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001362 temp_pattern.data.integer = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001363 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001364 temp_pattern.data.integer = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001365
1366 return 1;
1367}
1368
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001369/* report in test->flags a success or failure depending on the designated
1370 * server's state. There is no match function involved since there's no pattern.
1371 */
1372static int
1373acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1374 struct acl_expr *expr, struct acl_test *test)
1375{
1376 struct server *srv = expr->arg.srv;
1377
1378 test->flags = ACL_TEST_F_VOL_TEST;
1379 if (!(srv->state & SRV_MAINTAIN) &&
1380 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1381 test->flags |= ACL_TEST_F_SET_RES_PASS;
1382 else
1383 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1384 return 1;
1385}
1386
Willy Tarreaua5e37562011-12-16 17:06:15 +01001387/* set temp integer to the number of enabled servers on the proxy */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001388static int
1389acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1390 struct acl_expr *expr, struct acl_test *test)
1391{
1392 struct server *iterator;
1393 test->flags = ACL_TEST_F_VOL_TEST;
1394 if (expr->arg_len) {
1395 /* another proxy was designated, we must look for it */
1396 for (px = proxy; px; px = px->next)
1397 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1398 break;
1399 }
1400 if (!px)
1401 return 0;
1402
Willy Tarreaua5e37562011-12-16 17:06:15 +01001403 temp_pattern.data.integer = 0;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001404 iterator = px->srv;
1405 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001406 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001407 iterator = iterator->next;
1408 continue;
1409 }
1410 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001411 /* configuration is stupid */
1412 temp_pattern.data.integer = -1;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001413 return 1;
1414 }
1415
Willy Tarreaua5e37562011-12-16 17:06:15 +01001416 temp_pattern.data.integer += (iterator->maxconn - iterator->cur_sess)
1417 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001418 iterator = iterator->next;
1419 }
1420
1421 return 1;
1422}
1423
Willy Tarreaua5e37562011-12-16 17:06:15 +01001424/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001425static int
1426acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
1427 struct acl_expr *expr, struct acl_test *test) {
1428
1429 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001430 temp_pattern.data.integer = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001431
1432 return 1;
1433}
1434
Willy Tarreaua5e37562011-12-16 17:06:15 +01001435/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001436static int
1437acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
1438 struct acl_expr *expr, struct acl_test *test) {
1439
Willy Tarreau827aee92011-03-10 16:55:02 +01001440 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001441 return 0;
1442
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001443 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001444 temp_pattern.data.integer = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001445
1446 return 1;
1447}
1448
Willy Tarreaua5e37562011-12-16 17:06:15 +01001449/* set temp integer to the number of connections per second reaching the backend */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001450static int
1451acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1452 struct acl_expr *expr, struct acl_test *test)
1453{
1454 test->flags = ACL_TEST_F_VOL_TEST;
1455 if (expr->arg_len) {
1456 /* another proxy was designated, we must look for it */
1457 for (px = proxy; px; px = px->next)
1458 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1459 break;
1460 }
1461 if (!px)
1462 return 0;
1463
Willy Tarreaua5e37562011-12-16 17:06:15 +01001464 temp_pattern.data.integer = read_freq_ctr(&px->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001465 return 1;
1466}
1467
Willy Tarreaua5e37562011-12-16 17:06:15 +01001468/* set temp integer to the number of concurrent connections on the backend */
Willy Tarreaua36af912009-10-10 12:02:45 +02001469static int
1470acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1471 struct acl_expr *expr, struct acl_test *test)
1472{
1473 test->flags = ACL_TEST_F_VOL_TEST;
1474 if (expr->arg_len) {
1475 /* another proxy was designated, we must look for it */
1476 for (px = proxy; px; px = px->next)
1477 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1478 break;
1479 }
1480 if (!px)
1481 return 0;
1482
Willy Tarreaua5e37562011-12-16 17:06:15 +01001483 temp_pattern.data.integer = px->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001484 return 1;
1485}
1486
Willy Tarreaua5e37562011-12-16 17:06:15 +01001487/* set temp integer to the total number of queued connections on the backend */
Willy Tarreaua36af912009-10-10 12:02:45 +02001488static int
1489acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1490 struct acl_expr *expr, struct acl_test *test)
1491{
1492 test->flags = ACL_TEST_F_VOL_TEST;
1493 if (expr->arg_len) {
1494 /* another proxy was designated, we must look for it */
1495 for (px = proxy; px; px = px->next)
1496 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1497 break;
1498 }
1499 if (!px)
1500 return 0;
1501
Willy Tarreaua5e37562011-12-16 17:06:15 +01001502 temp_pattern.data.integer = px->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001503 return 1;
1504}
1505
Willy Tarreaua5e37562011-12-16 17:06:15 +01001506/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001507 * by the number of running servers and rounded up. If there is no running
1508 * server, we return twice the total, just as if we had half a running server.
1509 * This is more or less correct anyway, since we expect the last server to come
1510 * back soon.
1511 */
1512static int
1513acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1514 struct acl_expr *expr, struct acl_test *test)
1515{
1516 int nbsrv;
1517
1518 test->flags = ACL_TEST_F_VOL_TEST;
1519 if (expr->arg_len) {
1520 /* another proxy was designated, we must look for it */
1521 for (px = proxy; px; px = px->next)
1522 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1523 break;
1524 }
1525 if (!px)
1526 return 0;
1527
1528 if (px->srv_act)
1529 nbsrv = px->srv_act;
1530 else if (px->lbprm.fbck)
1531 nbsrv = 1;
1532 else
1533 nbsrv = px->srv_bck;
1534
1535 if (nbsrv > 0)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001536 temp_pattern.data.integer = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001537 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001538 temp_pattern.data.integer = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001539
1540 return 1;
1541}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001542
Willy Tarreaua5e37562011-12-16 17:06:15 +01001543/* set temp integer to the number of concurrent connections on the server in the backend */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001544static int
1545acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1546 struct acl_expr *expr, struct acl_test *test)
1547{
1548 struct server *srv = expr->arg.srv;
1549
Willy Tarreaua5e37562011-12-16 17:06:15 +01001550 temp_pattern.data.integer = srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001551 return 1;
1552}
1553
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001554/* Note: must not be declared <const> as its list will be overwritten */
1555static struct acl_kw_list acl_kws = {{ },{
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001556 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
1557 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
1558 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING },
1559 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
1560 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1561 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1562 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
1563 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreau4a0d8282011-02-23 15:24:16 +01001564 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL },
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001565 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001566 { NULL, NULL, NULL, NULL },
1567}};
1568
1569
1570__attribute__((constructor))
1571static void __backend_init(void)
1572{
1573 acl_register_keywords(&acl_kws);
1574}
1575
Willy Tarreaubaaee002006-06-26 02:48:02 +02001576/*
1577 * Local variables:
1578 * c-indent-level: 8
1579 * c-basic-offset: 8
1580 * End:
1581 */