blob: 7c1c92d68231ce8267a45abc168b808937185730 [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 Tarreau34db1082012-04-19 17:16:54 +020031#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/backend.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020033#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020034#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010035#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020036#include <proto/lb_fwlc.h>
37#include <proto/lb_fwrr.h>
38#include <proto/lb_map.h>
Willy Tarreau26d8c592012-05-07 18:12:14 +020039#include <proto/protocols.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020041#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010043#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020044#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010045#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020046#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/task.h>
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049/*
50 * This function recounts the number of usable active and backup servers for
51 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010052 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010053 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010054 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020056void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020057{
58 struct server *srv;
59
Willy Tarreau20697042007-11-15 23:26:18 +010060 px->srv_act = px->srv_bck = 0;
61 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010062 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010064 if (!srv_is_usable(srv->state, srv->eweight))
65 continue;
66
67 if (srv->state & SRV_BACKUP) {
68 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010069 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010070 px->lbprm.fbck = srv;
71 px->srv_bck++;
72 px->lbprm.tot_wbck += srv->eweight;
73 } else {
74 px->srv_act++;
75 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 }
77 }
Willy Tarreaub625a082007-11-26 01:15:43 +010078}
Willy Tarreau20697042007-11-15 23:26:18 +010079
Willy Tarreaub625a082007-11-26 01:15:43 +010080/* This function simply updates the backend's tot_weight and tot_used values
81 * after servers weights have been updated. It is designed to be used after
82 * recount_servers() or equivalent.
83 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020084void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010085{
Willy Tarreau20697042007-11-15 23:26:18 +010086 if (px->srv_act) {
87 px->lbprm.tot_weight = px->lbprm.tot_wact;
88 px->lbprm.tot_used = px->srv_act;
89 }
Willy Tarreaub625a082007-11-26 01:15:43 +010090 else if (px->lbprm.fbck) {
91 /* use only the first backup server */
92 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
93 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010094 }
95 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010096 px->lbprm.tot_weight = px->lbprm.tot_wbck;
97 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010098 }
Willy Tarreaub625a082007-11-26 01:15:43 +010099}
100
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200101/*
102 * This function tries to find a running server for the proxy <px> following
103 * the source hash method. Depending on the number of active/backup servers,
104 * it will either look for active servers, or for backup servers.
105 * If any server is found, it will be returned. If no valid server is found,
106 * NULL is returned.
107 */
108struct server *get_server_sh(struct proxy *px, const char *addr, int len)
109{
110 unsigned int h, l;
111
112 if (px->lbprm.tot_weight == 0)
113 return NULL;
114
115 l = h = 0;
116
117 /* note: we won't hash if there's only one server left */
118 if (px->lbprm.tot_used == 1)
119 goto hash_done;
120
121 while ((l + sizeof (int)) <= len) {
122 h ^= ntohl(*(unsigned int *)(&addr[l]));
123 l += sizeof (int);
124 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100125 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
126 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200127 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200128 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
129 return chash_get_server_hash(px, h);
130 else
131 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200132}
133
134/*
135 * This function tries to find a running server for the proxy <px> following
136 * the URI hash method. In order to optimize cache hits, the hash computation
137 * ends at the question mark. Depending on the number of active/backup servers,
138 * it will either look for active servers, or for backup servers.
139 * If any server is found, it will be returned. If no valid server is found,
140 * NULL is returned.
141 *
142 * This code was contributed by Guillaume Dallaire, who also selected this hash
143 * algorithm out of a tens because it gave him the best results.
144 *
145 */
146struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
147{
148 unsigned long hash = 0;
149 int c;
150 int slashes = 0;
151
152 if (px->lbprm.tot_weight == 0)
153 return NULL;
154
155 /* note: we won't hash if there's only one server left */
156 if (px->lbprm.tot_used == 1)
157 goto hash_done;
158
159 if (px->uri_len_limit)
160 uri_len = MIN(uri_len, px->uri_len_limit);
161
162 while (uri_len--) {
163 c = *uri++;
164 if (c == '/') {
165 slashes++;
166 if (slashes == px->uri_dirs_depth1) /* depth+1 */
167 break;
168 }
169 else if (c == '?')
170 break;
171
172 hash = c + (hash << 6) + (hash << 16) - hash;
173 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100174 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
175 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200176 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200177 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
178 return chash_get_server_hash(px, hash);
179 else
180 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200181}
182
Willy Tarreau01732802007-11-01 22:48:15 +0100183/*
184 * This function tries to find a running server for the proxy <px> following
185 * the URL parameter hash method. It looks for a specific parameter in the
186 * URL and hashes it to compute the server ID. This is useful to optimize
187 * performance by avoiding bounces between servers in contexts where sessions
188 * are shared but cookies are not usable. If the parameter is not found, NULL
189 * is returned. If any server is found, it will be returned. If no valid server
190 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100191 */
192struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
193{
194 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200195 const char *p;
196 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100197 int plen;
198
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200199 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100200 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100201 return NULL;
202
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200203 if ((p = memchr(uri, '?', uri_len)) == NULL)
204 return NULL;
205
Willy Tarreau01732802007-11-01 22:48:15 +0100206 p++;
207
208 uri_len -= (p - uri);
209 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200210 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100211
212 while (uri_len > plen) {
213 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200214 if (params[plen] == '=') {
215 if (memcmp(params, px->url_param_name, plen) == 0) {
216 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100217 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200218 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100219 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200220 p += plen + 1;
221 uri_len -= plen + 1;
222
Willy Tarreau01732802007-11-01 22:48:15 +0100223 while (uri_len && *p != '&') {
224 hash = *p + (hash << 6) + (hash << 16) - hash;
225 uri_len--;
226 p++;
227 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100228 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
229 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200230 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
231 return chash_get_server_hash(px, hash);
232 else
233 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100234 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200235 }
236 /* skip to next parameter */
237 p = memchr(params, '&', uri_len);
238 if (!p)
239 return NULL;
240 p++;
241 uri_len -= (p - params);
242 params = p;
243 }
244 return NULL;
245}
246
247/*
248 * this does the same as the previous server_ph, but check the body contents
249 */
250struct server *get_server_ph_post(struct session *s)
251{
252 unsigned long hash = 0;
253 struct http_txn *txn = &s->txn;
254 struct buffer *req = s->req;
255 struct http_msg *msg = &txn->req;
256 struct proxy *px = s->be;
257 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100258 unsigned long len = msg->body_len;
Willy Tarreauea1175a2012-03-05 15:52:30 +0100259 const char *params = req->p + msg->sov;
Willy Tarreau157dd632009-12-06 19:18:09 +0100260 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100262 if (len > req->i - (msg->sov - msg->som))
263 len = req->i - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266 return NULL;
267
Willy Tarreau157dd632009-12-06 19:18:09 +0100268 if (px->lbprm.tot_weight == 0)
269 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200270
271 while (len > plen) {
272 /* Look for the parameter name followed by an equal symbol */
273 if (params[plen] == '=') {
274 if (memcmp(params, px->url_param_name, plen) == 0) {
275 /* OK, we have the parameter here at <params>, and
276 * the value after the equal sign, at <p>
277 * skip the equal symbol
278 */
279 p += plen + 1;
280 len -= plen + 1;
281
282 while (len && *p != '&') {
283 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100284 /* if in a POST, body must be URI encoded or it's not a URI.
285 * Do not interprete any possible binary data as a parameter.
286 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
288 break;
289 return NULL; /* oh, no; this is not uri-encoded.
290 * This body does not contain parameters.
291 */
292 }
293 hash = *p + (hash << 6) + (hash << 16) - hash;
294 len--;
295 p++;
296 /* should we break if vlen exceeds limit? */
297 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100298 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
299 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200300 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
301 return chash_get_server_hash(px, hash);
302 else
303 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304 }
305 }
Willy Tarreau01732802007-11-01 22:48:15 +0100306 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100308 if (!p)
309 return NULL;
310 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 len -= (p - params);
312 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100313 }
314 return NULL;
315}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318/*
Benoitaffb4812009-03-25 13:02:10 +0100319 * This function tries to find a running server for the proxy <px> following
320 * the Header parameter hash method. It looks for a specific parameter in the
321 * URL and hashes it to compute the server ID. This is useful to optimize
322 * performance by avoiding bounces between servers in contexts where sessions
323 * are shared but cookies are not usable. If the parameter is not found, NULL
324 * is returned. If any server is found, it will be returned. If no valid server
325 * is found, NULL is returned.
326 */
327struct server *get_server_hh(struct session *s)
328{
329 unsigned long hash = 0;
330 struct http_txn *txn = &s->txn;
331 struct http_msg *msg = &txn->req;
332 struct proxy *px = s->be;
333 unsigned int plen = px->hh_len;
334 unsigned long len;
335 struct hdr_ctx ctx;
336 const char *p;
337
338 /* tot_weight appears to mean srv_count */
339 if (px->lbprm.tot_weight == 0)
340 return NULL;
341
Benoitaffb4812009-03-25 13:02:10 +0100342 ctx.idx = 0;
343
344 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau3a215be2012-03-09 21:39:51 +0100345 http_find_header2(px->hh_name, plen, s->req->p + msg->sol, &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100346
347 /* if the header is not found or empty, let's fallback to round robin */
348 if (!ctx.idx || !ctx.vlen)
349 return NULL;
350
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200351 /* note: we won't hash if there's only one server left */
352 if (px->lbprm.tot_used == 1)
353 goto hash_done;
354
Benoitaffb4812009-03-25 13:02:10 +0100355 /* Found a the hh_name in the headers.
356 * we will compute the hash based on this value ctx.val.
357 */
358 len = ctx.vlen;
359 p = (char *)ctx.line + ctx.val;
360 if (!px->hh_match_domain) {
361 while (len) {
362 hash = *p + (hash << 6) + (hash << 16) - hash;
363 len--;
364 p++;
365 }
366 } else {
367 int dohash = 0;
368 p += len - 1;
369 /* special computation, use only main domain name, not tld/host
370 * going back from the end of string, start hashing at first
371 * dot stop at next.
372 * This is designed to work with the 'Host' header, and requires
373 * a special option to activate this.
374 */
375 while (len) {
376 if (*p == '.') {
377 if (!dohash)
378 dohash = 1;
379 else
380 break;
381 } else {
382 if (dohash)
383 hash = *p + (hash << 6) + (hash << 16) - hash;
384 }
385 len--;
386 p--;
387 }
388 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100389 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
390 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200391 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200392 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
393 return chash_get_server_hash(px, hash);
394 else
395 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100396}
397
Willy Tarreau34db1082012-04-19 17:16:54 +0200398/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200399struct server *get_server_rch(struct session *s)
400{
401 unsigned long hash = 0;
402 struct proxy *px = s->be;
403 unsigned long len;
404 const char *p;
405 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200406 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200407 struct arg args[2];
Emeric Brun736aa232009-06-30 17:56:00 +0200408
409 /* tot_weight appears to mean srv_count */
410 if (px->lbprm.tot_weight == 0)
411 return NULL;
412
Willy Tarreau37406352012-04-23 16:16:37 +0200413 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200414
Willy Tarreau34db1082012-04-19 17:16:54 +0200415 args[0].type = ARGT_STR;
416 args[0].data.str.str = px->hh_name;
417 args[0].data.str.len = px->hh_len;
418 args[1].type = ARGT_STOP;
419
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200420 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200421 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100422
Willy Tarreau37406352012-04-23 16:16:37 +0200423 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200424 return NULL;
425
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200426 /* note: we won't hash if there's only one server left */
427 if (px->lbprm.tot_used == 1)
428 goto hash_done;
429
Emeric Brun736aa232009-06-30 17:56:00 +0200430 /* Found a the hh_name in the headers.
431 * we will compute the hash based on this value ctx.val.
432 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200433 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200434 while (len) {
435 hash = *p + (hash << 6) + (hash << 16) - hash;
436 len--;
437 p++;
438 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100439 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
440 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200441 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200442 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
443 return chash_get_server_hash(px, hash);
444 else
445 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200446}
Benoitaffb4812009-03-25 13:02:10 +0100447
448/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200449 * This function applies the load-balancing algorithm to the session, as
450 * defined by the backend it is assigned to. The session is then marked as
451 * 'assigned'.
452 *
453 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
454 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100455 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 * The function tries to keep the original connection slot if it reconnects to
457 * the same server, otherwise it releases it and tries to offer it.
458 *
459 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 *
461 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100462 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
464 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 * SRV_STATUS_INTERNAL for other unrecoverable errors.
466 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200467 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100468 * it does not need to be called anymore. This means that target_srv(&s->target)
469 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 *
471 */
472
473int assign_server(struct session *s)
474{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100475
Willy Tarreau7c669d72008-06-20 15:04:11 +0200476 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100477 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200478 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100479
Simon Horman8effd3d2011-08-13 08:03:52 +0900480 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 err = SRV_STATUS_INTERNAL;
483 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
484 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100485
Willy Tarreau827aee92011-03-10 16:55:02 +0100486 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau7c669d72008-06-20 15:04:11 +0200489 /* We have to release any connection slot before applying any LB algo,
490 * otherwise we may erroneously end up with no available slot.
491 */
492 if (conn_slot)
493 sess_change_server(s, NULL);
494
Willy Tarreau827aee92011-03-10 16:55:02 +0100495 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
496 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 * as well as if no server is available (check error code).
498 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100499
Willy Tarreau827aee92011-03-10 16:55:02 +0100500 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100501 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100502
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200503 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 /* we must check if we have at least one server available */
505 if (!s->be->lbprm.tot_weight) {
506 err = SRV_STATUS_NOSRV;
507 goto out;
508 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200510 /* First check whether we need to fetch some data or simply call
511 * the LB lookup function. Only the hashing functions will need
512 * some input data in fact, and will support multiple algorithms.
513 */
514 switch (s->be->lbprm.algo & BE_LB_LKUP) {
515 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100516 srv = fwrr_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 Tarreauf09c6602012-02-13 17:12:08 +0100519 case BE_LB_LKUP_FSTREE:
520 srv = fas_get_next_server(s->be, prev_srv);
521 break;
522
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200523 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100524 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200525 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200526
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200527 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200528 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200529 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200530 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100531 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200532 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100533 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200534 break;
535 }
536 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200538 err = SRV_STATUS_INTERNAL;
539 goto out;
540 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200541
542 switch (s->be->lbprm.algo & BE_LB_PARM) {
543 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200544 if (s->req->prod->addr.from.ss_family == AF_INET) {
545 srv = get_server_sh(s->be,
546 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
547 4);
548 }
549 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
550 srv = get_server_sh(s->be,
551 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
552 16);
553 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554 else {
555 /* unknown IP family */
556 err = SRV_STATUS_INTERNAL;
557 goto out;
558 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200559 break;
560
561 case BE_LB_HASH_URI:
562 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100563 if (s->txn.req.msg_state < HTTP_MSG_BODY)
564 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100565 srv = get_server_uh(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100566 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100567 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200568 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200569
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200570 case BE_LB_HASH_PRM:
571 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100572 if (s->txn.req.msg_state < HTTP_MSG_BODY)
573 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100574
Willy Tarreau827aee92011-03-10 16:55:02 +0100575 srv = get_server_ph(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100576 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100577 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100578
Willy Tarreau827aee92011-03-10 16:55:02 +0100579 if (!srv && s->txn.meth == HTTP_METH_POST)
580 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200581 break;
Benoitaffb4812009-03-25 13:02:10 +0100582
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583 case BE_LB_HASH_HDR:
584 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100585 if (s->txn.req.msg_state < HTTP_MSG_BODY)
586 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100587 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 break;
589
590 case BE_LB_HASH_RDP:
591 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593 break;
594
595 default:
596 /* unknown balancing algorithm */
597 err = SRV_STATUS_INTERNAL;
598 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100599 }
Emeric Brun736aa232009-06-30 17:56:00 +0200600
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601 /* If the hashing parameter was not found, let's fall
602 * back to round robin on the map.
603 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100604 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200607 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200609 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200610
611 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200612 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613
Willy Tarreau7c669d72008-06-20 15:04:11 +0200614 default:
615 /* unknown balancing algorithm */
616 err = SRV_STATUS_INTERNAL;
617 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
Willy Tarreau827aee92011-03-10 16:55:02 +0100620 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200621 err = SRV_STATUS_FULL;
622 goto out;
623 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100624 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100625 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100626 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100627 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100628 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200629 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200630 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100631 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100633 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200634 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100635 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100636 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100637 }
638 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200639 err = SRV_STATUS_NOSRV;
640 goto out;
641 }
642
643 s->flags |= SN_ASSIGNED;
644 err = SRV_STATUS_OK;
645 out:
646
647 /* Either we take back our connection slot, or we offer it to someone
648 * else if we don't need it anymore.
649 */
650 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 if (conn_slot == srv) {
652 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200653 } else {
654 if (may_dequeue_tasks(conn_slot, s->be))
655 process_srv_queue(conn_slot);
656 }
657 }
658
659 out_err:
660 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661}
662
663
664/*
665 * This function assigns a server address to a session, and sets SN_ADDR_SET.
666 * The address is taken from the currently assigned server, or from the
667 * dispatch or transparent address.
668 *
669 * It may return :
670 * SRV_STATUS_OK if everything is OK.
671 * SRV_STATUS_INTERNAL for other unrecoverable errors.
672 *
673 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
674 * not cleared, so it's to the caller to clear it if required.
675 *
676 */
677int assign_server_address(struct session *s)
678{
679#ifdef DEBUG_FULL
680 fprintf(stderr,"assign_server_address : s=%p\n",s);
681#endif
682
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200683 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 /* A server is necessarily known for this session */
685 if (!(s->flags & SN_ASSIGNED))
686 return SRV_STATUS_INTERNAL;
687
Willy Tarreau6471afb2011-09-23 10:54:59 +0200688 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689
Willy Tarreau6471afb2011-09-23 10:54:59 +0200690 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200691 /* if the server has no address, we use the same address
692 * the client asked, which is handy for remapping ports
693 * locally on multiple addresses at once.
694 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200695 if (!(s->be->options & PR_O_TRANSP))
696 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200697
Willy Tarreau6471afb2011-09-23 10:54:59 +0200698 if (s->req->prod->addr.to.ss_family == AF_INET) {
699 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
700 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
701 ((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 +0200702 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200703 }
704
Willy Tarreaubaaee002006-06-26 02:48:02 +0200705 /* if this server remaps proxied ports, we'll use
706 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100707 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100708 int base_port;
709
Willy Tarreau9b061e32012-04-07 18:03:52 +0200710 if (!(s->be->options & PR_O_TRANSP))
711 stream_sock_get_to_addr(s->req->prod);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100712
713 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200714 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715
716 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200717 base_port += get_host_port(&s->req->cons->addr.to);
718 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 }
720 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200721 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200723 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100725 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200727 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728
Willy Tarreau6471afb2011-09-23 10:54:59 +0200729 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
730 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 +0200731 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100732 /* when we support IPv6 on the backend, we may add other tests */
733 //qfprintf(stderr, "Cannot get original server address.\n");
734 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100736 else if (s->be->options & PR_O_HTTP_PROXY) {
737 /* If HTTP PROXY option is set, then server is already assigned
738 * during incoming client request parsing. */
739 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100740 else {
741 /* no server and no LB algorithm ! */
742 return SRV_STATUS_INTERNAL;
743 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744
745 s->flags |= SN_ADDR_SET;
746 return SRV_STATUS_OK;
747}
748
749
750/* This function assigns a server to session <s> if required, and can add the
751 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200752 * If ->srv_conn is set, the session is first released from the server.
753 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
754 * be called before any connection and after any retry or redispatch occurs.
755 *
756 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 *
758 * Returns :
759 *
760 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 * SRV_STATUS_QUEUED if the connection has been queued.
763 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200765 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 * SRV_STATUS_INTERNAL for other unrecoverable errors.
767 *
768 */
769int assign_server_and_queue(struct session *s)
770{
771 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100772 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 int err;
774
775 if (s->pend_pos)
776 return SRV_STATUS_INTERNAL;
777
Willy Tarreau7c669d72008-06-20 15:04:11 +0200778 err = SRV_STATUS_OK;
779 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100781
Willy Tarreau7c669d72008-06-20 15:04:11 +0200782 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100783 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200784 /* This session was previously assigned to a server. We have to
785 * update the session's and the server's stats :
786 * - if the server changed :
787 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
788 * - set SN_REDISP if it was successfully redispatched
789 * - increment srv->redispatches and be->redispatches
790 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100791 */
792
Willy Tarreau827aee92011-03-10 16:55:02 +0100793 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200794 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
795 s->txn.flags &= ~TX_CK_MASK;
796 s->txn.flags |= TX_CK_DOWN;
797 }
798 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100799 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100800 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200801 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100802 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100803 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100804 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100805 }
806 }
807
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 switch (err) {
809 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 srv = target_srv(&s->target);
812 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 return SRV_STATUS_OK; /* dispatch or proxy mode */
814
815 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200817 return SRV_STATUS_OK;
818
819 /* OK, this session already has an assigned server, but no
820 * connection slot yet. Either it is a redispatch, or it was
821 * assigned from persistence information (direct mode).
822 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100823 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200824 /* server scheduled for redirection, and already assigned. We
825 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100826 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100828 return SRV_STATUS_OK;
829 }
830
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 /* We might have to queue this session if the assigned server is full.
832 * We know we have to queue it into the server's queue, so if a maxqueue
833 * is set on the server, we must also check that the server's queue is
834 * not full, in which case we have to return FULL.
835 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100836 if (srv->maxconn &&
837 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838
Willy Tarreau827aee92011-03-10 16:55:02 +0100839 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200840 return SRV_STATUS_FULL;
841
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842 p = pendconn_add(s);
843 if (p)
844 return SRV_STATUS_QUEUED;
845 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848
849 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100850 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 return SRV_STATUS_OK;
852
853 case SRV_STATUS_FULL:
854 /* queue this session into the proxy's queue */
855 p = pendconn_add(s);
856 if (p)
857 return SRV_STATUS_QUEUED;
858 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200859 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200860
861 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862 return err;
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 case SRV_STATUS_INTERNAL:
865 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 default:
868 return SRV_STATUS_INTERNAL;
869 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100870}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871
Willy Tarreaub1d67742010-03-29 19:36:59 +0200872/* If an explicit source binding is specified on the server and/or backend, and
873 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200874 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875 */
876static void assign_tproxy_address(struct session *s)
877{
878#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100879 struct server *srv = target_srv(&s->target);
880
881 if (srv && srv->state & SRV_BIND_SRC) {
882 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200883 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200884 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200885 break;
886 case SRV_TPROXY_CLI:
887 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200888 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200889 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200890 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200891 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100892 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100893 char *vptr;
894 int vlen;
895
Willy Tarreaubce70882009-09-07 11:51:47 +0200896 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100897 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200898 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100899 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
900
901 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
902 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
903 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
904 htonl(inetaddr_host_lim(vptr, vptr + vlen));
905 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200906 }
907 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200908 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200909 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200910 }
911 }
912 else if (s->be->options & PR_O_BIND_SRC) {
913 switch (s->be->options & PR_O_TPXY_MASK) {
914 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200915 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200916 break;
917 case PR_O_TPXY_CLI:
918 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200919 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200920 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200921 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200922 case PR_O_TPXY_DYN:
923 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100924 char *vptr;
925 int vlen;
926
Willy Tarreaubce70882009-09-07 11:51:47 +0200927 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100928 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200929 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100930 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
931
932 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
933 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
934 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
935 htonl(inetaddr_host_lim(vptr, vptr + vlen));
936 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200937 }
938 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200939 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200940 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200941 }
942 }
943#endif
944}
945
946
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947/*
948 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200949 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100950 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 * It can return one of :
952 * - SN_ERR_NONE if everything's OK
953 * - SN_ERR_SRVTO if there are no more servers
954 * - SN_ERR_SRVCL if the connection was refused by the server
955 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
956 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
957 * - SN_ERR_INTERNAL for any other purely internal errors
958 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
959 */
960int connect_server(struct session *s)
961{
Willy Tarreau827aee92011-03-10 16:55:02 +0100962 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200963 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200964
965 if (!(s->flags & SN_ADDR_SET)) {
966 err = assign_server_address(s);
967 if (err != SRV_STATUS_OK)
968 return SN_ERR_INTERNAL;
969 }
970
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200971 /* Prepare the stream interface for a TCP connection. Later
972 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100973 * NOTE: when we later support HTTP keep-alive, we'll have to
974 * decide here if we can reuse the connection by comparing the
975 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200976 */
Willy Tarreau5c979a92012-05-07 17:15:39 +0200977 stream_interface_prepare(s->req->cons, &stream_sock);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200978
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200979 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100980 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200981
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100982 /* process the case where the server requires the PROXY protocol to be sent */
983 s->req->cons->send_proxy_ofs = 0;
984 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
985 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200986 stream_sock_get_to_addr(s->req->prod);
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100987 }
988
Willy Tarreau26d8c592012-05-07 18:12:14 +0200989 /* set the correct protocol on the output stream interface */
990 if (s->target.type == TARG_TYPE_SERVER)
991 s->req->cons->proto = target_srv(&s->target)->proto;
992 else if (s->target.type == TARG_TYPE_PROXY) {
993 s->req->cons->proto = protocol_by_family(s->req->cons->addr.to.ss_family);
994 if (!s->req->cons->proto)
995 return SN_ERR_INTERNAL;
996 }
997
Willy Tarreaub1d67742010-03-29 19:36:59 +0200998 assign_tproxy_address(s);
999
William Lallemandb7ff6a32012-03-02 14:35:21 +01001000 /* flag for logging source ip/port */
1001 if (s->fe->options2 & PR_O2_SRC_ADDR)
1002 s->req->cons->flags |= SI_FL_SRC_ADDR;
1003
Willy Tarreau26d8c592012-05-07 18:12:14 +02001004 err = s->req->cons->proto->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001005
Willy Tarreau9650f372009-08-16 14:02:45 +02001006 if (err != SN_ERR_NONE)
1007 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001008
Willy Tarreau827aee92011-03-10 16:55:02 +01001009 srv = target_srv(&s->target);
1010 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001011 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001012 srv->cur_sess++;
1013 if (srv->cur_sess > srv->counters.cur_sess_max)
1014 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001015 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001016 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 }
1018
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 return SN_ERR_NONE; /* connection is OK */
1020}
1021
1022
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023/* This function performs the "redispatch" part of a connection attempt. It
1024 * will assign a server if required, queue the connection if required, and
1025 * handle errors that might arise at this level. It can change the server
1026 * state. It will return 1 if it encounters an error, switches the server
1027 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1028 * that the connection is ready to use.
1029 */
1030
1031int srv_redispatch_connect(struct session *t)
1032{
Willy Tarreau827aee92011-03-10 16:55:02 +01001033 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 int conn_err;
1035
1036 /* We know that we don't have any connection pending, so we will
1037 * try to get a new one, and wait in this state if it's queued
1038 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001039 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001041 srv = target_srv(&t->target);
1042
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 switch (conn_err) {
1044 case SRV_STATUS_OK:
1045 break;
1046
Willy Tarreau7c669d72008-06-20 15:04:11 +02001047 case SRV_STATUS_FULL:
1048 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1049 * and we can redispatch to another server, or it is not and we return
1050 * 503. This only makes sense in DIRECT mode however, because normal LB
1051 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001052 * would bring us on the same server again. Note that t->target is set
1053 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001054 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001055 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1056 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001057 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001058 goto redispatch;
1059 }
1060
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001061 if (!t->req->cons->err_type) {
1062 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001063 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001064 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001065
Willy Tarreau827aee92011-03-10 16:55:02 +01001066 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001067 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001068 return 1;
1069
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001071 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001072 if (!t->req->cons->err_type) {
1073 t->req->cons->err_type = SI_ET_CONN_ERR;
1074 t->req->cons->err_loc = NULL;
1075 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001076
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001077 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001078 return 1;
1079
1080 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001081 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001082 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001083 /* do nothing else and do not wake any other session up */
1084 return 1;
1085
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086 case SRV_STATUS_INTERNAL:
1087 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001088 if (!t->req->cons->err_type) {
1089 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001090 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001091 }
1092
Willy Tarreau827aee92011-03-10 16:55:02 +01001093 if (srv)
1094 srv_inc_sess_ctr(srv);
1095 if (srv)
1096 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001097 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098
1099 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001100 if (may_dequeue_tasks(srv, t->be))
1101 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102 return 1;
1103 }
1104 /* if we get here, it's because we got SRV_STATUS_OK, which also
1105 * means that the connection has not been queued.
1106 */
1107 return 0;
1108}
1109
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001110/* Apply RDP cookie persistence to the current session. For this, the function
1111 * tries to extract an RDP cookie from the request buffer, and look for the
1112 * matching server in the list. If the server is found, it is assigned to the
1113 * session. This always returns 1, and the analyser removes itself from the
1114 * list. Nothing is performed if a server was already assigned.
1115 */
1116int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1117{
1118 struct proxy *px = s->be;
1119 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001120 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001121 struct server *srv = px->srv;
1122 struct sockaddr_in addr;
1123 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001124 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001125
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001126 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001127 now_ms, __FUNCTION__,
1128 s,
1129 req,
1130 req->rex, req->wex,
1131 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001132 req->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001133 req->analysers);
1134
1135 if (s->flags & SN_ASSIGNED)
1136 goto no_cookie;
1137
Willy Tarreau37406352012-04-23 16:16:37 +02001138 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001139
Willy Tarreau34db1082012-04-19 17:16:54 +02001140 args[0].type = ARGT_STR;
1141 args[0].data.str.str = s->be->rdp_cookie_name;
1142 args[0].data.str.len = s->be->rdp_cookie_len;
1143 args[1].type = ARGT_STOP;
1144
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001145 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001146 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001147 goto no_cookie;
1148
1149 memset(&addr, 0, sizeof(addr));
1150 addr.sin_family = AF_INET;
1151
Willy Tarreau664092c2011-12-16 19:11:42 +01001152 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001153 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001154 if (*p != '.')
1155 goto no_cookie;
1156 p++;
1157 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1158 if (*p != '.')
1159 goto no_cookie;
1160
Willy Tarreau9e000c62011-03-10 14:03:36 +01001161 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001162 while (srv) {
1163 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1164 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1165 /* we found the server and it is usable */
1166 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001167 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001168 break;
1169 }
1170 }
1171 srv = srv->next;
1172 }
1173
1174no_cookie:
1175 req->analysers &= ~an_bit;
1176 req->analyse_exp = TICK_ETERNITY;
1177 return 1;
1178}
1179
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001180int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001181 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001182 return px->down_time;
1183
1184 return now.tv_sec - px->last_change + px->down_time;
1185}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001187/*
1188 * This function returns a string containing the balancing
1189 * mode of the proxy in a format suitable for stats.
1190 */
1191
1192const char *backend_lb_algo_str(int algo) {
1193
1194 if (algo == BE_LB_ALGO_RR)
1195 return "roundrobin";
1196 else if (algo == BE_LB_ALGO_SRR)
1197 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001198 else if (algo == BE_LB_ALGO_FAS)
1199 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001200 else if (algo == BE_LB_ALGO_LC)
1201 return "leastconn";
1202 else if (algo == BE_LB_ALGO_SH)
1203 return "source";
1204 else if (algo == BE_LB_ALGO_UH)
1205 return "uri";
1206 else if (algo == BE_LB_ALGO_PH)
1207 return "url_param";
1208 else if (algo == BE_LB_ALGO_HH)
1209 return "hdr";
1210 else if (algo == BE_LB_ALGO_RCH)
1211 return "rdp-cookie";
1212 else
1213 return NULL;
1214}
1215
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001216/* This function parses a "balance" statement in a backend section describing
1217 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001218 * returns -1, it will write an error message into the <err> buffer which will
1219 * automatically be allocated and must be passed as NULL. The trailing '\n'
1220 * will not be written. The function must be called with <args> pointing to the
1221 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001222 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001223int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001224{
1225 if (!*(args[0])) {
1226 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001227 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1228 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001229 return 0;
1230 }
1231
1232 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001235 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001236 else if (!strcmp(args[0], "static-rr")) {
1237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1239 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001240 else if (!strcmp(args[0], "first")) {
1241 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1242 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1243 }
Willy Tarreau51406232008-03-10 22:04:20 +01001244 else if (!strcmp(args[0], "leastconn")) {
1245 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1246 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1247 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001248 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001249 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1250 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001251 }
1252 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001253 int arg = 1;
1254
Willy Tarreau31682232007-11-29 15:38:04 +01001255 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1256 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001257
1258 while (*args[arg]) {
1259 if (!strcmp(args[arg], "len")) {
1260 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001261 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001262 return -1;
1263 }
1264 curproxy->uri_len_limit = atoi(args[arg+1]);
1265 arg += 2;
1266 }
1267 else if (!strcmp(args[arg], "depth")) {
1268 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001269 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001270 return -1;
1271 }
1272 /* hint: we store the position of the ending '/' (depth+1) so
1273 * that we avoid a comparison while computing the hash.
1274 */
1275 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1276 arg += 2;
1277 }
1278 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001279 memprintf(err, "%s only accepts parameters 'len' and 'depth' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001280 return -1;
1281 }
1282 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001283 }
Willy Tarreau01732802007-11-01 22:48:15 +01001284 else if (!strcmp(args[0], "url_param")) {
1285 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001286 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001287 return -1;
1288 }
Willy Tarreau31682232007-11-29 15:38:04 +01001289 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1290 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001291
1292 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001293 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001294 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001295 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001296 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001297 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001298 return -1;
1299 }
1300 if (*args[3]) {
1301 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1302 curproxy->url_param_post_limit = str2ui(args[3]);
1303 }
1304 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1305 if (!curproxy->url_param_post_limit)
1306 curproxy->url_param_post_limit = 48;
1307 else if ( curproxy->url_param_post_limit < 3 )
1308 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1309 }
Benoitaffb4812009-03-25 13:02:10 +01001310 }
1311 else if (!strncmp(args[0], "hdr(", 4)) {
1312 const char *beg, *end;
1313
1314 beg = args[0] + 4;
1315 end = strchr(beg, ')');
1316
1317 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001318 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001319 return -1;
1320 }
1321
1322 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1323 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1324
1325 free(curproxy->hh_name);
1326 curproxy->hh_len = end - beg;
1327 curproxy->hh_name = my_strndup(beg, end - beg);
1328 curproxy->hh_match_domain = 0;
1329
1330 if (*args[1]) {
1331 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001332 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001333 return -1;
1334 }
1335 curproxy->hh_match_domain = 1;
1336 }
1337
Emeric Brun736aa232009-06-30 17:56:00 +02001338 }
1339 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1340 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1341 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1342
1343 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1344 const char *beg, *end;
1345
1346 beg = args[0] + 11;
1347 end = strchr(beg, ')');
1348
1349 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001350 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001351 return -1;
1352 }
1353
1354 free(curproxy->hh_name);
1355 curproxy->hh_name = my_strndup(beg, end - beg);
1356 curproxy->hh_len = end - beg;
1357 }
1358 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1359 free(curproxy->hh_name);
1360 curproxy->hh_name = strdup("mstshash");
1361 curproxy->hh_len = strlen(curproxy->hh_name);
1362 }
1363 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001364 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001365 return -1;
1366 }
Willy Tarreau01732802007-11-01 22:48:15 +01001367 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001368 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001369 memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001370 return -1;
1371 }
1372 return 0;
1373}
1374
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001375
1376/************************************************************************/
1377/* All supported keywords must be declared here. */
1378/************************************************************************/
1379
Willy Tarreau34db1082012-04-19 17:16:54 +02001380/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001381 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001382 * undefined behaviour.
1383 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001384static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001385acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001386 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001387{
Willy Tarreau37406352012-04-23 16:16:37 +02001388 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001389 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001390 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001391
1392 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001393 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001394 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001395 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001396 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001397 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001398
1399 return 1;
1400}
1401
Willy Tarreau37406352012-04-23 16:16:37 +02001402/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001403 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001404 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1405 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001406 */
1407static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001408acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001409 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001410{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001411 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001412
Willy Tarreau37406352012-04-23 16:16:37 +02001413 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001414 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001415 if (!(srv->state & SRV_MAINTAIN) &&
1416 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001417 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001418 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001419 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001420 return 1;
1421}
1422
Willy Tarreau34db1082012-04-19 17:16:54 +02001423/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001424 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001425 * undefined behaviour.
1426 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001427static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001428acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001429 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001430{
1431 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001432
Willy Tarreau37406352012-04-23 16:16:37 +02001433 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001434 smp->type = SMP_T_UINT;
1435 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001436
Willy Tarreau24e32d82012-04-23 23:55:44 +02001437 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001438 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001439 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001440
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001441 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001442 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001443 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001444 return 1;
1445 }
1446
Willy Tarreauf853c462012-04-23 18:53:56 +02001447 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001448 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001449 }
1450
1451 return 1;
1452}
1453
Willy Tarreaua5e37562011-12-16 17:06:15 +01001454/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001455static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001456acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001457 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001458{
Willy Tarreauf853c462012-04-23 18:53:56 +02001459 smp->flags = SMP_F_VOL_TXN;
1460 smp->type = SMP_T_UINT;
1461 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001462 return 1;
1463}
1464
Willy Tarreaua5e37562011-12-16 17:06:15 +01001465/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001466static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001467acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001468 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001469{
Willy Tarreau827aee92011-03-10 16:55:02 +01001470 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001471 return 0;
1472
Willy Tarreauf853c462012-04-23 18:53:56 +02001473 smp->type = SMP_T_UINT;
1474 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001475
1476 return 1;
1477}
1478
Willy Tarreau34db1082012-04-19 17:16:54 +02001479/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001480 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001481 * undefined behaviour.
1482 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001483static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001484acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001485 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001486{
Willy Tarreau37406352012-04-23 16:16:37 +02001487 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001488 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001489 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001490 return 1;
1491}
1492
Willy Tarreau34db1082012-04-19 17:16:54 +02001493/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001494 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001495 * undefined behaviour.
1496 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001497static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001498acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001499 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001500{
Willy Tarreau37406352012-04-23 16:16:37 +02001501 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001502 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001503 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001504 return 1;
1505}
1506
Willy Tarreau34db1082012-04-19 17:16:54 +02001507/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001508 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001509 * undefined behaviour.
1510 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001511static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001512acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001513 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001514{
Willy Tarreau37406352012-04-23 16:16:37 +02001515 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001516 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001517 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001518 return 1;
1519}
1520
Willy Tarreaua5e37562011-12-16 17:06:15 +01001521/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001522 * by the number of running servers and rounded up. If there is no running
1523 * server, we return twice the total, just as if we had half a running server.
1524 * This is more or less correct anyway, since we expect the last server to come
1525 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001526 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001527 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001528 */
1529static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001530acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001531 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001532{
1533 int nbsrv;
1534
Willy Tarreau37406352012-04-23 16:16:37 +02001535 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001536 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001537 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001538
1539 if (px->srv_act)
1540 nbsrv = px->srv_act;
1541 else if (px->lbprm.fbck)
1542 nbsrv = 1;
1543 else
1544 nbsrv = px->srv_bck;
1545
1546 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001547 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001548 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001549 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001550
1551 return 1;
1552}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001553
Willy Tarreau34db1082012-04-19 17:16:54 +02001554/* set temp integer to the number of concurrent connections on the server in the backend.
1555 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1556 * undefined behaviour.
1557 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001558static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001559acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001560 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001561{
Willy Tarreauf853c462012-04-23 18:53:56 +02001562 smp->flags = SMP_F_VOL_TEST;
1563 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001564 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001565 return 1;
1566}
1567
Willy Tarreau61612d42012-04-19 18:42:05 +02001568/* Note: must not be declared <const> as its list will be overwritten.
1569 * Please take care of keeping this list alphabetically sorted.
1570 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001571static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001572 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1573 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001574 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001575 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1576 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1577 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1578 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001579 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1580 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1581 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING, ARG1(1,SRV) },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001582 { NULL, NULL, NULL, NULL },
1583}};
1584
1585
1586__attribute__((constructor))
1587static void __backend_init(void)
1588{
1589 acl_register_keywords(&acl_kws);
1590}
1591
Willy Tarreaubaaee002006-06-26 02:48:02 +02001592/*
1593 * Local variables:
1594 * c-indent-level: 8
1595 * c-basic-offset: 8
1596 * End:
1597 */