blob: 82a88beb21b8ed68b74566b0d52557485c83ff2d [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 Tarreaud1d54542012-09-12 22:58:11 +020041#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020043#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010045#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020046#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020047#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010048#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/task.h>
50
Willy Tarreaubaaee002006-06-26 02:48:02 +020051/*
52 * This function recounts the number of usable active and backup servers for
53 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010054 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010055 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010056 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020058void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020059{
60 struct server *srv;
61
Willy Tarreau20697042007-11-15 23:26:18 +010062 px->srv_act = px->srv_bck = 0;
63 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010064 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010066 if (!srv_is_usable(srv->state, srv->eweight))
67 continue;
68
69 if (srv->state & SRV_BACKUP) {
70 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010071 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010072 px->lbprm.fbck = srv;
73 px->srv_bck++;
74 px->lbprm.tot_wbck += srv->eweight;
75 } else {
76 px->srv_act++;
77 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 }
79 }
Willy Tarreaub625a082007-11-26 01:15:43 +010080}
Willy Tarreau20697042007-11-15 23:26:18 +010081
Willy Tarreaub625a082007-11-26 01:15:43 +010082/* This function simply updates the backend's tot_weight and tot_used values
83 * after servers weights have been updated. It is designed to be used after
84 * recount_servers() or equivalent.
85 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020086void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010087{
Willy Tarreau20697042007-11-15 23:26:18 +010088 if (px->srv_act) {
89 px->lbprm.tot_weight = px->lbprm.tot_wact;
90 px->lbprm.tot_used = px->srv_act;
91 }
Willy Tarreaub625a082007-11-26 01:15:43 +010092 else if (px->lbprm.fbck) {
93 /* use only the first backup server */
94 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
95 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010096 }
97 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010098 px->lbprm.tot_weight = px->lbprm.tot_wbck;
99 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100100 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100101}
102
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200103/*
104 * This function tries to find a running server for the proxy <px> following
105 * the source hash method. Depending on the number of active/backup servers,
106 * it will either look for active servers, or for backup servers.
107 * If any server is found, it will be returned. If no valid server is found,
108 * NULL is returned.
109 */
110struct server *get_server_sh(struct proxy *px, const char *addr, int len)
111{
112 unsigned int h, l;
113
114 if (px->lbprm.tot_weight == 0)
115 return NULL;
116
117 l = h = 0;
118
119 /* note: we won't hash if there's only one server left */
120 if (px->lbprm.tot_used == 1)
121 goto hash_done;
122
123 while ((l + sizeof (int)) <= len) {
124 h ^= ntohl(*(unsigned int *)(&addr[l]));
125 l += sizeof (int);
126 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100127 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
128 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200129 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200130 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
131 return chash_get_server_hash(px, h);
132 else
133 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200134}
135
136/*
137 * This function tries to find a running server for the proxy <px> following
138 * the URI hash method. In order to optimize cache hits, the hash computation
139 * ends at the question mark. Depending on the number of active/backup servers,
140 * it will either look for active servers, or for backup servers.
141 * If any server is found, it will be returned. If no valid server is found,
142 * NULL is returned.
143 *
144 * This code was contributed by Guillaume Dallaire, who also selected this hash
145 * algorithm out of a tens because it gave him the best results.
146 *
147 */
148struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
149{
150 unsigned long hash = 0;
151 int c;
152 int slashes = 0;
153
154 if (px->lbprm.tot_weight == 0)
155 return NULL;
156
157 /* note: we won't hash if there's only one server left */
158 if (px->lbprm.tot_used == 1)
159 goto hash_done;
160
161 if (px->uri_len_limit)
162 uri_len = MIN(uri_len, px->uri_len_limit);
163
164 while (uri_len--) {
165 c = *uri++;
166 if (c == '/') {
167 slashes++;
168 if (slashes == px->uri_dirs_depth1) /* depth+1 */
169 break;
170 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100171 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200172 break;
173
174 hash = c + (hash << 6) + (hash << 16) - hash;
175 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100176 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
177 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200178 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200179 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
180 return chash_get_server_hash(px, hash);
181 else
182 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200183}
184
Willy Tarreau01732802007-11-01 22:48:15 +0100185/*
186 * This function tries to find a running server for the proxy <px> following
187 * the URL parameter hash method. It looks for a specific parameter in the
188 * URL and hashes it to compute the server ID. This is useful to optimize
189 * performance by avoiding bounces between servers in contexts where sessions
190 * are shared but cookies are not usable. If the parameter is not found, NULL
191 * is returned. If any server is found, it will be returned. If no valid server
192 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100193 */
194struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
195{
196 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200197 const char *p;
198 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100199 int plen;
200
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200201 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100202 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100203 return NULL;
204
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200205 if ((p = memchr(uri, '?', uri_len)) == NULL)
206 return NULL;
207
Willy Tarreau01732802007-11-01 22:48:15 +0100208 p++;
209
210 uri_len -= (p - uri);
211 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200212 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100213
214 while (uri_len > plen) {
215 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200216 if (params[plen] == '=') {
217 if (memcmp(params, px->url_param_name, plen) == 0) {
218 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100219 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200220 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100221 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200222 p += plen + 1;
223 uri_len -= plen + 1;
224
Willy Tarreau01732802007-11-01 22:48:15 +0100225 while (uri_len && *p != '&') {
226 hash = *p + (hash << 6) + (hash << 16) - hash;
227 uri_len--;
228 p++;
229 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100230 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
231 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200232 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
233 return chash_get_server_hash(px, hash);
234 else
235 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100236 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200237 }
238 /* skip to next parameter */
239 p = memchr(params, '&', uri_len);
240 if (!p)
241 return NULL;
242 p++;
243 uri_len -= (p - params);
244 params = p;
245 }
246 return NULL;
247}
248
249/*
250 * this does the same as the previous server_ph, but check the body contents
251 */
252struct server *get_server_ph_post(struct session *s)
253{
254 unsigned long hash = 0;
255 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200256 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200257 struct http_msg *msg = &txn->req;
258 struct proxy *px = s->be;
259 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100260 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200261 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100262 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263
Willy Tarreau9b28e032012-10-12 23:49:43 +0200264 if (len > buffer_len(req->buf) - msg->sov)
265 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266
Willy Tarreau157dd632009-12-06 19:18:09 +0100267 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268 return NULL;
269
Willy Tarreau157dd632009-12-06 19:18:09 +0100270 if (px->lbprm.tot_weight == 0)
271 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200272
273 while (len > plen) {
274 /* Look for the parameter name followed by an equal symbol */
275 if (params[plen] == '=') {
276 if (memcmp(params, px->url_param_name, plen) == 0) {
277 /* OK, we have the parameter here at <params>, and
278 * the value after the equal sign, at <p>
279 * skip the equal symbol
280 */
281 p += plen + 1;
282 len -= plen + 1;
283
284 while (len && *p != '&') {
285 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100286 /* if in a POST, body must be URI encoded or it's not a URI.
287 * Do not interprete any possible binary data as a parameter.
288 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200289 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
290 break;
291 return NULL; /* oh, no; this is not uri-encoded.
292 * This body does not contain parameters.
293 */
294 }
295 hash = *p + (hash << 6) + (hash << 16) - hash;
296 len--;
297 p++;
298 /* should we break if vlen exceeds limit? */
299 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100300 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
301 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200302 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
303 return chash_get_server_hash(px, hash);
304 else
305 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200306 }
307 }
Willy Tarreau01732802007-11-01 22:48:15 +0100308 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100310 if (!p)
311 return NULL;
312 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200313 len -= (p - params);
314 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100315 }
316 return NULL;
317}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320/*
Benoitaffb4812009-03-25 13:02:10 +0100321 * This function tries to find a running server for the proxy <px> following
322 * the Header parameter hash method. It looks for a specific parameter in the
323 * URL and hashes it to compute the server ID. This is useful to optimize
324 * performance by avoiding bounces between servers in contexts where sessions
325 * are shared but cookies are not usable. If the parameter is not found, NULL
326 * is returned. If any server is found, it will be returned. If no valid server
327 * is found, NULL is returned.
328 */
329struct server *get_server_hh(struct session *s)
330{
331 unsigned long hash = 0;
332 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100333 struct proxy *px = s->be;
334 unsigned int plen = px->hh_len;
335 unsigned long len;
336 struct hdr_ctx ctx;
337 const char *p;
338
339 /* tot_weight appears to mean srv_count */
340 if (px->lbprm.tot_weight == 0)
341 return NULL;
342
Benoitaffb4812009-03-25 13:02:10 +0100343 ctx.idx = 0;
344
345 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200346 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 +0100347
348 /* if the header is not found or empty, let's fallback to round robin */
349 if (!ctx.idx || !ctx.vlen)
350 return NULL;
351
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200352 /* note: we won't hash if there's only one server left */
353 if (px->lbprm.tot_used == 1)
354 goto hash_done;
355
Benoitaffb4812009-03-25 13:02:10 +0100356 /* Found a the hh_name in the headers.
357 * we will compute the hash based on this value ctx.val.
358 */
359 len = ctx.vlen;
360 p = (char *)ctx.line + ctx.val;
361 if (!px->hh_match_domain) {
362 while (len) {
363 hash = *p + (hash << 6) + (hash << 16) - hash;
364 len--;
365 p++;
366 }
367 } else {
368 int dohash = 0;
369 p += len - 1;
370 /* special computation, use only main domain name, not tld/host
371 * going back from the end of string, start hashing at first
372 * dot stop at next.
373 * This is designed to work with the 'Host' header, and requires
374 * a special option to activate this.
375 */
376 while (len) {
377 if (*p == '.') {
378 if (!dohash)
379 dohash = 1;
380 else
381 break;
382 } else {
383 if (dohash)
384 hash = *p + (hash << 6) + (hash << 16) - hash;
385 }
386 len--;
387 p--;
388 }
389 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100390 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
391 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200392 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200393 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
394 return chash_get_server_hash(px, hash);
395 else
396 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100397}
398
Willy Tarreau34db1082012-04-19 17:16:54 +0200399/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200400struct server *get_server_rch(struct session *s)
401{
402 unsigned long hash = 0;
403 struct proxy *px = s->be;
404 unsigned long len;
405 const char *p;
406 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200407 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200408 struct arg args[2];
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200409 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200410
411 /* tot_weight appears to mean srv_count */
412 if (px->lbprm.tot_weight == 0)
413 return NULL;
414
Willy Tarreau37406352012-04-23 16:16:37 +0200415 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200416
Willy Tarreau34db1082012-04-19 17:16:54 +0200417 args[0].type = ARGT_STR;
418 args[0].data.str.str = px->hh_name;
419 args[0].data.str.len = px->hh_len;
420 args[1].type = ARGT_STOP;
421
Willy Tarreau9b28e032012-10-12 23:49:43 +0200422 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200423
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200424 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200425 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100426
Willy Tarreau9b28e032012-10-12 23:49:43 +0200427 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200428
Willy Tarreau37406352012-04-23 16:16:37 +0200429 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200430 return NULL;
431
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200432 /* note: we won't hash if there's only one server left */
433 if (px->lbprm.tot_used == 1)
434 goto hash_done;
435
Emeric Brun736aa232009-06-30 17:56:00 +0200436 /* Found a the hh_name in the headers.
437 * we will compute the hash based on this value ctx.val.
438 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200439 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200440 while (len) {
441 hash = *p + (hash << 6) + (hash << 16) - hash;
442 len--;
443 p++;
444 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100445 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
446 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200447 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200448 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
449 return chash_get_server_hash(px, hash);
450 else
451 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200452}
Benoitaffb4812009-03-25 13:02:10 +0100453
454/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200455 * This function applies the load-balancing algorithm to the session, as
456 * defined by the backend it is assigned to. The session is then marked as
457 * 'assigned'.
458 *
459 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
460 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100461 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200462 * The function tries to keep the original connection slot if it reconnects to
463 * the same server, otherwise it releases it and tries to offer it.
464 *
465 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 *
467 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100468 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200469 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
470 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471 * SRV_STATUS_INTERNAL for other unrecoverable errors.
472 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200473 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100474 * it does not need to be called anymore. This means that target_srv(&s->target)
475 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 *
477 */
478
479int assign_server(struct session *s)
480{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100481
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100483 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200484 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100485
Simon Horman8effd3d2011-08-13 08:03:52 +0900486 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200487
Willy Tarreau7c669d72008-06-20 15:04:11 +0200488 err = SRV_STATUS_INTERNAL;
489 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
490 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100491
Willy Tarreau827aee92011-03-10 16:55:02 +0100492 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200493 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
Willy Tarreau7c669d72008-06-20 15:04:11 +0200495 /* We have to release any connection slot before applying any LB algo,
496 * otherwise we may erroneously end up with no available slot.
497 */
498 if (conn_slot)
499 sess_change_server(s, NULL);
500
Willy Tarreau827aee92011-03-10 16:55:02 +0100501 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
502 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200503 * as well as if no server is available (check error code).
504 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100505
Willy Tarreau827aee92011-03-10 16:55:02 +0100506 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100507 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100508
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200509 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200510 /* we must check if we have at least one server available */
511 if (!s->be->lbprm.tot_weight) {
512 err = SRV_STATUS_NOSRV;
513 goto out;
514 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200516 /* First check whether we need to fetch some data or simply call
517 * the LB lookup function. Only the hashing functions will need
518 * some input data in fact, and will support multiple algorithms.
519 */
520 switch (s->be->lbprm.algo & BE_LB_LKUP) {
521 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100522 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200523 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200524
Willy Tarreauf09c6602012-02-13 17:12:08 +0100525 case BE_LB_LKUP_FSTREE:
526 srv = fas_get_next_server(s->be, prev_srv);
527 break;
528
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100530 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200531 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200532
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200533 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200534 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200535 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200536 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100537 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200538 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100539 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200540 break;
541 }
542 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200543 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200544 err = SRV_STATUS_INTERNAL;
545 goto out;
546 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200547
548 switch (s->be->lbprm.algo & BE_LB_PARM) {
549 case BE_LB_HASH_SRC:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200550 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200551 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200552 (void *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200553 4);
554 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200555 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200556 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200557 (void *)&((struct sockaddr_in6 *)&s->req->prod->conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200558 16);
559 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200560 else {
561 /* unknown IP family */
562 err = SRV_STATUS_INTERNAL;
563 goto out;
564 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200565 break;
566
567 case BE_LB_HASH_URI:
568 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100569 if (s->txn.req.msg_state < HTTP_MSG_BODY)
570 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100571 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200572 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100573 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200574 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200575
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200576 case BE_LB_HASH_PRM:
577 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100578 if (s->txn.req.msg_state < HTTP_MSG_BODY)
579 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100580
Willy Tarreau827aee92011-03-10 16:55:02 +0100581 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200582 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100583 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100584
Willy Tarreau827aee92011-03-10 16:55:02 +0100585 if (!srv && s->txn.meth == HTTP_METH_POST)
586 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200587 break;
Benoitaffb4812009-03-25 13:02:10 +0100588
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200589 case BE_LB_HASH_HDR:
590 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100591 if (s->txn.req.msg_state < HTTP_MSG_BODY)
592 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100593 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594 break;
595
596 case BE_LB_HASH_RDP:
597 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100598 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200599 break;
600
601 default:
602 /* unknown balancing algorithm */
603 err = SRV_STATUS_INTERNAL;
604 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100605 }
Emeric Brun736aa232009-06-30 17:56:00 +0200606
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200607 /* If the hashing parameter was not found, let's fall
608 * back to round robin on the map.
609 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100610 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200611 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100612 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200613 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100614 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200615 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
617 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200618 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
Willy Tarreau7c669d72008-06-20 15:04:11 +0200620 default:
621 /* unknown balancing algorithm */
622 err = SRV_STATUS_INTERNAL;
623 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200625
Willy Tarreau827aee92011-03-10 16:55:02 +0100626 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200627 err = SRV_STATUS_FULL;
628 goto out;
629 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100630 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100631 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100632 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100633 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100634 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200635 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200636 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100637 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100639 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200640 is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100641 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100642 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100643 }
644 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200645 err = SRV_STATUS_NOSRV;
646 goto out;
647 }
648
649 s->flags |= SN_ASSIGNED;
650 err = SRV_STATUS_OK;
651 out:
652
653 /* Either we take back our connection slot, or we offer it to someone
654 * else if we don't need it anymore.
655 */
656 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100657 if (conn_slot == srv) {
658 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200659 } else {
660 if (may_dequeue_tasks(conn_slot, s->be))
661 process_srv_queue(conn_slot);
662 }
663 }
664
665 out_err:
666 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667}
668
669
670/*
671 * This function assigns a server address to a session, and sets SN_ADDR_SET.
672 * The address is taken from the currently assigned server, or from the
673 * dispatch or transparent address.
674 *
675 * It may return :
676 * SRV_STATUS_OK if everything is OK.
677 * SRV_STATUS_INTERNAL for other unrecoverable errors.
678 *
679 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
680 * not cleared, so it's to the caller to clear it if required.
681 *
682 */
683int assign_server_address(struct session *s)
684{
685#ifdef DEBUG_FULL
686 fprintf(stderr,"assign_server_address : s=%p\n",s);
687#endif
688
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200689 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 /* A server is necessarily known for this session */
691 if (!(s->flags & SN_ASSIGNED))
692 return SRV_STATUS_INTERNAL;
693
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200694 s->req->cons->conn->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200696 if (!is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200697 /* if the server has no address, we use the same address
698 * the client asked, which is handy for remapping ports
699 * locally on multiple addresses at once.
700 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200701 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200702 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200703
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200704 if (s->req->prod->conn->addr.to.ss_family == AF_INET) {
705 ((struct sockaddr_in *)&s->req->cons->conn->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
706 } else if (s->req->prod->conn->addr.to.ss_family == AF_INET6) {
707 ((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 +0200708 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200709 }
710
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 /* if this server remaps proxied ports, we'll use
712 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100713 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100714 int base_port;
715
Willy Tarreau9b061e32012-04-07 18:03:52 +0200716 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200717 conn_get_to_addr(s->req->prod->conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100718
719 /* First, retrieve the port from the incoming connection */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200720 base_port = get_host_port(&s->req->prod->conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100721
722 /* Second, assign the outgoing connection's port */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200723 base_port += get_host_port(&s->req->cons->conn->addr.to);
724 set_host_port(&s->req->cons->conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 }
726 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200727 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728 /* connect to the defined dispatch addr */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200729 s->req->cons->conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100731 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200733 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200735 if (s->req->prod->conn->addr.to.ss_family == AF_INET || s->req->prod->conn->addr.to.ss_family == AF_INET6) {
736 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 +0200737 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100738 /* when we support IPv6 on the backend, we may add other tests */
739 //qfprintf(stderr, "Cannot get original server address.\n");
740 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100742 else if (s->be->options & PR_O_HTTP_PROXY) {
743 /* If HTTP PROXY option is set, then server is already assigned
744 * during incoming client request parsing. */
745 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100746 else {
747 /* no server and no LB algorithm ! */
748 return SRV_STATUS_INTERNAL;
749 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750
751 s->flags |= SN_ADDR_SET;
752 return SRV_STATUS_OK;
753}
754
755
756/* This function assigns a server to session <s> if required, and can add the
757 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200758 * If ->srv_conn is set, the session is first released from the server.
759 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
760 * be called before any connection and after any retry or redispatch occurs.
761 *
762 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 *
764 * Returns :
765 *
766 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 * SRV_STATUS_QUEUED if the connection has been queued.
769 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100770 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200771 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 * SRV_STATUS_INTERNAL for other unrecoverable errors.
773 *
774 */
775int assign_server_and_queue(struct session *s)
776{
777 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 int err;
780
781 if (s->pend_pos)
782 return SRV_STATUS_INTERNAL;
783
Willy Tarreau7c669d72008-06-20 15:04:11 +0200784 err = SRV_STATUS_OK;
785 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100786 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100787
Willy Tarreau7c669d72008-06-20 15:04:11 +0200788 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100789 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200790 /* This session was previously assigned to a server. We have to
791 * update the session's and the server's stats :
792 * - if the server changed :
793 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
794 * - set SN_REDISP if it was successfully redispatched
795 * - increment srv->redispatches and be->redispatches
796 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100797 */
798
Willy Tarreau827aee92011-03-10 16:55:02 +0100799 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200800 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
801 s->txn.flags &= ~TX_CK_MASK;
802 s->txn.flags |= TX_CK_DOWN;
803 }
804 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100805 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100806 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200807 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100808 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100809 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100810 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100811 }
812 }
813
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 switch (err) {
815 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 srv = target_srv(&s->target);
818 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200819 return SRV_STATUS_OK; /* dispatch or proxy mode */
820
821 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100822 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200823 return SRV_STATUS_OK;
824
825 /* OK, this session already has an assigned server, but no
826 * connection slot yet. Either it is a redispatch, or it was
827 * assigned from persistence information (direct mode).
828 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100829 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200830 /* server scheduled for redirection, and already assigned. We
831 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100832 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100833 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100834 return SRV_STATUS_OK;
835 }
836
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837 /* We might have to queue this session if the assigned server is full.
838 * We know we have to queue it into the server's queue, so if a maxqueue
839 * is set on the server, we must also check that the server's queue is
840 * not full, in which case we have to return FULL.
841 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 if (srv->maxconn &&
843 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844
Willy Tarreau827aee92011-03-10 16:55:02 +0100845 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 return SRV_STATUS_FULL;
847
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848 p = pendconn_add(s);
849 if (p)
850 return SRV_STATUS_QUEUED;
851 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200852 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200854
855 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100856 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 return SRV_STATUS_OK;
858
859 case SRV_STATUS_FULL:
860 /* queue this session into the proxy's queue */
861 p = pendconn_add(s);
862 if (p)
863 return SRV_STATUS_QUEUED;
864 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866
867 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200868 return err;
869
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 case SRV_STATUS_INTERNAL:
871 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 default:
874 return SRV_STATUS_INTERNAL;
875 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreaub1d67742010-03-29 19:36:59 +0200878/* If an explicit source binding is specified on the server and/or backend, and
879 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200880 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200881 */
882static void assign_tproxy_address(struct session *s)
883{
884#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100885 struct server *srv = target_srv(&s->target);
886
887 if (srv && srv->state & SRV_BIND_SRC) {
888 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200889 case SRV_TPROXY_ADDR:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200890 s->req->cons->conn->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200891 break;
892 case SRV_TPROXY_CLI:
893 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200894 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200895 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200896 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200897 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100898 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100899 char *vptr;
900 int vlen;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200901 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100902
Willy Tarreaubce70882009-09-07 11:51:47 +0200903 /* bind to the IP in a header */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200904 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
905 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
906 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100907
Willy Tarreau9b28e032012-10-12 23:49:43 +0200908 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreau294c4732011-12-16 21:35:50 +0100909 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
910 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200911 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
Willy Tarreau294c4732011-12-16 21:35:50 +0100912 htonl(inetaddr_host_lim(vptr, vptr + vlen));
913 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200914 b_adv(s->req->buf, rewind);
Willy Tarreaubce70882009-09-07 11:51:47 +0200915 }
916 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200917 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200918 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200919 }
920 }
921 else if (s->be->options & PR_O_BIND_SRC) {
922 switch (s->be->options & PR_O_TPXY_MASK) {
923 case PR_O_TPXY_ADDR:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200924 s->req->cons->conn->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200925 break;
926 case PR_O_TPXY_CLI:
927 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200928 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200929 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200930 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200931 case PR_O_TPXY_DYN:
932 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100933 char *vptr;
934 int vlen;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200935 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100936
Willy Tarreaubce70882009-09-07 11:51:47 +0200937 /* bind to the IP in a header */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200938 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
939 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
940 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100941
Willy Tarreau9b28e032012-10-12 23:49:43 +0200942 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreau294c4732011-12-16 21:35:50 +0100943 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
944 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200945 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
Willy Tarreau294c4732011-12-16 21:35:50 +0100946 htonl(inetaddr_host_lim(vptr, vptr + vlen));
947 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200948 b_adv(s->req->buf, rewind);
Willy Tarreaubce70882009-09-07 11:51:47 +0200949 }
950 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200951 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200952 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200953 }
954 }
955#endif
956}
957
958
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959/*
960 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200961 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100962 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963 * It can return one of :
964 * - SN_ERR_NONE if everything's OK
965 * - SN_ERR_SRVTO if there are no more servers
966 * - SN_ERR_SRVCL if the connection was refused by the server
967 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
968 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
969 * - SN_ERR_INTERNAL for any other purely internal errors
970 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
971 */
972int connect_server(struct session *s)
973{
Willy Tarreau827aee92011-03-10 16:55:02 +0100974 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200975 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976
977 if (!(s->flags & SN_ADDR_SET)) {
978 err = assign_server_address(s);
979 if (err != SRV_STATUS_OK)
980 return SN_ERR_INTERNAL;
981 }
982
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200983 /* the target was only on the session, assign it to the SI now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200984 copy_target(&s->req->cons->conn->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200985
Willy Tarreau26d8c592012-05-07 18:12:14 +0200986 /* set the correct protocol on the output stream interface */
Willy Tarreaud02394b2012-05-11 18:32:18 +0200987 if (s->target.type == TARG_TYPE_SERVER) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200988 si_prepare_conn(s->req->cons, target_srv(&s->target)->proto, target_srv(&s->target)->xprt);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200989 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200990 else if (s->target.type == TARG_TYPE_PROXY) {
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200991 /* proxies exclusively run on raw_sock right now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200992 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 +0200993 if (!si_ctrl(s->req->cons))
Willy Tarreau26d8c592012-05-07 18:12:14 +0200994 return SN_ERR_INTERNAL;
995 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200996 else
997 return SN_ERR_INTERNAL; /* how did we get there ? */
998
999 /* process the case where the server requires the PROXY protocol to be sent */
1000 s->req->cons->send_proxy_ofs = 0;
1001 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
1002 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001003 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud02394b2012-05-11 18:32:18 +02001004 }
Willy Tarreau26d8c592012-05-07 18:12:14 +02001005
Willy Tarreaub1d67742010-03-29 19:36:59 +02001006 assign_tproxy_address(s);
1007
William Lallemandb7ff6a32012-03-02 14:35:21 +01001008 /* flag for logging source ip/port */
1009 if (s->fe->options2 & PR_O2_SRC_ADDR)
1010 s->req->cons->flags |= SI_FL_SRC_ADDR;
1011
Willy Tarreau14f8e862012-08-30 22:23:13 +02001012 /* disable lingering */
1013 if (s->be->options & PR_O_TCP_NOLING)
1014 s->req->cons->flags |= SI_FL_NOLINGER;
1015
Willy Tarreau73b013b2012-05-21 16:31:45 +02001016 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017
Willy Tarreau9650f372009-08-16 14:02:45 +02001018 if (err != SN_ERR_NONE)
1019 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001020
Willy Tarreau14f8e862012-08-30 22:23:13 +02001021 /* set connect timeout */
1022 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1023
Willy Tarreau827aee92011-03-10 16:55:02 +01001024 srv = target_srv(&s->target);
1025 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001026 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001027 srv->cur_sess++;
1028 if (srv->cur_sess > srv->counters.cur_sess_max)
1029 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001030 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001031 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 }
1033
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 return SN_ERR_NONE; /* connection is OK */
1035}
1036
1037
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038/* This function performs the "redispatch" part of a connection attempt. It
1039 * will assign a server if required, queue the connection if required, and
1040 * handle errors that might arise at this level. It can change the server
1041 * state. It will return 1 if it encounters an error, switches the server
1042 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1043 * that the connection is ready to use.
1044 */
1045
1046int srv_redispatch_connect(struct session *t)
1047{
Willy Tarreau827aee92011-03-10 16:55:02 +01001048 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049 int conn_err;
1050
1051 /* We know that we don't have any connection pending, so we will
1052 * try to get a new one, and wait in this state if it's queued
1053 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001054 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001055 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001056 srv = target_srv(&t->target);
1057
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 switch (conn_err) {
1059 case SRV_STATUS_OK:
1060 break;
1061
Willy Tarreau7c669d72008-06-20 15:04:11 +02001062 case SRV_STATUS_FULL:
1063 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1064 * and we can redispatch to another server, or it is not and we return
1065 * 503. This only makes sense in DIRECT mode however, because normal LB
1066 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001067 * would bring us on the same server again. Note that t->target is set
1068 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001069 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001070 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1071 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001072 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001073 goto redispatch;
1074 }
1075
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001076 if (!t->req->cons->err_type) {
1077 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001078 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001079 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001080
Willy Tarreau827aee92011-03-10 16:55:02 +01001081 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001082 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001083 return 1;
1084
Willy Tarreaubaaee002006-06-26 02:48:02 +02001085 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001086 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001087 if (!t->req->cons->err_type) {
1088 t->req->cons->err_type = SI_ET_CONN_ERR;
1089 t->req->cons->err_loc = NULL;
1090 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001091
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001092 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093 return 1;
1094
1095 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001096 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001097 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 /* do nothing else and do not wake any other session up */
1099 return 1;
1100
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101 case SRV_STATUS_INTERNAL:
1102 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001103 if (!t->req->cons->err_type) {
1104 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001105 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001106 }
1107
Willy Tarreau827aee92011-03-10 16:55:02 +01001108 if (srv)
1109 srv_inc_sess_ctr(srv);
1110 if (srv)
1111 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001112 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113
1114 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001115 if (may_dequeue_tasks(srv, t->be))
1116 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117 return 1;
1118 }
1119 /* if we get here, it's because we got SRV_STATUS_OK, which also
1120 * means that the connection has not been queued.
1121 */
1122 return 0;
1123}
1124
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001125/* Apply RDP cookie persistence to the current session. For this, the function
1126 * tries to extract an RDP cookie from the request buffer, and look for the
1127 * matching server in the list. If the server is found, it is assigned to the
1128 * session. This always returns 1, and the analyser removes itself from the
1129 * list. Nothing is performed if a server was already assigned.
1130 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001131int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001132{
1133 struct proxy *px = s->be;
1134 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001135 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001136 struct server *srv = px->srv;
1137 struct sockaddr_in addr;
1138 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001139 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001140
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001141 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 +02001142 now_ms, __FUNCTION__,
1143 s,
1144 req,
1145 req->rex, req->wex,
1146 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001147 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001148 req->analysers);
1149
1150 if (s->flags & SN_ASSIGNED)
1151 goto no_cookie;
1152
Willy Tarreau37406352012-04-23 16:16:37 +02001153 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001154
Willy Tarreau34db1082012-04-19 17:16:54 +02001155 args[0].type = ARGT_STR;
1156 args[0].data.str.str = s->be->rdp_cookie_name;
1157 args[0].data.str.len = s->be->rdp_cookie_len;
1158 args[1].type = ARGT_STOP;
1159
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001160 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001161 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001162 goto no_cookie;
1163
1164 memset(&addr, 0, sizeof(addr));
1165 addr.sin_family = AF_INET;
1166
Willy Tarreau664092c2011-12-16 19:11:42 +01001167 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001168 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001169 if (*p != '.')
1170 goto no_cookie;
1171 p++;
1172 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1173 if (*p != '.')
1174 goto no_cookie;
1175
Willy Tarreau9e000c62011-03-10 14:03:36 +01001176 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001177 while (srv) {
1178 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1179 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1180 /* we found the server and it is usable */
1181 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001182 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001183 break;
1184 }
1185 }
1186 srv = srv->next;
1187 }
1188
1189no_cookie:
1190 req->analysers &= ~an_bit;
1191 req->analyse_exp = TICK_ETERNITY;
1192 return 1;
1193}
1194
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001195int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001196 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001197 return px->down_time;
1198
1199 return now.tv_sec - px->last_change + px->down_time;
1200}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001202/*
1203 * This function returns a string containing the balancing
1204 * mode of the proxy in a format suitable for stats.
1205 */
1206
1207const char *backend_lb_algo_str(int algo) {
1208
1209 if (algo == BE_LB_ALGO_RR)
1210 return "roundrobin";
1211 else if (algo == BE_LB_ALGO_SRR)
1212 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001213 else if (algo == BE_LB_ALGO_FAS)
1214 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001215 else if (algo == BE_LB_ALGO_LC)
1216 return "leastconn";
1217 else if (algo == BE_LB_ALGO_SH)
1218 return "source";
1219 else if (algo == BE_LB_ALGO_UH)
1220 return "uri";
1221 else if (algo == BE_LB_ALGO_PH)
1222 return "url_param";
1223 else if (algo == BE_LB_ALGO_HH)
1224 return "hdr";
1225 else if (algo == BE_LB_ALGO_RCH)
1226 return "rdp-cookie";
1227 else
1228 return NULL;
1229}
1230
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001231/* This function parses a "balance" statement in a backend section describing
1232 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001233 * returns -1, it will write an error message into the <err> buffer which will
1234 * automatically be allocated and must be passed as NULL. The trailing '\n'
1235 * will not be written. The function must be called with <args> pointing to the
1236 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001237 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001238int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001239{
1240 if (!*(args[0])) {
1241 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001242 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1243 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001244 return 0;
1245 }
1246
1247 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001248 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1249 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001250 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001251 else if (!strcmp(args[0], "static-rr")) {
1252 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1253 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1254 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001255 else if (!strcmp(args[0], "first")) {
1256 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1257 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1258 }
Willy Tarreau51406232008-03-10 22:04:20 +01001259 else if (!strcmp(args[0], "leastconn")) {
1260 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1261 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1262 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001263 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001264 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1265 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001266 }
1267 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001268 int arg = 1;
1269
Willy Tarreau31682232007-11-29 15:38:04 +01001270 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1271 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001272
Oskar Stolc8dc41842012-05-19 10:19:54 +01001273 curproxy->uri_whole = 0;
1274
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001275 while (*args[arg]) {
1276 if (!strcmp(args[arg], "len")) {
1277 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001278 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001279 return -1;
1280 }
1281 curproxy->uri_len_limit = atoi(args[arg+1]);
1282 arg += 2;
1283 }
1284 else if (!strcmp(args[arg], "depth")) {
1285 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001286 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001287 return -1;
1288 }
1289 /* hint: we store the position of the ending '/' (depth+1) so
1290 * that we avoid a comparison while computing the hash.
1291 */
1292 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1293 arg += 2;
1294 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001295 else if (!strcmp(args[arg], "whole")) {
1296 curproxy->uri_whole = 1;
1297 arg += 1;
1298 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001299 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001300 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001301 return -1;
1302 }
1303 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001304 }
Willy Tarreau01732802007-11-01 22:48:15 +01001305 else if (!strcmp(args[0], "url_param")) {
1306 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001307 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001308 return -1;
1309 }
Willy Tarreau31682232007-11-29 15:38:04 +01001310 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1311 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001312
1313 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001314 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001315 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001316 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001317 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001318 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001319 return -1;
1320 }
1321 if (*args[3]) {
1322 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1323 curproxy->url_param_post_limit = str2ui(args[3]);
1324 }
1325 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1326 if (!curproxy->url_param_post_limit)
1327 curproxy->url_param_post_limit = 48;
1328 else if ( curproxy->url_param_post_limit < 3 )
1329 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1330 }
Benoitaffb4812009-03-25 13:02:10 +01001331 }
1332 else if (!strncmp(args[0], "hdr(", 4)) {
1333 const char *beg, *end;
1334
1335 beg = args[0] + 4;
1336 end = strchr(beg, ')');
1337
1338 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001339 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001340 return -1;
1341 }
1342
1343 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1344 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1345
1346 free(curproxy->hh_name);
1347 curproxy->hh_len = end - beg;
1348 curproxy->hh_name = my_strndup(beg, end - beg);
1349 curproxy->hh_match_domain = 0;
1350
1351 if (*args[1]) {
1352 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001353 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001354 return -1;
1355 }
1356 curproxy->hh_match_domain = 1;
1357 }
1358
Emeric Brun736aa232009-06-30 17:56:00 +02001359 }
1360 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1361 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1362 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1363
1364 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1365 const char *beg, *end;
1366
1367 beg = args[0] + 11;
1368 end = strchr(beg, ')');
1369
1370 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001371 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001372 return -1;
1373 }
1374
1375 free(curproxy->hh_name);
1376 curproxy->hh_name = my_strndup(beg, end - beg);
1377 curproxy->hh_len = end - beg;
1378 }
1379 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1380 free(curproxy->hh_name);
1381 curproxy->hh_name = strdup("mstshash");
1382 curproxy->hh_len = strlen(curproxy->hh_name);
1383 }
1384 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001385 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001386 return -1;
1387 }
Willy Tarreau01732802007-11-01 22:48:15 +01001388 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001389 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001390 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 +01001391 return -1;
1392 }
1393 return 0;
1394}
1395
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001396
1397/************************************************************************/
1398/* All supported keywords must be declared here. */
1399/************************************************************************/
1400
Willy Tarreau34db1082012-04-19 17:16:54 +02001401/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001402 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001403 * undefined behaviour.
1404 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001405static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001406acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001407 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001408{
Willy Tarreau37406352012-04-23 16:16:37 +02001409 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001410 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001411 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001412
1413 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001414 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001415 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001416 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001417 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001418 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001419
1420 return 1;
1421}
1422
Willy Tarreau37406352012-04-23 16:16:37 +02001423/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001424 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001425 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1426 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001427 */
1428static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001429acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001430 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001431{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001432 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001433
Willy Tarreau37406352012-04-23 16:16:37 +02001434 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001435 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001436 if (!(srv->state & SRV_MAINTAIN) &&
1437 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001438 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001439 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001440 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001441 return 1;
1442}
1443
Willy Tarreau34db1082012-04-19 17:16:54 +02001444/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001445 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001446 * undefined behaviour.
1447 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001448static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001449acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001450 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001451{
1452 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001453
Willy Tarreau37406352012-04-23 16:16:37 +02001454 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001455 smp->type = SMP_T_UINT;
1456 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001457
Willy Tarreau24e32d82012-04-23 23:55:44 +02001458 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001459 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001460 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001461
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001462 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001463 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001464 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001465 return 1;
1466 }
1467
Willy Tarreauf853c462012-04-23 18:53:56 +02001468 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001469 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001470 }
1471
1472 return 1;
1473}
1474
Willy Tarreaua5e37562011-12-16 17:06:15 +01001475/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001476static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001477acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001478 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001479{
Willy Tarreauf853c462012-04-23 18:53:56 +02001480 smp->flags = SMP_F_VOL_TXN;
1481 smp->type = SMP_T_UINT;
1482 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001483 return 1;
1484}
1485
Willy Tarreaua5e37562011-12-16 17:06:15 +01001486/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001487static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001488acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001489 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001490{
Willy Tarreau827aee92011-03-10 16:55:02 +01001491 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001492 return 0;
1493
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->type = SMP_T_UINT;
1495 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001496
1497 return 1;
1498}
1499
Willy Tarreau34db1082012-04-19 17:16:54 +02001500/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001501 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001502 * undefined behaviour.
1503 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001504static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001505acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001506 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001507{
Willy Tarreau37406352012-04-23 16:16:37 +02001508 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001509 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001510 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001511 return 1;
1512}
1513
Willy Tarreau34db1082012-04-19 17:16:54 +02001514/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001515 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001516 * undefined behaviour.
1517 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001518static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001519acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001520 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001521{
Willy Tarreau37406352012-04-23 16:16:37 +02001522 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001523 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001524 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001525 return 1;
1526}
1527
Willy Tarreau34db1082012-04-19 17:16:54 +02001528/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001529 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001530 * undefined behaviour.
1531 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001532static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001533acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001534 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001535{
Willy Tarreau37406352012-04-23 16:16:37 +02001536 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001537 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001538 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001539 return 1;
1540}
1541
Willy Tarreaua5e37562011-12-16 17:06:15 +01001542/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001543 * by the number of running servers and rounded up. If there is no running
1544 * server, we return twice the total, just as if we had half a running server.
1545 * This is more or less correct anyway, since we expect the last server to come
1546 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001547 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001548 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001549 */
1550static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001551acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001552 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001553{
1554 int nbsrv;
1555
Willy Tarreau37406352012-04-23 16:16:37 +02001556 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001557 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001558 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001559
1560 if (px->srv_act)
1561 nbsrv = px->srv_act;
1562 else if (px->lbprm.fbck)
1563 nbsrv = 1;
1564 else
1565 nbsrv = px->srv_bck;
1566
1567 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001568 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001569 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001570 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001571
1572 return 1;
1573}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001574
Willy Tarreau34db1082012-04-19 17:16:54 +02001575/* set temp integer to the number of concurrent connections on the server in the backend.
1576 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1577 * undefined behaviour.
1578 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001579static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001580acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001581 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001582{
Willy Tarreauf853c462012-04-23 18:53:56 +02001583 smp->flags = SMP_F_VOL_TEST;
1584 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001585 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001586 return 1;
1587}
1588
Willy Tarreau61612d42012-04-19 18:42:05 +02001589/* Note: must not be declared <const> as its list will be overwritten.
1590 * Please take care of keeping this list alphabetically sorted.
1591 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001592static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001593 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1594 { "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 +02001595 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001596 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1597 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1598 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1599 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001600 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1601 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1602 { "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 +01001603 { NULL, NULL, NULL, NULL },
1604}};
1605
1606
1607__attribute__((constructor))
1608static void __backend_init(void)
1609{
1610 acl_register_keywords(&acl_kws);
1611}
1612
Willy Tarreaubaaee002006-06-26 02:48:02 +02001613/*
1614 * Local variables:
1615 * c-indent-level: 8
1616 * c-basic-offset: 8
1617 * End:
1618 */