blob: 32eea68b4cf6534f97fbbc117cf10dda0a43e4b0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreauf09c6602012-02-13 17:12:08 +01004 * Copyright 2000-2012 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 Tarreauf09c6602012-02-13 17:12:08 +010034#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020035#include <proto/lb_fwlc.h>
36#include <proto/lb_fwrr.h>
37#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020039#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010041#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020042#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010043#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020044#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/task.h>
46
Willy Tarreaubaaee002006-06-26 02:48:02 +020047/*
48 * This function recounts the number of usable active and backup servers for
49 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010050 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010051 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010052 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020053 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020054void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020055{
56 struct server *srv;
57
Willy Tarreau20697042007-11-15 23:26:18 +010058 px->srv_act = px->srv_bck = 0;
59 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010060 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010062 if (!srv_is_usable(srv->state, srv->eweight))
63 continue;
64
65 if (srv->state & SRV_BACKUP) {
66 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010067 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010068 px->lbprm.fbck = srv;
69 px->srv_bck++;
70 px->lbprm.tot_wbck += srv->eweight;
71 } else {
72 px->srv_act++;
73 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
75 }
Willy Tarreaub625a082007-11-26 01:15:43 +010076}
Willy Tarreau20697042007-11-15 23:26:18 +010077
Willy Tarreaub625a082007-11-26 01:15:43 +010078/* This function simply updates the backend's tot_weight and tot_used values
79 * after servers weights have been updated. It is designed to be used after
80 * recount_servers() or equivalent.
81 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020082void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010083{
Willy Tarreau20697042007-11-15 23:26:18 +010084 if (px->srv_act) {
85 px->lbprm.tot_weight = px->lbprm.tot_wact;
86 px->lbprm.tot_used = px->srv_act;
87 }
Willy Tarreaub625a082007-11-26 01:15:43 +010088 else if (px->lbprm.fbck) {
89 /* use only the first backup server */
90 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
91 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010092 }
93 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010094 px->lbprm.tot_weight = px->lbprm.tot_wbck;
95 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010096 }
Willy Tarreaub625a082007-11-26 01:15:43 +010097}
98
Willy Tarreau39c9ba72009-10-01 21:11:15 +020099/*
100 * This function tries to find a running server for the proxy <px> following
101 * the source hash method. Depending on the number of active/backup servers,
102 * it will either look for active servers, or for backup servers.
103 * If any server is found, it will be returned. If no valid server is found,
104 * NULL is returned.
105 */
106struct server *get_server_sh(struct proxy *px, const char *addr, int len)
107{
108 unsigned int h, l;
109
110 if (px->lbprm.tot_weight == 0)
111 return NULL;
112
113 l = h = 0;
114
115 /* note: we won't hash if there's only one server left */
116 if (px->lbprm.tot_used == 1)
117 goto hash_done;
118
119 while ((l + sizeof (int)) <= len) {
120 h ^= ntohl(*(unsigned int *)(&addr[l]));
121 l += sizeof (int);
122 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100123 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
124 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200125 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200126 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
127 return chash_get_server_hash(px, h);
128 else
129 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200130}
131
132/*
133 * This function tries to find a running server for the proxy <px> following
134 * the URI hash method. In order to optimize cache hits, the hash computation
135 * ends at the question mark. Depending on the number of active/backup servers,
136 * it will either look for active servers, or for backup servers.
137 * If any server is found, it will be returned. If no valid server is found,
138 * NULL is returned.
139 *
140 * This code was contributed by Guillaume Dallaire, who also selected this hash
141 * algorithm out of a tens because it gave him the best results.
142 *
143 */
144struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
145{
146 unsigned long hash = 0;
147 int c;
148 int slashes = 0;
149
150 if (px->lbprm.tot_weight == 0)
151 return NULL;
152
153 /* note: we won't hash if there's only one server left */
154 if (px->lbprm.tot_used == 1)
155 goto hash_done;
156
157 if (px->uri_len_limit)
158 uri_len = MIN(uri_len, px->uri_len_limit);
159
160 while (uri_len--) {
161 c = *uri++;
162 if (c == '/') {
163 slashes++;
164 if (slashes == px->uri_dirs_depth1) /* depth+1 */
165 break;
166 }
167 else if (c == '?')
168 break;
169
170 hash = c + (hash << 6) + (hash << 16) - hash;
171 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100172 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
173 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200174 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200175 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
176 return chash_get_server_hash(px, hash);
177 else
178 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200179}
180
Willy Tarreau01732802007-11-01 22:48:15 +0100181/*
182 * This function tries to find a running server for the proxy <px> following
183 * the URL parameter hash method. It looks for a specific parameter in the
184 * URL and hashes it to compute the server ID. This is useful to optimize
185 * performance by avoiding bounces between servers in contexts where sessions
186 * are shared but cookies are not usable. If the parameter is not found, NULL
187 * is returned. If any server is found, it will be returned. If no valid server
188 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100189 */
190struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
191{
192 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200193 const char *p;
194 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100195 int plen;
196
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200197 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100198 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100199 return NULL;
200
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200201 if ((p = memchr(uri, '?', uri_len)) == NULL)
202 return NULL;
203
Willy Tarreau01732802007-11-01 22:48:15 +0100204 p++;
205
206 uri_len -= (p - uri);
207 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200208 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100209
210 while (uri_len > plen) {
211 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200212 if (params[plen] == '=') {
213 if (memcmp(params, px->url_param_name, plen) == 0) {
214 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100215 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200216 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100217 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200218 p += plen + 1;
219 uri_len -= plen + 1;
220
Willy Tarreau01732802007-11-01 22:48:15 +0100221 while (uri_len && *p != '&') {
222 hash = *p + (hash << 6) + (hash << 16) - hash;
223 uri_len--;
224 p++;
225 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100226 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
227 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200228 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
229 return chash_get_server_hash(px, hash);
230 else
231 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100232 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200233 }
234 /* skip to next parameter */
235 p = memchr(params, '&', uri_len);
236 if (!p)
237 return NULL;
238 p++;
239 uri_len -= (p - params);
240 params = p;
241 }
242 return NULL;
243}
244
245/*
246 * this does the same as the previous server_ph, but check the body contents
247 */
248struct server *get_server_ph_post(struct session *s)
249{
250 unsigned long hash = 0;
251 struct http_txn *txn = &s->txn;
252 struct buffer *req = s->req;
253 struct http_msg *msg = &txn->req;
254 struct proxy *px = s->be;
255 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100256 unsigned long len = msg->body_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100257 const char *params = req->data + msg->sov;
258 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259
Willy Tarreau7c96f672009-12-27 22:47:25 +0100260 if (len > req->l - (msg->sov - msg->som))
261 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262
Willy Tarreau157dd632009-12-06 19:18:09 +0100263 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 return NULL;
265
Willy Tarreau157dd632009-12-06 19:18:09 +0100266 if (px->lbprm.tot_weight == 0)
267 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268
269 while (len > plen) {
270 /* Look for the parameter name followed by an equal symbol */
271 if (params[plen] == '=') {
272 if (memcmp(params, px->url_param_name, plen) == 0) {
273 /* OK, we have the parameter here at <params>, and
274 * the value after the equal sign, at <p>
275 * skip the equal symbol
276 */
277 p += plen + 1;
278 len -= plen + 1;
279
280 while (len && *p != '&') {
281 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100282 /* if in a POST, body must be URI encoded or it's not a URI.
283 * Do not interprete any possible binary data as a parameter.
284 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200285 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
286 break;
287 return NULL; /* oh, no; this is not uri-encoded.
288 * This body does not contain parameters.
289 */
290 }
291 hash = *p + (hash << 6) + (hash << 16) - hash;
292 len--;
293 p++;
294 /* should we break if vlen exceeds limit? */
295 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100296 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
297 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200298 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
299 return chash_get_server_hash(px, hash);
300 else
301 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200302 }
303 }
Willy Tarreau01732802007-11-01 22:48:15 +0100304 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100306 if (!p)
307 return NULL;
308 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 len -= (p - params);
310 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100311 }
312 return NULL;
313}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200315
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316/*
Benoitaffb4812009-03-25 13:02:10 +0100317 * This function tries to find a running server for the proxy <px> following
318 * the Header parameter hash method. It looks for a specific parameter in the
319 * URL and hashes it to compute the server ID. This is useful to optimize
320 * performance by avoiding bounces between servers in contexts where sessions
321 * are shared but cookies are not usable. If the parameter is not found, NULL
322 * is returned. If any server is found, it will be returned. If no valid server
323 * is found, NULL is returned.
324 */
325struct server *get_server_hh(struct session *s)
326{
327 unsigned long hash = 0;
328 struct http_txn *txn = &s->txn;
329 struct http_msg *msg = &txn->req;
330 struct proxy *px = s->be;
331 unsigned int plen = px->hh_len;
332 unsigned long len;
333 struct hdr_ctx ctx;
334 const char *p;
335
336 /* tot_weight appears to mean srv_count */
337 if (px->lbprm.tot_weight == 0)
338 return NULL;
339
Benoitaffb4812009-03-25 13:02:10 +0100340 ctx.idx = 0;
341
342 /* if the message is chunked, we skip the chunk size, but use the value as len */
343 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
344
345 /* if the header is not found or empty, let's fallback to round robin */
346 if (!ctx.idx || !ctx.vlen)
347 return NULL;
348
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200349 /* note: we won't hash if there's only one server left */
350 if (px->lbprm.tot_used == 1)
351 goto hash_done;
352
Benoitaffb4812009-03-25 13:02:10 +0100353 /* Found a the hh_name in the headers.
354 * we will compute the hash based on this value ctx.val.
355 */
356 len = ctx.vlen;
357 p = (char *)ctx.line + ctx.val;
358 if (!px->hh_match_domain) {
359 while (len) {
360 hash = *p + (hash << 6) + (hash << 16) - hash;
361 len--;
362 p++;
363 }
364 } else {
365 int dohash = 0;
366 p += len - 1;
367 /* special computation, use only main domain name, not tld/host
368 * going back from the end of string, start hashing at first
369 * dot stop at next.
370 * This is designed to work with the 'Host' header, and requires
371 * a special option to activate this.
372 */
373 while (len) {
374 if (*p == '.') {
375 if (!dohash)
376 dohash = 1;
377 else
378 break;
379 } else {
380 if (dohash)
381 hash = *p + (hash << 6) + (hash << 16) - hash;
382 }
383 len--;
384 p--;
385 }
386 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100387 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
388 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200389 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200390 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
391 return chash_get_server_hash(px, hash);
392 else
393 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100394}
395
Emeric Brun736aa232009-06-30 17:56:00 +0200396struct server *get_server_rch(struct session *s)
397{
398 unsigned long hash = 0;
399 struct proxy *px = s->be;
400 unsigned long len;
401 const char *p;
402 int ret;
403 struct acl_expr expr;
404 struct acl_test test;
405
406 /* tot_weight appears to mean srv_count */
407 if (px->lbprm.tot_weight == 0)
408 return NULL;
409
Emeric Brun736aa232009-06-30 17:56:00 +0200410 memset(&expr, 0, sizeof(expr));
411 memset(&test, 0, sizeof(test));
412
413 expr.arg.str = px->hh_name;
414 expr.arg_len = px->hh_len;
415
416 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +0100417 len = temp_pattern.data.str.len;
418
419 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200420 return NULL;
421
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200422 /* note: we won't hash if there's only one server left */
423 if (px->lbprm.tot_used == 1)
424 goto hash_done;
425
Emeric Brun736aa232009-06-30 17:56:00 +0200426 /* Found a the hh_name in the headers.
427 * we will compute the hash based on this value ctx.val.
428 */
Willy Tarreau664092c2011-12-16 19:11:42 +0100429 p = temp_pattern.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200430 while (len) {
431 hash = *p + (hash << 6) + (hash << 16) - hash;
432 len--;
433 p++;
434 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100435 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
436 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);
Emeric Brun736aa232009-06-30 17:56:00 +0200442}
Benoitaffb4812009-03-25 13:02:10 +0100443
444/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200445 * This function applies the load-balancing algorithm to the session, as
446 * defined by the backend it is assigned to. The session is then marked as
447 * 'assigned'.
448 *
449 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
450 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100451 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200452 * The function tries to keep the original connection slot if it reconnects to
453 * the same server, otherwise it releases it and tries to offer it.
454 *
455 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456 *
457 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100458 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200459 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
460 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 * SRV_STATUS_INTERNAL for other unrecoverable errors.
462 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100464 * it does not need to be called anymore. This means that target_srv(&s->target)
465 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 *
467 */
468
469int assign_server(struct session *s)
470{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100471
Willy Tarreau7c669d72008-06-20 15:04:11 +0200472 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100473 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200474 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100475
Simon Horman8effd3d2011-08-13 08:03:52 +0900476 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477
Willy Tarreau7c669d72008-06-20 15:04:11 +0200478 err = SRV_STATUS_INTERNAL;
479 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
480 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100481
Willy Tarreau827aee92011-03-10 16:55:02 +0100482 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 /* We have to release any connection slot before applying any LB algo,
486 * otherwise we may erroneously end up with no available slot.
487 */
488 if (conn_slot)
489 sess_change_server(s, NULL);
490
Willy Tarreau827aee92011-03-10 16:55:02 +0100491 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
492 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200493 * as well as if no server is available (check error code).
494 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100495
Willy Tarreau827aee92011-03-10 16:55:02 +0100496 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100497 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100498
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200499 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 int len;
501 /* we must check if we have at least one server available */
502 if (!s->be->lbprm.tot_weight) {
503 err = SRV_STATUS_NOSRV;
504 goto out;
505 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200507 /* First check whether we need to fetch some data or simply call
508 * the LB lookup function. Only the hashing functions will need
509 * some input data in fact, and will support multiple algorithms.
510 */
511 switch (s->be->lbprm.algo & BE_LB_LKUP) {
512 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100513 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200515
Willy Tarreauf09c6602012-02-13 17:12:08 +0100516 case BE_LB_LKUP_FSTREE:
517 srv = fas_get_next_server(s->be, prev_srv);
518 break;
519
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200520 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100521 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200523
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200524 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200525 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200526 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200527 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100528 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200529 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100530 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200531 break;
532 }
533 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200534 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 err = SRV_STATUS_INTERNAL;
536 goto out;
537 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200538
539 switch (s->be->lbprm.algo & BE_LB_PARM) {
540 case BE_LB_HASH_SRC:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200541 if (s->req->prod->addr.from.ss_family == AF_INET)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200542 len = 4;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200543 else if (s->req->prod->addr.from.ss_family == AF_INET6)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200544 len = 16;
545 else {
546 /* unknown IP family */
547 err = SRV_STATUS_INTERNAL;
548 goto out;
549 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
Willy Tarreau827aee92011-03-10 16:55:02 +0100551 srv = get_server_sh(s->be,
Willy Tarreau6471afb2011-09-23 10:54:59 +0200552 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
Willy Tarreau827aee92011-03-10 16:55:02 +0100553 len);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554 break;
555
556 case BE_LB_HASH_URI:
557 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100558 if (s->txn.req.msg_state < HTTP_MSG_BODY)
559 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100560 srv = get_server_uh(s->be,
561 s->txn.req.sol + s->txn.req.sl.rq.u,
562 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200563 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200564
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200565 case BE_LB_HASH_PRM:
566 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100567 if (s->txn.req.msg_state < HTTP_MSG_BODY)
568 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100569
Willy Tarreau827aee92011-03-10 16:55:02 +0100570 srv = get_server_ph(s->be,
571 s->txn.req.sol + s->txn.req.sl.rq.u,
572 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100573
Willy Tarreau827aee92011-03-10 16:55:02 +0100574 if (!srv && s->txn.meth == HTTP_METH_POST)
575 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200576 break;
Benoitaffb4812009-03-25 13:02:10 +0100577
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200578 case BE_LB_HASH_HDR:
579 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100580 if (s->txn.req.msg_state < HTTP_MSG_BODY)
581 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583 break;
584
585 case BE_LB_HASH_RDP:
586 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100587 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 break;
589
590 default:
591 /* unknown balancing algorithm */
592 err = SRV_STATUS_INTERNAL;
593 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100594 }
Emeric Brun736aa232009-06-30 17:56:00 +0200595
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596 /* If the hashing parameter was not found, let's fall
597 * back to round robin on the map.
598 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100599 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200600 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100601 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200602 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100603 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200604 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200605
606 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200607 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200608
Willy Tarreau7c669d72008-06-20 15:04:11 +0200609 default:
610 /* unknown balancing algorithm */
611 err = SRV_STATUS_INTERNAL;
612 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200614
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616 err = SRV_STATUS_FULL;
617 goto out;
618 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100619 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100620 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100621 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100622 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200624 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200625 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100626 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100628 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200629 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100630 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100631 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100632 }
633 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200634 err = SRV_STATUS_NOSRV;
635 goto out;
636 }
637
638 s->flags |= SN_ASSIGNED;
639 err = SRV_STATUS_OK;
640 out:
641
642 /* Either we take back our connection slot, or we offer it to someone
643 * else if we don't need it anymore.
644 */
645 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100646 if (conn_slot == srv) {
647 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200648 } else {
649 if (may_dequeue_tasks(conn_slot, s->be))
650 process_srv_queue(conn_slot);
651 }
652 }
653
654 out_err:
655 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656}
657
658
659/*
660 * This function assigns a server address to a session, and sets SN_ADDR_SET.
661 * The address is taken from the currently assigned server, or from the
662 * dispatch or transparent address.
663 *
664 * It may return :
665 * SRV_STATUS_OK if everything is OK.
666 * SRV_STATUS_INTERNAL for other unrecoverable errors.
667 *
668 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
669 * not cleared, so it's to the caller to clear it if required.
670 *
671 */
672int assign_server_address(struct session *s)
673{
674#ifdef DEBUG_FULL
675 fprintf(stderr,"assign_server_address : s=%p\n",s);
676#endif
677
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200678 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 /* A server is necessarily known for this session */
680 if (!(s->flags & SN_ASSIGNED))
681 return SRV_STATUS_INTERNAL;
682
Willy Tarreau6471afb2011-09-23 10:54:59 +0200683 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684
Willy Tarreau6471afb2011-09-23 10:54:59 +0200685 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200686 /* if the server has no address, we use the same address
687 * the client asked, which is handy for remapping ports
688 * locally on multiple addresses at once.
689 */
690 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
691 get_frt_addr(s);
692
Willy Tarreau6471afb2011-09-23 10:54:59 +0200693 if (s->req->prod->addr.to.ss_family == AF_INET) {
694 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
695 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
696 ((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 +0200697 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200698 }
699
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 /* if this server remaps proxied ports, we'll use
701 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100702 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100703 int base_port;
704
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100705 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200706 get_frt_addr(s);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100707
708 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200709 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100710
711 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200712 base_port += get_host_port(&s->req->cons->addr.to);
713 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
715 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200716 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200718 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100720 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100722 if (!(s->flags & SN_FRT_ADDR_SET))
723 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724
Willy Tarreau6471afb2011-09-23 10:54:59 +0200725 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
726 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 +0200727 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100728 /* when we support IPv6 on the backend, we may add other tests */
729 //qfprintf(stderr, "Cannot get original server address.\n");
730 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100732 else if (s->be->options & PR_O_HTTP_PROXY) {
733 /* If HTTP PROXY option is set, then server is already assigned
734 * during incoming client request parsing. */
735 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100736 else {
737 /* no server and no LB algorithm ! */
738 return SRV_STATUS_INTERNAL;
739 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740
741 s->flags |= SN_ADDR_SET;
742 return SRV_STATUS_OK;
743}
744
745
746/* This function assigns a server to session <s> if required, and can add the
747 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200748 * If ->srv_conn is set, the session is first released from the server.
749 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
750 * be called before any connection and after any retry or redispatch occurs.
751 *
752 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 *
754 * Returns :
755 *
756 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100757 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 * SRV_STATUS_QUEUED if the connection has been queued.
759 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200761 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 * SRV_STATUS_INTERNAL for other unrecoverable errors.
763 *
764 */
765int assign_server_and_queue(struct session *s)
766{
767 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100768 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 int err;
770
771 if (s->pend_pos)
772 return SRV_STATUS_INTERNAL;
773
Willy Tarreau7c669d72008-06-20 15:04:11 +0200774 err = SRV_STATUS_OK;
775 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100777
Willy Tarreau7c669d72008-06-20 15:04:11 +0200778 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100779 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200780 /* This session was previously assigned to a server. We have to
781 * update the session's and the server's stats :
782 * - if the server changed :
783 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
784 * - set SN_REDISP if it was successfully redispatched
785 * - increment srv->redispatches and be->redispatches
786 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100787 */
788
Willy Tarreau827aee92011-03-10 16:55:02 +0100789 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200790 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
791 s->txn.flags &= ~TX_CK_MASK;
792 s->txn.flags |= TX_CK_DOWN;
793 }
794 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100795 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100796 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200797 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100798 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100799 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100800 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100801 }
802 }
803
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804 switch (err) {
805 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200806 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100807 srv = target_srv(&s->target);
808 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200809 return SRV_STATUS_OK; /* dispatch or proxy mode */
810
811 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100812 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 return SRV_STATUS_OK;
814
815 /* OK, this session already has an assigned server, but no
816 * connection slot yet. Either it is a redispatch, or it was
817 * assigned from persistence information (direct mode).
818 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 /* server scheduled for redirection, and already assigned. We
821 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100822 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100823 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100824 return SRV_STATUS_OK;
825 }
826
Willy Tarreau7c669d72008-06-20 15:04:11 +0200827 /* We might have to queue this session if the assigned server is full.
828 * We know we have to queue it into the server's queue, so if a maxqueue
829 * is set on the server, we must also check that the server's queue is
830 * not full, in which case we have to return FULL.
831 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100832 if (srv->maxconn &&
833 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200834
Willy Tarreau827aee92011-03-10 16:55:02 +0100835 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200836 return SRV_STATUS_FULL;
837
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 p = pendconn_add(s);
839 if (p)
840 return SRV_STATUS_QUEUED;
841 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200842 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844
845 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 return SRV_STATUS_OK;
848
849 case SRV_STATUS_FULL:
850 /* queue this session into the proxy's queue */
851 p = pendconn_add(s);
852 if (p)
853 return SRV_STATUS_QUEUED;
854 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200855 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856
857 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200858 return err;
859
Willy Tarreaubaaee002006-06-26 02:48:02 +0200860 case SRV_STATUS_INTERNAL:
861 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863 default:
864 return SRV_STATUS_INTERNAL;
865 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100866}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867
Willy Tarreaub1d67742010-03-29 19:36:59 +0200868/* If an explicit source binding is specified on the server and/or backend, and
869 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200870 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200871 */
872static void assign_tproxy_address(struct session *s)
873{
874#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100875 struct server *srv = target_srv(&s->target);
876
877 if (srv && srv->state & SRV_BIND_SRC) {
878 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200879 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200880 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200881 break;
882 case SRV_TPROXY_CLI:
883 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200884 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200885 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200886 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200887 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100888 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100889 char *vptr;
890 int vlen;
891
Willy Tarreaubce70882009-09-07 11:51:47 +0200892 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100893 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200894 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100895 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
896
897 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
898 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
899 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
900 htonl(inetaddr_host_lim(vptr, vptr + vlen));
901 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200902 }
903 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200904 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200905 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200906 }
907 }
908 else if (s->be->options & PR_O_BIND_SRC) {
909 switch (s->be->options & PR_O_TPXY_MASK) {
910 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200911 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200912 break;
913 case PR_O_TPXY_CLI:
914 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200915 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200916 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200917 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200918 case PR_O_TPXY_DYN:
919 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100920 char *vptr;
921 int vlen;
922
Willy Tarreaubce70882009-09-07 11:51:47 +0200923 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100924 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200925 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100926 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
927
928 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
929 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
930 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
931 htonl(inetaddr_host_lim(vptr, vptr + vlen));
932 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200933 }
934 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200935 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200936 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200937 }
938 }
939#endif
940}
941
942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943/*
944 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200945 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100946 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947 * It can return one of :
948 * - SN_ERR_NONE if everything's OK
949 * - SN_ERR_SRVTO if there are no more servers
950 * - SN_ERR_SRVCL if the connection was refused by the server
951 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
952 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
953 * - SN_ERR_INTERNAL for any other purely internal errors
954 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
955 */
956int connect_server(struct session *s)
957{
Willy Tarreau827aee92011-03-10 16:55:02 +0100958 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200959 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960
961 if (!(s->flags & SN_ADDR_SET)) {
962 err = assign_server_address(s);
963 if (err != SRV_STATUS_OK)
964 return SN_ERR_INTERNAL;
965 }
966
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200967 /* Prepare the stream interface for a TCP connection. Later
968 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100969 * NOTE: when we later support HTTP keep-alive, we'll have to
970 * decide here if we can reuse the connection by comparing the
971 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200972 */
973 stream_sock_prepare_interface(s->req->cons);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100974 s->req->cons->connect = tcp_connect_server;
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200975 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100976 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200977
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100978 /* process the case where the server requires the PROXY protocol to be sent */
979 s->req->cons->send_proxy_ofs = 0;
980 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
981 s->req->cons->send_proxy_ofs = 1; /* must compute size */
982 if (!(s->flags & SN_FRT_ADDR_SET))
983 get_frt_addr(s);
984 }
985
Willy Tarreaub1d67742010-03-29 19:36:59 +0200986 assign_tproxy_address(s);
987
Willy Tarreauac825402011-03-04 22:04:29 +0100988 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989
Willy Tarreau9650f372009-08-16 14:02:45 +0200990 if (err != SN_ERR_NONE)
991 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200992
Willy Tarreau827aee92011-03-10 16:55:02 +0100993 srv = target_srv(&s->target);
994 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100995 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100996 srv->cur_sess++;
997 if (srv->cur_sess > srv->counters.cur_sess_max)
998 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100999 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001000 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 }
1002
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003 return SN_ERR_NONE; /* connection is OK */
1004}
1005
1006
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007/* This function performs the "redispatch" part of a connection attempt. It
1008 * will assign a server if required, queue the connection if required, and
1009 * handle errors that might arise at this level. It can change the server
1010 * state. It will return 1 if it encounters an error, switches the server
1011 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1012 * that the connection is ready to use.
1013 */
1014
1015int srv_redispatch_connect(struct session *t)
1016{
Willy Tarreau827aee92011-03-10 16:55:02 +01001017 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 int conn_err;
1019
1020 /* We know that we don't have any connection pending, so we will
1021 * try to get a new one, and wait in this state if it's queued
1022 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001023 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001025 srv = target_srv(&t->target);
1026
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027 switch (conn_err) {
1028 case SRV_STATUS_OK:
1029 break;
1030
Willy Tarreau7c669d72008-06-20 15:04:11 +02001031 case SRV_STATUS_FULL:
1032 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1033 * and we can redispatch to another server, or it is not and we return
1034 * 503. This only makes sense in DIRECT mode however, because normal LB
1035 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001036 * would bring us on the same server again. Note that t->target is set
1037 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001038 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001039 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1040 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001041 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001042 goto redispatch;
1043 }
1044
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001045 if (!t->req->cons->err_type) {
1046 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001047 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001048 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001049
Willy Tarreau827aee92011-03-10 16:55:02 +01001050 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001051 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001052 return 1;
1053
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001055 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001056 if (!t->req->cons->err_type) {
1057 t->req->cons->err_type = SI_ET_CONN_ERR;
1058 t->req->cons->err_loc = NULL;
1059 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001060
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001061 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 return 1;
1063
1064 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001065 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001066 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067 /* do nothing else and do not wake any other session up */
1068 return 1;
1069
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 case SRV_STATUS_INTERNAL:
1071 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001072 if (!t->req->cons->err_type) {
1073 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001075 }
1076
Willy Tarreau827aee92011-03-10 16:55:02 +01001077 if (srv)
1078 srv_inc_sess_ctr(srv);
1079 if (srv)
1080 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001081 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001082
1083 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001084 if (may_dequeue_tasks(srv, t->be))
1085 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086 return 1;
1087 }
1088 /* if we get here, it's because we got SRV_STATUS_OK, which also
1089 * means that the connection has not been queued.
1090 */
1091 return 0;
1092}
1093
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001094/* Apply RDP cookie persistence to the current session. For this, the function
1095 * tries to extract an RDP cookie from the request buffer, and look for the
1096 * matching server in the list. If the server is found, it is assigned to the
1097 * session. This always returns 1, and the analyser removes itself from the
1098 * list. Nothing is performed if a server was already assigned.
1099 */
1100int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1101{
1102 struct proxy *px = s->be;
1103 int ret;
1104 struct acl_expr expr;
1105 struct acl_test test;
1106 struct server *srv = px->srv;
1107 struct sockaddr_in addr;
1108 char *p;
1109
1110 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1111 now_ms, __FUNCTION__,
1112 s,
1113 req,
1114 req->rex, req->wex,
1115 req->flags,
1116 req->l,
1117 req->analysers);
1118
1119 if (s->flags & SN_ASSIGNED)
1120 goto no_cookie;
1121
1122 memset(&expr, 0, sizeof(expr));
1123 memset(&test, 0, sizeof(test));
1124
1125 expr.arg.str = s->be->rdp_cookie_name;
1126 expr.arg_len = s->be->rdp_cookie_len;
1127
1128 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +01001129 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || temp_pattern.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001130 goto no_cookie;
1131
1132 memset(&addr, 0, sizeof(addr));
1133 addr.sin_family = AF_INET;
1134
Willy Tarreau664092c2011-12-16 19:11:42 +01001135 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
1136 addr.sin_addr.s_addr = strtoul(temp_pattern.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001137 if (*p != '.')
1138 goto no_cookie;
1139 p++;
1140 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1141 if (*p != '.')
1142 goto no_cookie;
1143
Willy Tarreau9e000c62011-03-10 14:03:36 +01001144 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001145 while (srv) {
1146 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1147 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1148 /* we found the server and it is usable */
1149 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001150 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001151 break;
1152 }
1153 }
1154 srv = srv->next;
1155 }
1156
1157no_cookie:
1158 req->analysers &= ~an_bit;
1159 req->analyse_exp = TICK_ETERNITY;
1160 return 1;
1161}
1162
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001163int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001164 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001165 return px->down_time;
1166
1167 return now.tv_sec - px->last_change + px->down_time;
1168}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001170/*
1171 * This function returns a string containing the balancing
1172 * mode of the proxy in a format suitable for stats.
1173 */
1174
1175const char *backend_lb_algo_str(int algo) {
1176
1177 if (algo == BE_LB_ALGO_RR)
1178 return "roundrobin";
1179 else if (algo == BE_LB_ALGO_SRR)
1180 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001181 else if (algo == BE_LB_ALGO_FAS)
1182 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001183 else if (algo == BE_LB_ALGO_LC)
1184 return "leastconn";
1185 else if (algo == BE_LB_ALGO_SH)
1186 return "source";
1187 else if (algo == BE_LB_ALGO_UH)
1188 return "uri";
1189 else if (algo == BE_LB_ALGO_PH)
1190 return "url_param";
1191 else if (algo == BE_LB_ALGO_HH)
1192 return "hdr";
1193 else if (algo == BE_LB_ALGO_RCH)
1194 return "rdp-cookie";
1195 else
1196 return NULL;
1197}
1198
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001199/* This function parses a "balance" statement in a backend section describing
1200 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1201 * returns -1, it may write an error message into ther <err> buffer, for at
1202 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1203 * written. The function must be called with <args> pointing to the first word
1204 * after "balance".
1205 */
1206int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1207{
1208 if (!*(args[0])) {
1209 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001210 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1211 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001212 return 0;
1213 }
1214
1215 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001216 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1217 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001218 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001219 else if (!strcmp(args[0], "static-rr")) {
1220 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1221 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1222 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001223 else if (!strcmp(args[0], "first")) {
1224 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1225 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1226 }
Willy Tarreau51406232008-03-10 22:04:20 +01001227 else if (!strcmp(args[0], "leastconn")) {
1228 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1229 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1230 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001231 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001232 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1233 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001234 }
1235 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001236 int arg = 1;
1237
Willy Tarreau31682232007-11-29 15:38:04 +01001238 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1239 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001240
1241 while (*args[arg]) {
1242 if (!strcmp(args[arg], "len")) {
1243 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1244 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1245 return -1;
1246 }
1247 curproxy->uri_len_limit = atoi(args[arg+1]);
1248 arg += 2;
1249 }
1250 else if (!strcmp(args[arg], "depth")) {
1251 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1252 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1253 return -1;
1254 }
1255 /* hint: we store the position of the ending '/' (depth+1) so
1256 * that we avoid a comparison while computing the hash.
1257 */
1258 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1259 arg += 2;
1260 }
1261 else {
1262 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1263 return -1;
1264 }
1265 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001266 }
Willy Tarreau01732802007-11-01 22:48:15 +01001267 else if (!strcmp(args[0], "url_param")) {
1268 if (!*args[1]) {
1269 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1270 return -1;
1271 }
Willy Tarreau31682232007-11-29 15:38:04 +01001272 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1273 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001274
1275 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001276 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001277 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001278 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001279 if (strcmp(args[2], "check_post")) {
1280 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1281 return -1;
1282 }
1283 if (*args[3]) {
1284 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1285 curproxy->url_param_post_limit = str2ui(args[3]);
1286 }
1287 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1288 if (!curproxy->url_param_post_limit)
1289 curproxy->url_param_post_limit = 48;
1290 else if ( curproxy->url_param_post_limit < 3 )
1291 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1292 }
Benoitaffb4812009-03-25 13:02:10 +01001293 }
1294 else if (!strncmp(args[0], "hdr(", 4)) {
1295 const char *beg, *end;
1296
1297 beg = args[0] + 4;
1298 end = strchr(beg, ')');
1299
1300 if (!end || end == beg) {
1301 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1302 return -1;
1303 }
1304
1305 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1306 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1307
1308 free(curproxy->hh_name);
1309 curproxy->hh_len = end - beg;
1310 curproxy->hh_name = my_strndup(beg, end - beg);
1311 curproxy->hh_match_domain = 0;
1312
1313 if (*args[1]) {
1314 if (strcmp(args[1], "use_domain_only")) {
1315 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1316 return -1;
1317 }
1318 curproxy->hh_match_domain = 1;
1319 }
1320
Emeric Brun736aa232009-06-30 17:56:00 +02001321 }
1322 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1323 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1324 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1325
1326 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1327 const char *beg, *end;
1328
1329 beg = args[0] + 11;
1330 end = strchr(beg, ')');
1331
1332 if (!end || end == beg) {
1333 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1334 return -1;
1335 }
1336
1337 free(curproxy->hh_name);
1338 curproxy->hh_name = my_strndup(beg, end - beg);
1339 curproxy->hh_len = end - beg;
1340 }
1341 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1342 free(curproxy->hh_name);
1343 curproxy->hh_name = strdup("mstshash");
1344 curproxy->hh_len = strlen(curproxy->hh_name);
1345 }
1346 else { /* syntax */
1347 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1348 return -1;
1349 }
Willy Tarreau01732802007-11-01 22:48:15 +01001350 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001351 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001352 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 +01001353 return -1;
1354 }
1355 return 0;
1356}
1357
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001358
1359/************************************************************************/
1360/* All supported keywords must be declared here. */
1361/************************************************************************/
1362
Willy Tarreaua5e37562011-12-16 17:06:15 +01001363/* set temp integer to the number of enabled servers on the proxy */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001364static int
1365acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1366 struct acl_expr *expr, struct acl_test *test)
1367{
1368 test->flags = ACL_TEST_F_VOL_TEST;
1369 if (expr->arg_len) {
1370 /* another proxy was designated, we must look for it */
1371 for (px = proxy; px; px = px->next)
1372 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1373 break;
1374 }
1375 if (!px)
1376 return 0;
1377
1378 if (px->srv_act)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001379 temp_pattern.data.integer = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001380 else if (px->lbprm.fbck)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001381 temp_pattern.data.integer = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001382 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001383 temp_pattern.data.integer = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001384
1385 return 1;
1386}
1387
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001388/* report in test->flags a success or failure depending on the designated
1389 * server's state. There is no match function involved since there's no pattern.
1390 */
1391static int
1392acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1393 struct acl_expr *expr, struct acl_test *test)
1394{
1395 struct server *srv = expr->arg.srv;
1396
1397 test->flags = ACL_TEST_F_VOL_TEST;
1398 if (!(srv->state & SRV_MAINTAIN) &&
1399 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1400 test->flags |= ACL_TEST_F_SET_RES_PASS;
1401 else
1402 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1403 return 1;
1404}
1405
Willy Tarreaua5e37562011-12-16 17:06:15 +01001406/* set temp integer to the number of enabled servers on the proxy */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001407static int
1408acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1409 struct acl_expr *expr, struct acl_test *test)
1410{
1411 struct server *iterator;
1412 test->flags = ACL_TEST_F_VOL_TEST;
1413 if (expr->arg_len) {
1414 /* another proxy was designated, we must look for it */
1415 for (px = proxy; px; px = px->next)
1416 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1417 break;
1418 }
1419 if (!px)
1420 return 0;
1421
Willy Tarreaua5e37562011-12-16 17:06:15 +01001422 temp_pattern.data.integer = 0;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001423 iterator = px->srv;
1424 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001425 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001426 iterator = iterator->next;
1427 continue;
1428 }
1429 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001430 /* configuration is stupid */
1431 temp_pattern.data.integer = -1;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001432 return 1;
1433 }
1434
Willy Tarreaua5e37562011-12-16 17:06:15 +01001435 temp_pattern.data.integer += (iterator->maxconn - iterator->cur_sess)
1436 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001437 iterator = iterator->next;
1438 }
1439
1440 return 1;
1441}
1442
Willy Tarreaua5e37562011-12-16 17:06:15 +01001443/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001444static int
1445acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
1446 struct acl_expr *expr, struct acl_test *test) {
1447
1448 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001449 temp_pattern.data.integer = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001450
1451 return 1;
1452}
1453
Willy Tarreaua5e37562011-12-16 17:06:15 +01001454/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001455static int
1456acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
1457 struct acl_expr *expr, struct acl_test *test) {
1458
Willy Tarreau827aee92011-03-10 16:55:02 +01001459 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001460 return 0;
1461
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001462 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001463 temp_pattern.data.integer = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001464
1465 return 1;
1466}
1467
Willy Tarreaua5e37562011-12-16 17:06:15 +01001468/* set temp integer to the number of connections per second reaching the backend */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001469static int
1470acl_fetch_be_sess_rate(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 = read_freq_ctr(&px->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001484 return 1;
1485}
1486
Willy Tarreaua5e37562011-12-16 17:06:15 +01001487/* set temp integer to the number of concurrent connections on the backend */
Willy Tarreaua36af912009-10-10 12:02:45 +02001488static int
1489acl_fetch_be_conn(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->beconn;
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 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001507static int
1508acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1509 struct acl_expr *expr, struct acl_test *test)
1510{
1511 test->flags = ACL_TEST_F_VOL_TEST;
1512 if (expr->arg_len) {
1513 /* another proxy was designated, we must look for it */
1514 for (px = proxy; px; px = px->next)
1515 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1516 break;
1517 }
1518 if (!px)
1519 return 0;
1520
Willy Tarreaua5e37562011-12-16 17:06:15 +01001521 temp_pattern.data.integer = px->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001522 return 1;
1523}
1524
Willy Tarreaua5e37562011-12-16 17:06:15 +01001525/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001526 * by the number of running servers and rounded up. If there is no running
1527 * server, we return twice the total, just as if we had half a running server.
1528 * This is more or less correct anyway, since we expect the last server to come
1529 * back soon.
1530 */
1531static int
1532acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1533 struct acl_expr *expr, struct acl_test *test)
1534{
1535 int nbsrv;
1536
1537 test->flags = ACL_TEST_F_VOL_TEST;
1538 if (expr->arg_len) {
1539 /* another proxy was designated, we must look for it */
1540 for (px = proxy; px; px = px->next)
1541 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1542 break;
1543 }
1544 if (!px)
1545 return 0;
1546
1547 if (px->srv_act)
1548 nbsrv = px->srv_act;
1549 else if (px->lbprm.fbck)
1550 nbsrv = 1;
1551 else
1552 nbsrv = px->srv_bck;
1553
1554 if (nbsrv > 0)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001555 temp_pattern.data.integer = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001556 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001557 temp_pattern.data.integer = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001558
1559 return 1;
1560}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001561
Willy Tarreaua5e37562011-12-16 17:06:15 +01001562/* set temp integer to the number of concurrent connections on the server in the backend */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001563static int
1564acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1565 struct acl_expr *expr, struct acl_test *test)
1566{
1567 struct server *srv = expr->arg.srv;
1568
Willy Tarreaua5e37562011-12-16 17:06:15 +01001569 temp_pattern.data.integer = srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001570 return 1;
1571}
1572
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001573/* Note: must not be declared <const> as its list will be overwritten */
1574static struct acl_kw_list acl_kws = {{ },{
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001575 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
1576 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
1577 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING },
1578 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
1579 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1580 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1581 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
1582 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreau4a0d8282011-02-23 15:24:16 +01001583 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL },
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001584 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001585 { NULL, NULL, NULL, NULL },
1586}};
1587
1588
1589__attribute__((constructor))
1590static void __backend_init(void)
1591{
1592 acl_register_keywords(&acl_kws);
1593}
1594
Willy Tarreaubaaee002006-06-26 02:48:02 +02001595/*
1596 * Local variables:
1597 * c-indent-level: 8
1598 * c-basic-offset: 8
1599 * End:
1600 */