blob: e550d35bc2f39ff9a37021491333b9bbb3e9c611 [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 Tarreauc7e42382012-08-24 19:22:53 +020022#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020025#include <common/debug.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010031#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020032#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020035#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020036#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010037#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020038#include <proto/lb_fwlc.h>
39#include <proto/lb_fwrr.h>
40#include <proto/lb_map.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010041#include <proto/obj_type.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020042#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020044#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010046#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020047#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020048#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010049#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/task.h>
51
Willy Tarreaubaaee002006-06-26 02:48:02 +020052/*
53 * This function recounts the number of usable active and backup servers for
54 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010055 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010056 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010057 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020059void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020060{
61 struct server *srv;
62
Willy Tarreau20697042007-11-15 23:26:18 +010063 px->srv_act = px->srv_bck = 0;
64 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010065 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010067 if (!srv_is_usable(srv->state, srv->eweight))
68 continue;
69
70 if (srv->state & SRV_BACKUP) {
71 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010072 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010073 px->lbprm.fbck = srv;
74 px->srv_bck++;
75 px->lbprm.tot_wbck += srv->eweight;
76 } else {
77 px->srv_act++;
78 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 }
80 }
Willy Tarreaub625a082007-11-26 01:15:43 +010081}
Willy Tarreau20697042007-11-15 23:26:18 +010082
Willy Tarreaub625a082007-11-26 01:15:43 +010083/* This function simply updates the backend's tot_weight and tot_used values
84 * after servers weights have been updated. It is designed to be used after
85 * recount_servers() or equivalent.
86 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020087void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010088{
Willy Tarreau20697042007-11-15 23:26:18 +010089 if (px->srv_act) {
90 px->lbprm.tot_weight = px->lbprm.tot_wact;
91 px->lbprm.tot_used = px->srv_act;
92 }
Willy Tarreaub625a082007-11-26 01:15:43 +010093 else if (px->lbprm.fbck) {
94 /* use only the first backup server */
95 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
96 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010097 }
98 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010099 px->lbprm.tot_weight = px->lbprm.tot_wbck;
100 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100101 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100102}
103
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200104/*
105 * This function tries to find a running server for the proxy <px> following
106 * the source hash method. Depending on the number of active/backup servers,
107 * it will either look for active servers, or for backup servers.
108 * If any server is found, it will be returned. If no valid server is found,
109 * NULL is returned.
110 */
111struct server *get_server_sh(struct proxy *px, const char *addr, int len)
112{
113 unsigned int h, l;
114
115 if (px->lbprm.tot_weight == 0)
116 return NULL;
117
118 l = h = 0;
119
120 /* note: we won't hash if there's only one server left */
121 if (px->lbprm.tot_used == 1)
122 goto hash_done;
123
124 while ((l + sizeof (int)) <= len) {
125 h ^= ntohl(*(unsigned int *)(&addr[l]));
126 l += sizeof (int);
127 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100128 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
129 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200130 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200131 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
132 return chash_get_server_hash(px, h);
133 else
134 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200135}
136
137/*
138 * This function tries to find a running server for the proxy <px> following
139 * the URI hash method. In order to optimize cache hits, the hash computation
140 * ends at the question mark. Depending on the number of active/backup servers,
141 * it will either look for active servers, or for backup servers.
142 * If any server is found, it will be returned. If no valid server is found,
143 * NULL is returned.
144 *
145 * This code was contributed by Guillaume Dallaire, who also selected this hash
146 * algorithm out of a tens because it gave him the best results.
147 *
148 */
149struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
150{
151 unsigned long hash = 0;
152 int c;
153 int slashes = 0;
154
155 if (px->lbprm.tot_weight == 0)
156 return NULL;
157
158 /* note: we won't hash if there's only one server left */
159 if (px->lbprm.tot_used == 1)
160 goto hash_done;
161
162 if (px->uri_len_limit)
163 uri_len = MIN(uri_len, px->uri_len_limit);
164
165 while (uri_len--) {
166 c = *uri++;
167 if (c == '/') {
168 slashes++;
169 if (slashes == px->uri_dirs_depth1) /* depth+1 */
170 break;
171 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100172 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200173 break;
174
175 hash = c + (hash << 6) + (hash << 16) - hash;
176 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100177 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
178 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200179 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200180 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
181 return chash_get_server_hash(px, hash);
182 else
183 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200184}
185
Willy Tarreau01732802007-11-01 22:48:15 +0100186/*
187 * This function tries to find a running server for the proxy <px> following
188 * the URL parameter hash method. It looks for a specific parameter in the
189 * URL and hashes it to compute the server ID. This is useful to optimize
190 * performance by avoiding bounces between servers in contexts where sessions
191 * are shared but cookies are not usable. If the parameter is not found, NULL
192 * is returned. If any server is found, it will be returned. If no valid server
193 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100194 */
195struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
196{
197 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200198 const char *p;
199 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100200 int plen;
201
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200202 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100203 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100204 return NULL;
205
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200206 if ((p = memchr(uri, '?', uri_len)) == NULL)
207 return NULL;
208
Willy Tarreau01732802007-11-01 22:48:15 +0100209 p++;
210
211 uri_len -= (p - uri);
212 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200213 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100214
215 while (uri_len > plen) {
216 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200217 if (params[plen] == '=') {
218 if (memcmp(params, px->url_param_name, plen) == 0) {
219 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100220 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200221 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100222 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200223 p += plen + 1;
224 uri_len -= plen + 1;
225
Willy Tarreau01732802007-11-01 22:48:15 +0100226 while (uri_len && *p != '&') {
227 hash = *p + (hash << 6) + (hash << 16) - hash;
228 uri_len--;
229 p++;
230 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100231 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
232 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200233 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
234 return chash_get_server_hash(px, hash);
235 else
236 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100237 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200238 }
239 /* skip to next parameter */
240 p = memchr(params, '&', uri_len);
241 if (!p)
242 return NULL;
243 p++;
244 uri_len -= (p - params);
245 params = p;
246 }
247 return NULL;
248}
249
250/*
251 * this does the same as the previous server_ph, but check the body contents
252 */
253struct server *get_server_ph_post(struct session *s)
254{
255 unsigned long hash = 0;
256 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200257 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258 struct http_msg *msg = &txn->req;
259 struct proxy *px = s->be;
260 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100261 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200262 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100263 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264
Willy Tarreau9b28e032012-10-12 23:49:43 +0200265 if (len > buffer_len(req->buf) - msg->sov)
266 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267
Willy Tarreau157dd632009-12-06 19:18:09 +0100268 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269 return NULL;
270
Willy Tarreau157dd632009-12-06 19:18:09 +0100271 if (px->lbprm.tot_weight == 0)
272 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200273
274 while (len > plen) {
275 /* Look for the parameter name followed by an equal symbol */
276 if (params[plen] == '=') {
277 if (memcmp(params, px->url_param_name, plen) == 0) {
278 /* OK, we have the parameter here at <params>, and
279 * the value after the equal sign, at <p>
280 * skip the equal symbol
281 */
282 p += plen + 1;
283 len -= plen + 1;
284
285 while (len && *p != '&') {
286 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100287 /* if in a POST, body must be URI encoded or it's not a URI.
288 * Do not interprete any possible binary data as a parameter.
289 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200290 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
291 break;
292 return NULL; /* oh, no; this is not uri-encoded.
293 * This body does not contain parameters.
294 */
295 }
296 hash = *p + (hash << 6) + (hash << 16) - hash;
297 len--;
298 p++;
299 /* should we break if vlen exceeds limit? */
300 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100301 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
302 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200303 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
304 return chash_get_server_hash(px, hash);
305 else
306 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 }
308 }
Willy Tarreau01732802007-11-01 22:48:15 +0100309 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100311 if (!p)
312 return NULL;
313 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200314 len -= (p - params);
315 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100316 }
317 return NULL;
318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200320
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321/*
Benoitaffb4812009-03-25 13:02:10 +0100322 * This function tries to find a running server for the proxy <px> following
323 * the Header parameter hash method. It looks for a specific parameter in the
324 * URL and hashes it to compute the server ID. This is useful to optimize
325 * performance by avoiding bounces between servers in contexts where sessions
326 * are shared but cookies are not usable. If the parameter is not found, NULL
327 * is returned. If any server is found, it will be returned. If no valid server
328 * is found, NULL is returned.
329 */
330struct server *get_server_hh(struct session *s)
331{
332 unsigned long hash = 0;
333 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100334 struct proxy *px = s->be;
335 unsigned int plen = px->hh_len;
336 unsigned long len;
337 struct hdr_ctx ctx;
338 const char *p;
339
340 /* tot_weight appears to mean srv_count */
341 if (px->lbprm.tot_weight == 0)
342 return NULL;
343
Benoitaffb4812009-03-25 13:02:10 +0100344 ctx.idx = 0;
345
346 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200347 http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, (int)-s->req->buf->o), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100348
349 /* if the header is not found or empty, let's fallback to round robin */
350 if (!ctx.idx || !ctx.vlen)
351 return NULL;
352
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200353 /* note: we won't hash if there's only one server left */
354 if (px->lbprm.tot_used == 1)
355 goto hash_done;
356
Benoitaffb4812009-03-25 13:02:10 +0100357 /* Found a the hh_name in the headers.
358 * we will compute the hash based on this value ctx.val.
359 */
360 len = ctx.vlen;
361 p = (char *)ctx.line + ctx.val;
362 if (!px->hh_match_domain) {
363 while (len) {
364 hash = *p + (hash << 6) + (hash << 16) - hash;
365 len--;
366 p++;
367 }
368 } else {
369 int dohash = 0;
370 p += len - 1;
371 /* special computation, use only main domain name, not tld/host
372 * going back from the end of string, start hashing at first
373 * dot stop at next.
374 * This is designed to work with the 'Host' header, and requires
375 * a special option to activate this.
376 */
377 while (len) {
378 if (*p == '.') {
379 if (!dohash)
380 dohash = 1;
381 else
382 break;
383 } else {
384 if (dohash)
385 hash = *p + (hash << 6) + (hash << 16) - hash;
386 }
387 len--;
388 p--;
389 }
390 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100391 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
392 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200393 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200394 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
395 return chash_get_server_hash(px, hash);
396 else
397 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100398}
399
Willy Tarreau34db1082012-04-19 17:16:54 +0200400/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200401struct server *get_server_rch(struct session *s)
402{
403 unsigned long hash = 0;
404 struct proxy *px = s->be;
405 unsigned long len;
406 const char *p;
407 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200408 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200409 struct arg args[2];
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200410 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200411
412 /* tot_weight appears to mean srv_count */
413 if (px->lbprm.tot_weight == 0)
414 return NULL;
415
Willy Tarreau37406352012-04-23 16:16:37 +0200416 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200417
Willy Tarreau34db1082012-04-19 17:16:54 +0200418 args[0].type = ARGT_STR;
419 args[0].data.str.str = px->hh_name;
420 args[0].data.str.len = px->hh_len;
421 args[1].type = ARGT_STOP;
422
Willy Tarreau9b28e032012-10-12 23:49:43 +0200423 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200424
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200425 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200426 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100427
Willy Tarreau9b28e032012-10-12 23:49:43 +0200428 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200429
Willy Tarreau37406352012-04-23 16:16:37 +0200430 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200431 return NULL;
432
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200433 /* note: we won't hash if there's only one server left */
434 if (px->lbprm.tot_used == 1)
435 goto hash_done;
436
Emeric Brun736aa232009-06-30 17:56:00 +0200437 /* Found a the hh_name in the headers.
438 * we will compute the hash based on this value ctx.val.
439 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200440 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200441 while (len) {
442 hash = *p + (hash << 6) + (hash << 16) - hash;
443 len--;
444 p++;
445 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100446 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
447 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200448 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200449 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
450 return chash_get_server_hash(px, hash);
451 else
452 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200453}
Benoitaffb4812009-03-25 13:02:10 +0100454
455/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 * This function applies the load-balancing algorithm to the session, as
457 * defined by the backend it is assigned to. The session is then marked as
458 * 'assigned'.
459 *
460 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
461 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100462 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * The function tries to keep the original connection slot if it reconnects to
464 * the same server, otherwise it releases it and tries to offer it.
465 *
466 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467 *
468 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100469 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
471 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472 * SRV_STATUS_INTERNAL for other unrecoverable errors.
473 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200474 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100475 * it does not need to be called anymore. This means that target_srv(&s->target)
476 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477 *
478 */
479
480int assign_server(struct session *s)
481{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100482
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100484 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100486
Simon Horman8effd3d2011-08-13 08:03:52 +0900487 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau7c669d72008-06-20 15:04:11 +0200489 err = SRV_STATUS_INTERNAL;
490 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
491 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100492
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100493 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200494 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 /* We have to release any connection slot before applying any LB algo,
497 * otherwise we may erroneously end up with no available slot.
498 */
499 if (conn_slot)
500 sess_change_server(s, NULL);
501
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100502 /* We will now try to find the good server and store it into <objt_server(s->target)>.
503 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 * as well as if no server is available (check error code).
505 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100506
Willy Tarreau827aee92011-03-10 16:55:02 +0100507 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100508 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100509
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200510 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200511 /* we must check if we have at least one server available */
512 if (!s->be->lbprm.tot_weight) {
513 err = SRV_STATUS_NOSRV;
514 goto out;
515 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200517 /* First check whether we need to fetch some data or simply call
518 * the LB lookup function. Only the hashing functions will need
519 * some input data in fact, and will support multiple algorithms.
520 */
521 switch (s->be->lbprm.algo & BE_LB_LKUP) {
522 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100523 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200524 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200525
Willy Tarreauf09c6602012-02-13 17:12:08 +0100526 case BE_LB_LKUP_FSTREE:
527 srv = fas_get_next_server(s->be, prev_srv);
528 break;
529
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200530 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100531 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200532 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200533
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200534 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200535 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200536 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200537 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100538 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200539 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100540 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200541 break;
542 }
543 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200544 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200545 err = SRV_STATUS_INTERNAL;
546 goto out;
547 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200548
549 switch (s->be->lbprm.algo & BE_LB_PARM) {
550 case BE_LB_HASH_SRC:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200551 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200552 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200553 (void *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200554 4);
555 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200556 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200557 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200558 (void *)&((struct sockaddr_in6 *)&s->req->prod->conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200559 16);
560 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200561 else {
562 /* unknown IP family */
563 err = SRV_STATUS_INTERNAL;
564 goto out;
565 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200566 break;
567
568 case BE_LB_HASH_URI:
569 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100570 if (s->txn.req.msg_state < HTTP_MSG_BODY)
571 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100572 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200573 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100574 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200575 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200576
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 case BE_LB_HASH_PRM:
578 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100579 if (s->txn.req.msg_state < HTTP_MSG_BODY)
580 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100581
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200583 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100584 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100585
Willy Tarreau827aee92011-03-10 16:55:02 +0100586 if (!srv && s->txn.meth == HTTP_METH_POST)
587 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 break;
Benoitaffb4812009-03-25 13:02:10 +0100589
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200590 case BE_LB_HASH_HDR:
591 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100592 if (s->txn.req.msg_state < HTTP_MSG_BODY)
593 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200595 break;
596
597 case BE_LB_HASH_RDP:
598 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100599 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600 break;
601
602 default:
603 /* unknown balancing algorithm */
604 err = SRV_STATUS_INTERNAL;
605 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100606 }
Emeric Brun736aa232009-06-30 17:56:00 +0200607
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200608 /* If the hashing parameter was not found, let's fall
609 * back to round robin on the map.
610 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200612 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200614 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200616 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200617
618 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200619 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200620
Willy Tarreau7c669d72008-06-20 15:04:11 +0200621 default:
622 /* unknown balancing algorithm */
623 err = SRV_STATUS_INTERNAL;
624 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200626
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200628 err = SRV_STATUS_FULL;
629 goto out;
630 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100632 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100634 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100635 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200636 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200637 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100640 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200641 is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100642 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100643 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100644 }
645 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200646 err = SRV_STATUS_NOSRV;
647 goto out;
648 }
649
650 s->flags |= SN_ASSIGNED;
651 err = SRV_STATUS_OK;
652 out:
653
654 /* Either we take back our connection slot, or we offer it to someone
655 * else if we don't need it anymore.
656 */
657 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100658 if (conn_slot == srv) {
659 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200660 } else {
661 if (may_dequeue_tasks(conn_slot, s->be))
662 process_srv_queue(conn_slot);
663 }
664 }
665
666 out_err:
667 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668}
669
670
671/*
672 * This function assigns a server address to a session, and sets SN_ADDR_SET.
673 * The address is taken from the currently assigned server, or from the
674 * dispatch or transparent address.
675 *
676 * It may return :
677 * SRV_STATUS_OK if everything is OK.
678 * SRV_STATUS_INTERNAL for other unrecoverable errors.
679 *
680 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
681 * not cleared, so it's to the caller to clear it if required.
682 *
683 */
684int assign_server_address(struct session *s)
685{
686#ifdef DEBUG_FULL
687 fprintf(stderr,"assign_server_address : s=%p\n",s);
688#endif
689
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200690 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691 /* A server is necessarily known for this session */
692 if (!(s->flags & SN_ASSIGNED))
693 return SRV_STATUS_INTERNAL;
694
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100695 s->req->cons->conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200697 if (!is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200698 /* if the server has no address, we use the same address
699 * the client asked, which is handy for remapping ports
700 * locally on multiple addresses at once.
701 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200702 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200703 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200704
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200705 if (s->req->prod->conn->addr.to.ss_family == AF_INET) {
706 ((struct sockaddr_in *)&s->req->cons->conn->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
707 } else if (s->req->prod->conn->addr.to.ss_family == AF_INET6) {
708 ((struct sockaddr_in6 *)&s->req->cons->conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&s->req->prod->conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200709 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200710 }
711
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 /* if this server remaps proxied ports, we'll use
713 * the port the client connected to with an offset. */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100714 if (objt_server(s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715 int base_port;
716
Willy Tarreau9b061e32012-04-07 18:03:52 +0200717 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200718 conn_get_to_addr(s->req->prod->conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100719
720 /* First, retrieve the port from the incoming connection */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200721 base_port = get_host_port(&s->req->prod->conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100722
723 /* Second, assign the outgoing connection's port */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200724 base_port += get_host_port(&s->req->cons->conn->addr.to);
725 set_host_port(&s->req->cons->conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 }
727 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200728 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 /* connect to the defined dispatch addr */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200730 s->req->cons->conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100732 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200734 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200736 if (s->req->prod->conn->addr.to.ss_family == AF_INET || s->req->prod->conn->addr.to.ss_family == AF_INET6) {
737 memcpy(&s->req->cons->conn->addr.to, &s->req->prod->conn->addr.to, MIN(sizeof(s->req->cons->conn->addr.to), sizeof(s->req->prod->conn->addr.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200738 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100739 /* when we support IPv6 on the backend, we may add other tests */
740 //qfprintf(stderr, "Cannot get original server address.\n");
741 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100743 else if (s->be->options & PR_O_HTTP_PROXY) {
744 /* If HTTP PROXY option is set, then server is already assigned
745 * during incoming client request parsing. */
746 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100747 else {
748 /* no server and no LB algorithm ! */
749 return SRV_STATUS_INTERNAL;
750 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751
752 s->flags |= SN_ADDR_SET;
753 return SRV_STATUS_OK;
754}
755
756
757/* This function assigns a server to session <s> if required, and can add the
758 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200759 * If ->srv_conn is set, the session is first released from the server.
760 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
761 * be called before any connection and after any retry or redispatch occurs.
762 *
763 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764 *
765 * Returns :
766 *
767 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100768 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 * SRV_STATUS_QUEUED if the connection has been queued.
770 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200772 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 * SRV_STATUS_INTERNAL for other unrecoverable errors.
774 *
775 */
776int assign_server_and_queue(struct session *s)
777{
778 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 int err;
781
782 if (s->pend_pos)
783 return SRV_STATUS_INTERNAL;
784
Willy Tarreau7c669d72008-06-20 15:04:11 +0200785 err = SRV_STATUS_OK;
786 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100787 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100788
Willy Tarreau7c669d72008-06-20 15:04:11 +0200789 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100790 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200791 /* This session was previously assigned to a server. We have to
792 * update the session's and the server's stats :
793 * - if the server changed :
794 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
795 * - set SN_REDISP if it was successfully redispatched
796 * - increment srv->redispatches and be->redispatches
797 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100798 */
799
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100800 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200801 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
802 s->txn.flags &= ~TX_CK_MASK;
803 s->txn.flags |= TX_CK_DOWN;
804 }
805 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100806 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100807 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200808 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100809 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100810 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100811 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100812 }
813 }
814
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 switch (err) {
816 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200817 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100818 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 return SRV_STATUS_OK; /* dispatch or proxy mode */
821
822 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100823 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200824 return SRV_STATUS_OK;
825
826 /* OK, this session already has an assigned server, but no
827 * connection slot yet. Either it is a redispatch, or it was
828 * assigned from persistence information (direct mode).
829 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100830 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 /* server scheduled for redirection, and already assigned. We
832 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100833 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100834 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100835 return SRV_STATUS_OK;
836 }
837
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838 /* We might have to queue this session if the assigned server is full.
839 * We know we have to queue it into the server's queue, so if a maxqueue
840 * is set on the server, we must also check that the server's queue is
841 * not full, in which case we have to return FULL.
842 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100843 if (srv->maxconn &&
844 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847 return SRV_STATUS_FULL;
848
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 p = pendconn_add(s);
850 if (p)
851 return SRV_STATUS_QUEUED;
852 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200853 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200855
856 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100857 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 return SRV_STATUS_OK;
859
860 case SRV_STATUS_FULL:
861 /* queue this session into the proxy's queue */
862 p = pendconn_add(s);
863 if (p)
864 return SRV_STATUS_QUEUED;
865 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867
868 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869 return err;
870
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871 case SRV_STATUS_INTERNAL:
872 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200873
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 default:
875 return SRV_STATUS_INTERNAL;
876 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100877}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878
Willy Tarreaub1d67742010-03-29 19:36:59 +0200879/* If an explicit source binding is specified on the server and/or backend, and
880 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200881 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200882 */
883static void assign_tproxy_address(struct session *s)
884{
885#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100886 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100887 struct conn_src *src;
Willy Tarreau827aee92011-03-10 16:55:02 +0100888
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100889 if (srv && srv->conn_src.opts & CO_SRC_BIND)
890 src = &srv->conn_src;
891 else if (s->be->conn_src.opts & CO_SRC_BIND)
892 src = &s->be->conn_src;
893 else
894 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100895
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100896 switch (src->opts & CO_SRC_TPROXY_MASK) {
897 case CO_SRC_TPROXY_ADDR:
898 s->req->cons->conn->addr.from = src->tproxy_addr;
899 break;
900 case CO_SRC_TPROXY_CLI:
901 case CO_SRC_TPROXY_CIP:
902 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
903 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
904 break;
905 case CO_SRC_TPROXY_DYN:
906 if (src->bind_hdr_occ) {
907 char *vptr;
908 int vlen;
909 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100910
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100911 /* bind to the IP in a header */
912 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
913 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
914 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100915
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100916 b_rew(s->req->buf, rewind = s->req->buf->o);
917 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
918 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
919 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
920 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200921 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100922 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200923 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100924 break;
925 default:
926 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200927 }
928#endif
929}
930
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932/*
933 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200934 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100935 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936 * It can return one of :
937 * - SN_ERR_NONE if everything's OK
938 * - SN_ERR_SRVTO if there are no more servers
939 * - SN_ERR_SRVCL if the connection was refused by the server
940 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
941 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
942 * - SN_ERR_INTERNAL for any other purely internal errors
943 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
944 */
945int connect_server(struct session *s)
946{
Willy Tarreau827aee92011-03-10 16:55:02 +0100947 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200948 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949
950 if (!(s->flags & SN_ADDR_SET)) {
951 err = assign_server_address(s);
952 if (err != SRV_STATUS_OK)
953 return SN_ERR_INTERNAL;
954 }
955
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200956 /* the target was only on the session, assign it to the SI now */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100957 s->req->cons->conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200958
Willy Tarreau26d8c592012-05-07 18:12:14 +0200959 /* set the correct protocol on the output stream interface */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100960 if (objt_server(s->target)) {
961 si_prepare_conn(s->req->cons, objt_server(s->target)->proto, objt_server(s->target)->xprt);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200962 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100963 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200964 /* proxies exclusively run on raw_sock right now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200965 si_prepare_conn(s->req->cons, protocol_by_family(s->req->cons->conn->addr.to.ss_family), &raw_sock);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200966 if (!si_ctrl(s->req->cons))
Willy Tarreau26d8c592012-05-07 18:12:14 +0200967 return SN_ERR_INTERNAL;
968 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200969 else
970 return SN_ERR_INTERNAL; /* how did we get there ? */
971
972 /* process the case where the server requires the PROXY protocol to be sent */
973 s->req->cons->send_proxy_ofs = 0;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100974 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200975 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200976 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200977 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200978
Willy Tarreaub1d67742010-03-29 19:36:59 +0200979 assign_tproxy_address(s);
980
William Lallemandb7ff6a32012-03-02 14:35:21 +0100981 /* flag for logging source ip/port */
982 if (s->fe->options2 & PR_O2_SRC_ADDR)
983 s->req->cons->flags |= SI_FL_SRC_ADDR;
984
Willy Tarreau14f8e862012-08-30 22:23:13 +0200985 /* disable lingering */
986 if (s->be->options & PR_O_TCP_NOLING)
987 s->req->cons->flags |= SI_FL_NOLINGER;
988
Willy Tarreau73b013b2012-05-21 16:31:45 +0200989 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200990
Willy Tarreau9650f372009-08-16 14:02:45 +0200991 if (err != SN_ERR_NONE)
992 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200993
Willy Tarreau14f8e862012-08-30 22:23:13 +0200994 /* set connect timeout */
995 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
996
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100997 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100998 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100999 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001000 srv->cur_sess++;
1001 if (srv->cur_sess > srv->counters.cur_sess_max)
1002 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001003 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001004 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001005 }
1006
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 return SN_ERR_NONE; /* connection is OK */
1008}
1009
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011/* This function performs the "redispatch" part of a connection attempt. It
1012 * will assign a server if required, queue the connection if required, and
1013 * handle errors that might arise at this level. It can change the server
1014 * state. It will return 1 if it encounters an error, switches the server
1015 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1016 * that the connection is ready to use.
1017 */
1018
1019int srv_redispatch_connect(struct session *t)
1020{
Willy Tarreau827aee92011-03-10 16:55:02 +01001021 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022 int conn_err;
1023
1024 /* We know that we don't have any connection pending, so we will
1025 * try to get a new one, and wait in this state if it's queued
1026 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001027 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001029 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001030
Willy Tarreaubaaee002006-06-26 02:48:02 +02001031 switch (conn_err) {
1032 case SRV_STATUS_OK:
1033 break;
1034
Willy Tarreau7c669d72008-06-20 15:04:11 +02001035 case SRV_STATUS_FULL:
1036 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1037 * and we can redispatch to another server, or it is not and we return
1038 * 503. This only makes sense in DIRECT mode however, because normal LB
1039 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001040 * would bring us on the same server again. Note that t->target is set
1041 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001042 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001043 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1044 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001045 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001046 goto redispatch;
1047 }
1048
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001049 if (!t->req->cons->err_type) {
1050 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001051 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001052 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001053
Willy Tarreau827aee92011-03-10 16:55:02 +01001054 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001055 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001056 return 1;
1057
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001059 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001060 if (!t->req->cons->err_type) {
1061 t->req->cons->err_type = SI_ET_CONN_ERR;
1062 t->req->cons->err_loc = NULL;
1063 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001064
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001065 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 return 1;
1067
1068 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001069 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001070 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001071 /* do nothing else and do not wake any other session up */
1072 return 1;
1073
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074 case SRV_STATUS_INTERNAL:
1075 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001076 if (!t->req->cons->err_type) {
1077 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001078 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001079 }
1080
Willy Tarreau827aee92011-03-10 16:55:02 +01001081 if (srv)
1082 srv_inc_sess_ctr(srv);
1083 if (srv)
1084 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001085 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086
1087 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001088 if (may_dequeue_tasks(srv, t->be))
1089 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090 return 1;
1091 }
1092 /* if we get here, it's because we got SRV_STATUS_OK, which also
1093 * means that the connection has not been queued.
1094 */
1095 return 0;
1096}
1097
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001098/* Apply RDP cookie persistence to the current session. For this, the function
1099 * tries to extract an RDP cookie from the request buffer, and look for the
1100 * matching server in the list. If the server is found, it is assigned to the
1101 * session. This always returns 1, and the analyser removes itself from the
1102 * list. Nothing is performed if a server was already assigned.
1103 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001104int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001105{
1106 struct proxy *px = s->be;
1107 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001108 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001109 struct server *srv = px->srv;
1110 struct sockaddr_in addr;
1111 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001112 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001113
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001114 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 +02001115 now_ms, __FUNCTION__,
1116 s,
1117 req,
1118 req->rex, req->wex,
1119 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001120 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001121 req->analysers);
1122
1123 if (s->flags & SN_ASSIGNED)
1124 goto no_cookie;
1125
Willy Tarreau37406352012-04-23 16:16:37 +02001126 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001127
Willy Tarreau34db1082012-04-19 17:16:54 +02001128 args[0].type = ARGT_STR;
1129 args[0].data.str.str = s->be->rdp_cookie_name;
1130 args[0].data.str.len = s->be->rdp_cookie_len;
1131 args[1].type = ARGT_STOP;
1132
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001133 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001134 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001135 goto no_cookie;
1136
1137 memset(&addr, 0, sizeof(addr));
1138 addr.sin_family = AF_INET;
1139
Willy Tarreau664092c2011-12-16 19:11:42 +01001140 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001141 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001142 if (*p != '.')
1143 goto no_cookie;
1144 p++;
1145 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1146 if (*p != '.')
1147 goto no_cookie;
1148
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001149 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001150 while (srv) {
1151 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1152 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1153 /* we found the server and it is usable */
1154 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001155 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001156 break;
1157 }
1158 }
1159 srv = srv->next;
1160 }
1161
1162no_cookie:
1163 req->analysers &= ~an_bit;
1164 req->analyse_exp = TICK_ETERNITY;
1165 return 1;
1166}
1167
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001168int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001169 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001170 return px->down_time;
1171
1172 return now.tv_sec - px->last_change + px->down_time;
1173}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001175/*
1176 * This function returns a string containing the balancing
1177 * mode of the proxy in a format suitable for stats.
1178 */
1179
1180const char *backend_lb_algo_str(int algo) {
1181
1182 if (algo == BE_LB_ALGO_RR)
1183 return "roundrobin";
1184 else if (algo == BE_LB_ALGO_SRR)
1185 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001186 else if (algo == BE_LB_ALGO_FAS)
1187 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001188 else if (algo == BE_LB_ALGO_LC)
1189 return "leastconn";
1190 else if (algo == BE_LB_ALGO_SH)
1191 return "source";
1192 else if (algo == BE_LB_ALGO_UH)
1193 return "uri";
1194 else if (algo == BE_LB_ALGO_PH)
1195 return "url_param";
1196 else if (algo == BE_LB_ALGO_HH)
1197 return "hdr";
1198 else if (algo == BE_LB_ALGO_RCH)
1199 return "rdp-cookie";
1200 else
1201 return NULL;
1202}
1203
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001204/* This function parses a "balance" statement in a backend section describing
1205 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001206 * returns -1, it will write an error message into the <err> buffer which will
1207 * automatically be allocated and must be passed as NULL. The trailing '\n'
1208 * will not be written. The function must be called with <args> pointing to the
1209 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001210 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001211int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001212{
1213 if (!*(args[0])) {
1214 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001215 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1216 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001217 return 0;
1218 }
1219
1220 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001221 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1222 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001223 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001224 else if (!strcmp(args[0], "static-rr")) {
1225 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1226 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1227 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001228 else if (!strcmp(args[0], "first")) {
1229 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1230 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1231 }
Willy Tarreau51406232008-03-10 22:04:20 +01001232 else if (!strcmp(args[0], "leastconn")) {
1233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1235 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001236 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001239 }
1240 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001241 int arg = 1;
1242
Willy Tarreau31682232007-11-29 15:38:04 +01001243 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1244 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001245
Oskar Stolc8dc41842012-05-19 10:19:54 +01001246 curproxy->uri_whole = 0;
1247
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001248 while (*args[arg]) {
1249 if (!strcmp(args[arg], "len")) {
1250 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001251 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001252 return -1;
1253 }
1254 curproxy->uri_len_limit = atoi(args[arg+1]);
1255 arg += 2;
1256 }
1257 else if (!strcmp(args[arg], "depth")) {
1258 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001259 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001260 return -1;
1261 }
1262 /* hint: we store the position of the ending '/' (depth+1) so
1263 * that we avoid a comparison while computing the hash.
1264 */
1265 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1266 arg += 2;
1267 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001268 else if (!strcmp(args[arg], "whole")) {
1269 curproxy->uri_whole = 1;
1270 arg += 1;
1271 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001272 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001273 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001274 return -1;
1275 }
1276 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001277 }
Willy Tarreau01732802007-11-01 22:48:15 +01001278 else if (!strcmp(args[0], "url_param")) {
1279 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001280 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001281 return -1;
1282 }
Willy Tarreau31682232007-11-29 15:38:04 +01001283 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1284 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001285
1286 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001287 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001288 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001289 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001290 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001291 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001292 return -1;
1293 }
1294 if (*args[3]) {
1295 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1296 curproxy->url_param_post_limit = str2ui(args[3]);
1297 }
1298 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1299 if (!curproxy->url_param_post_limit)
1300 curproxy->url_param_post_limit = 48;
1301 else if ( curproxy->url_param_post_limit < 3 )
1302 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1303 }
Benoitaffb4812009-03-25 13:02:10 +01001304 }
1305 else if (!strncmp(args[0], "hdr(", 4)) {
1306 const char *beg, *end;
1307
1308 beg = args[0] + 4;
1309 end = strchr(beg, ')');
1310
1311 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001312 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001313 return -1;
1314 }
1315
1316 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1317 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1318
1319 free(curproxy->hh_name);
1320 curproxy->hh_len = end - beg;
1321 curproxy->hh_name = my_strndup(beg, end - beg);
1322 curproxy->hh_match_domain = 0;
1323
1324 if (*args[1]) {
1325 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001326 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001327 return -1;
1328 }
1329 curproxy->hh_match_domain = 1;
1330 }
1331
Emeric Brun736aa232009-06-30 17:56:00 +02001332 }
1333 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1334 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1335 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1336
1337 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1338 const char *beg, *end;
1339
1340 beg = args[0] + 11;
1341 end = strchr(beg, ')');
1342
1343 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001344 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001345 return -1;
1346 }
1347
1348 free(curproxy->hh_name);
1349 curproxy->hh_name = my_strndup(beg, end - beg);
1350 curproxy->hh_len = end - beg;
1351 }
1352 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1353 free(curproxy->hh_name);
1354 curproxy->hh_name = strdup("mstshash");
1355 curproxy->hh_len = strlen(curproxy->hh_name);
1356 }
1357 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001358 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001359 return -1;
1360 }
Willy Tarreau01732802007-11-01 22:48:15 +01001361 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001362 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001363 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 +01001364 return -1;
1365 }
1366 return 0;
1367}
1368
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001369
1370/************************************************************************/
1371/* All supported keywords must be declared here. */
1372/************************************************************************/
1373
Willy Tarreau34db1082012-04-19 17:16:54 +02001374/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001375 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001376 * undefined behaviour.
1377 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001378static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001379acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001380 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001381{
Willy Tarreau37406352012-04-23 16:16:37 +02001382 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001383 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001384 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001385
1386 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001387 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001388 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001389 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001390 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001391 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001392
1393 return 1;
1394}
1395
Willy Tarreau37406352012-04-23 16:16:37 +02001396/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001397 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001398 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1399 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001400 */
1401static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001402acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001403 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001404{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001405 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001406
Willy Tarreau37406352012-04-23 16:16:37 +02001407 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001408 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001409 if (!(srv->state & SRV_MAINTAIN) &&
1410 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001411 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001412 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001413 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001414 return 1;
1415}
1416
Willy Tarreau34db1082012-04-19 17:16:54 +02001417/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001418 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001419 * undefined behaviour.
1420 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001421static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001422acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001423 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001424{
1425 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001426
Willy Tarreau37406352012-04-23 16:16:37 +02001427 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001428 smp->type = SMP_T_UINT;
1429 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001430
Willy Tarreau24e32d82012-04-23 23:55:44 +02001431 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001432 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001433 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001434
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001435 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001436 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001437 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001438 return 1;
1439 }
1440
Willy Tarreauf853c462012-04-23 18:53:56 +02001441 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001442 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001443 }
1444
1445 return 1;
1446}
1447
Willy Tarreaua5e37562011-12-16 17:06:15 +01001448/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001449static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001450acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001451 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001452{
Willy Tarreauf853c462012-04-23 18:53:56 +02001453 smp->flags = SMP_F_VOL_TXN;
1454 smp->type = SMP_T_UINT;
1455 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001456 return 1;
1457}
1458
Willy Tarreaua5e37562011-12-16 17:06:15 +01001459/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001460static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001461acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001462 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001463{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001464 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001465 return 0;
1466
Willy Tarreauf853c462012-04-23 18:53:56 +02001467 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001468 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001469
1470 return 1;
1471}
1472
Willy Tarreau34db1082012-04-19 17:16:54 +02001473/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001474 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001475 * undefined behaviour.
1476 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001477static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001478acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001479 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001480{
Willy Tarreau37406352012-04-23 16:16:37 +02001481 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001482 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001483 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001484 return 1;
1485}
1486
Willy Tarreau34db1082012-04-19 17:16:54 +02001487/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001488 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001489 * undefined behaviour.
1490 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001491static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001492acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001493 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001494{
Willy Tarreau37406352012-04-23 16:16:37 +02001495 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001496 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001497 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001498 return 1;
1499}
1500
Willy Tarreau34db1082012-04-19 17:16:54 +02001501/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001502 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001503 * undefined behaviour.
1504 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001505static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001506acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001507 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001508{
Willy Tarreau37406352012-04-23 16:16:37 +02001509 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001510 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001511 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001512 return 1;
1513}
1514
Willy Tarreaua5e37562011-12-16 17:06:15 +01001515/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001516 * by the number of running servers and rounded up. If there is no running
1517 * server, we return twice the total, just as if we had half a running server.
1518 * This is more or less correct anyway, since we expect the last server to come
1519 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001520 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001521 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001522 */
1523static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001524acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001525 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001526{
1527 int nbsrv;
1528
Willy Tarreau37406352012-04-23 16:16:37 +02001529 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001530 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001531 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001532
1533 if (px->srv_act)
1534 nbsrv = px->srv_act;
1535 else if (px->lbprm.fbck)
1536 nbsrv = 1;
1537 else
1538 nbsrv = px->srv_bck;
1539
1540 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001541 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001542 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001543 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001544
1545 return 1;
1546}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001547
Willy Tarreau34db1082012-04-19 17:16:54 +02001548/* set temp integer to the number of concurrent connections on the server in the backend.
1549 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1550 * undefined behaviour.
1551 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001552static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001553acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001554 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001555{
Willy Tarreauf853c462012-04-23 18:53:56 +02001556 smp->flags = SMP_F_VOL_TEST;
1557 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001558 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001559 return 1;
1560}
1561
Tait Clarridge7896d522012-12-05 21:39:31 -05001562/* set temp integer to the number of enabled servers on the proxy.
1563 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1564 * undefined behaviour.
1565 */
1566static int
1567acl_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1568 const struct arg *args, struct sample *smp)
1569{
1570 smp->flags = SMP_F_VOL_TEST;
1571 smp->type = SMP_T_UINT;
1572 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1573 return 1;
1574}
1575
Willy Tarreau61612d42012-04-19 18:42:05 +02001576/* Note: must not be declared <const> as its list will be overwritten.
1577 * Please take care of keeping this list alphabetically sorted.
1578 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001579static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001580 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1581 { "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 +02001582 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001583 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1584 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1585 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1586 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001587 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1588 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1589 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING, ARG1(1,SRV) },
Tait Clarridge7896d522012-12-05 21:39:31 -05001590 { "srv_sess_rate", acl_parse_int, acl_fetch_srv_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001591 { NULL, NULL, NULL, NULL },
1592}};
1593
1594
1595__attribute__((constructor))
1596static void __backend_init(void)
1597{
1598 acl_register_keywords(&acl_kws);
1599}
1600
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601/*
1602 * Local variables:
1603 * c-indent-level: 8
1604 * c-basic-offset: 8
1605 * End:
1606 */