blob: 48c87617f7c982620cc557d53fb919ef622ad59a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau1a7eca12013-01-07 22:38:03 +01004 * Copyright 2000-2013 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 Tarreaud4c33c82013-01-07 21:59:07 +010042#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020045#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010047#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020049#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020050#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010051#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/task.h>
53
Willy Tarreaubaaee002006-06-26 02:48:02 +020054/*
55 * This function recounts the number of usable active and backup servers for
56 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010057 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010058 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010059 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020061void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020062{
63 struct server *srv;
64
Willy Tarreau20697042007-11-15 23:26:18 +010065 px->srv_act = px->srv_bck = 0;
66 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010067 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010069 if (!srv_is_usable(srv->state, srv->eweight))
70 continue;
71
72 if (srv->state & SRV_BACKUP) {
73 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010074 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010075 px->lbprm.fbck = srv;
76 px->srv_bck++;
77 px->lbprm.tot_wbck += srv->eweight;
78 } else {
79 px->srv_act++;
80 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 }
82 }
Willy Tarreaub625a082007-11-26 01:15:43 +010083}
Willy Tarreau20697042007-11-15 23:26:18 +010084
Willy Tarreaub625a082007-11-26 01:15:43 +010085/* This function simply updates the backend's tot_weight and tot_used values
86 * after servers weights have been updated. It is designed to be used after
87 * recount_servers() or equivalent.
88 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020089void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010090{
Willy Tarreau20697042007-11-15 23:26:18 +010091 if (px->srv_act) {
92 px->lbprm.tot_weight = px->lbprm.tot_wact;
93 px->lbprm.tot_used = px->srv_act;
94 }
Willy Tarreaub625a082007-11-26 01:15:43 +010095 else if (px->lbprm.fbck) {
96 /* use only the first backup server */
97 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
98 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010099 }
100 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100101 px->lbprm.tot_weight = px->lbprm.tot_wbck;
102 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100103 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100104}
105
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200106/*
107 * This function tries to find a running server for the proxy <px> following
108 * the source hash method. Depending on the number of active/backup servers,
109 * it will either look for active servers, or for backup servers.
110 * If any server is found, it will be returned. If no valid server is found,
111 * NULL is returned.
112 */
113struct server *get_server_sh(struct proxy *px, const char *addr, int len)
114{
115 unsigned int h, l;
116
117 if (px->lbprm.tot_weight == 0)
118 return NULL;
119
120 l = h = 0;
121
122 /* note: we won't hash if there's only one server left */
123 if (px->lbprm.tot_used == 1)
124 goto hash_done;
125
126 while ((l + sizeof (int)) <= len) {
127 h ^= ntohl(*(unsigned int *)(&addr[l]));
128 l += sizeof (int);
129 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100130 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
131 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200132 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200133 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
134 return chash_get_server_hash(px, h);
135 else
136 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200137}
138
139/*
140 * This function tries to find a running server for the proxy <px> following
141 * the URI hash method. In order to optimize cache hits, the hash computation
142 * ends at the question mark. Depending on the number of active/backup servers,
143 * it will either look for active servers, or for backup servers.
144 * If any server is found, it will be returned. If no valid server is found,
145 * NULL is returned.
146 *
147 * This code was contributed by Guillaume Dallaire, who also selected this hash
148 * algorithm out of a tens because it gave him the best results.
149 *
150 */
151struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
152{
153 unsigned long hash = 0;
154 int c;
155 int slashes = 0;
156
157 if (px->lbprm.tot_weight == 0)
158 return NULL;
159
160 /* note: we won't hash if there's only one server left */
161 if (px->lbprm.tot_used == 1)
162 goto hash_done;
163
164 if (px->uri_len_limit)
165 uri_len = MIN(uri_len, px->uri_len_limit);
166
167 while (uri_len--) {
168 c = *uri++;
169 if (c == '/') {
170 slashes++;
171 if (slashes == px->uri_dirs_depth1) /* depth+1 */
172 break;
173 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100174 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200175 break;
176
177 hash = c + (hash << 6) + (hash << 16) - hash;
178 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100179 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
180 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200181 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200182 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
183 return chash_get_server_hash(px, hash);
184 else
185 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200186}
187
Willy Tarreau01732802007-11-01 22:48:15 +0100188/*
189 * This function tries to find a running server for the proxy <px> following
190 * the URL parameter hash method. It looks for a specific parameter in the
191 * URL and hashes it to compute the server ID. This is useful to optimize
192 * performance by avoiding bounces between servers in contexts where sessions
193 * are shared but cookies are not usable. If the parameter is not found, NULL
194 * is returned. If any server is found, it will be returned. If no valid server
195 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100196 */
197struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
198{
199 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 const char *p;
201 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100202 int plen;
203
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200204 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100205 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100206 return NULL;
207
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200208 if ((p = memchr(uri, '?', uri_len)) == NULL)
209 return NULL;
210
Willy Tarreau01732802007-11-01 22:48:15 +0100211 p++;
212
213 uri_len -= (p - uri);
214 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200215 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100216
217 while (uri_len > plen) {
218 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200219 if (params[plen] == '=') {
220 if (memcmp(params, px->url_param_name, plen) == 0) {
221 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100222 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200223 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100224 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200225 p += plen + 1;
226 uri_len -= plen + 1;
227
Willy Tarreau01732802007-11-01 22:48:15 +0100228 while (uri_len && *p != '&') {
229 hash = *p + (hash << 6) + (hash << 16) - hash;
230 uri_len--;
231 p++;
232 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100233 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
234 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200235 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
236 return chash_get_server_hash(px, hash);
237 else
238 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100239 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200240 }
241 /* skip to next parameter */
242 p = memchr(params, '&', uri_len);
243 if (!p)
244 return NULL;
245 p++;
246 uri_len -= (p - params);
247 params = p;
248 }
249 return NULL;
250}
251
252/*
253 * this does the same as the previous server_ph, but check the body contents
254 */
255struct server *get_server_ph_post(struct session *s)
256{
257 unsigned long hash = 0;
258 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200259 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 struct http_msg *msg = &txn->req;
261 struct proxy *px = s->be;
262 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100263 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200264 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266
Willy Tarreau9b28e032012-10-12 23:49:43 +0200267 if (len > buffer_len(req->buf) - msg->sov)
268 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269
Willy Tarreau157dd632009-12-06 19:18:09 +0100270 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271 return NULL;
272
Willy Tarreau157dd632009-12-06 19:18:09 +0100273 if (px->lbprm.tot_weight == 0)
274 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200275
276 while (len > plen) {
277 /* Look for the parameter name followed by an equal symbol */
278 if (params[plen] == '=') {
279 if (memcmp(params, px->url_param_name, plen) == 0) {
280 /* OK, we have the parameter here at <params>, and
281 * the value after the equal sign, at <p>
282 * skip the equal symbol
283 */
284 p += plen + 1;
285 len -= plen + 1;
286
287 while (len && *p != '&') {
288 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100289 /* if in a POST, body must be URI encoded or it's not a URI.
290 * Do not interprete any possible binary data as a parameter.
291 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
293 break;
294 return NULL; /* oh, no; this is not uri-encoded.
295 * This body does not contain parameters.
296 */
297 }
298 hash = *p + (hash << 6) + (hash << 16) - hash;
299 len--;
300 p++;
301 /* should we break if vlen exceeds limit? */
302 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100303 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
304 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200305 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
306 return chash_get_server_hash(px, hash);
307 else
308 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 }
310 }
Willy Tarreau01732802007-11-01 22:48:15 +0100311 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100313 if (!p)
314 return NULL;
315 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200316 len -= (p - params);
317 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100318 }
319 return NULL;
320}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200322
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323/*
Benoitaffb4812009-03-25 13:02:10 +0100324 * This function tries to find a running server for the proxy <px> following
325 * the Header parameter hash method. It looks for a specific parameter in the
326 * URL and hashes it to compute the server ID. This is useful to optimize
327 * performance by avoiding bounces between servers in contexts where sessions
328 * are shared but cookies are not usable. If the parameter is not found, NULL
329 * is returned. If any server is found, it will be returned. If no valid server
330 * is found, NULL is returned.
331 */
332struct server *get_server_hh(struct session *s)
333{
334 unsigned long hash = 0;
335 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100336 struct proxy *px = s->be;
337 unsigned int plen = px->hh_len;
338 unsigned long len;
339 struct hdr_ctx ctx;
340 const char *p;
341
342 /* tot_weight appears to mean srv_count */
343 if (px->lbprm.tot_weight == 0)
344 return NULL;
345
Benoitaffb4812009-03-25 13:02:10 +0100346 ctx.idx = 0;
347
348 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200349 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 +0100350
351 /* if the header is not found or empty, let's fallback to round robin */
352 if (!ctx.idx || !ctx.vlen)
353 return NULL;
354
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200355 /* note: we won't hash if there's only one server left */
356 if (px->lbprm.tot_used == 1)
357 goto hash_done;
358
Benoitaffb4812009-03-25 13:02:10 +0100359 /* Found a the hh_name in the headers.
360 * we will compute the hash based on this value ctx.val.
361 */
362 len = ctx.vlen;
363 p = (char *)ctx.line + ctx.val;
364 if (!px->hh_match_domain) {
365 while (len) {
366 hash = *p + (hash << 6) + (hash << 16) - hash;
367 len--;
368 p++;
369 }
370 } else {
371 int dohash = 0;
372 p += len - 1;
373 /* special computation, use only main domain name, not tld/host
374 * going back from the end of string, start hashing at first
375 * dot stop at next.
376 * This is designed to work with the 'Host' header, and requires
377 * a special option to activate this.
378 */
379 while (len) {
380 if (*p == '.') {
381 if (!dohash)
382 dohash = 1;
383 else
384 break;
385 } else {
386 if (dohash)
387 hash = *p + (hash << 6) + (hash << 16) - hash;
388 }
389 len--;
390 p--;
391 }
392 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100393 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
394 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200395 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200396 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
397 return chash_get_server_hash(px, hash);
398 else
399 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100400}
401
Willy Tarreau34db1082012-04-19 17:16:54 +0200402/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200403struct server *get_server_rch(struct session *s)
404{
405 unsigned long hash = 0;
406 struct proxy *px = s->be;
407 unsigned long len;
408 const char *p;
409 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200410 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200411 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200412
413 /* tot_weight appears to mean srv_count */
414 if (px->lbprm.tot_weight == 0)
415 return NULL;
416
Willy Tarreau37406352012-04-23 16:16:37 +0200417 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200418
Willy Tarreau9b28e032012-10-12 23:49:43 +0200419 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200420
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200421 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200422 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100423
Willy Tarreau9b28e032012-10-12 23:49:43 +0200424 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200425
Willy Tarreau37406352012-04-23 16:16:37 +0200426 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200427 return NULL;
428
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200429 /* note: we won't hash if there's only one server left */
430 if (px->lbprm.tot_used == 1)
431 goto hash_done;
432
Emeric Brun736aa232009-06-30 17:56:00 +0200433 /* Found a the hh_name in the headers.
434 * we will compute the hash based on this value ctx.val.
435 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200436 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200437 while (len) {
438 hash = *p + (hash << 6) + (hash << 16) - hash;
439 len--;
440 p++;
441 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100442 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
443 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200444 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200445 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
446 return chash_get_server_hash(px, hash);
447 else
448 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200449}
Benoitaffb4812009-03-25 13:02:10 +0100450
451/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200452 * This function applies the load-balancing algorithm to the session, as
453 * defined by the backend it is assigned to. The session is then marked as
454 * 'assigned'.
455 *
456 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
457 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100458 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200459 * The function tries to keep the original connection slot if it reconnects to
460 * the same server, otherwise it releases it and tries to offer it.
461 *
462 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 *
464 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100465 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200466 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
467 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468 * SRV_STATUS_INTERNAL for other unrecoverable errors.
469 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100471 * it does not need to be called anymore. This means that target_srv(&s->target)
472 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 *
474 */
475
476int assign_server(struct session *s)
477{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100478
Willy Tarreau7c669d72008-06-20 15:04:11 +0200479 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100480 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100482
Simon Horman8effd3d2011-08-13 08:03:52 +0900483 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 err = SRV_STATUS_INTERNAL;
486 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
487 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100488
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100489 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200490 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491
Willy Tarreau7c669d72008-06-20 15:04:11 +0200492 /* We have to release any connection slot before applying any LB algo,
493 * otherwise we may erroneously end up with no available slot.
494 */
495 if (conn_slot)
496 sess_change_server(s, NULL);
497
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100498 /* We will now try to find the good server and store it into <objt_server(s->target)>.
499 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 * as well as if no server is available (check error code).
501 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100502
Willy Tarreau827aee92011-03-10 16:55:02 +0100503 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100504 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100505
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200506 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200507 /* we must check if we have at least one server available */
508 if (!s->be->lbprm.tot_weight) {
509 err = SRV_STATUS_NOSRV;
510 goto out;
511 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200513 /* First check whether we need to fetch some data or simply call
514 * the LB lookup function. Only the hashing functions will need
515 * some input data in fact, and will support multiple algorithms.
516 */
517 switch (s->be->lbprm.algo & BE_LB_LKUP) {
518 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100519 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200521
Willy Tarreauf09c6602012-02-13 17:12:08 +0100522 case BE_LB_LKUP_FSTREE:
523 srv = fas_get_next_server(s->be, prev_srv);
524 break;
525
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200526 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200528 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200530 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200531 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200532 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200533 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100534 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200535 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100536 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200537 break;
538 }
539 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200541 err = SRV_STATUS_INTERNAL;
542 goto out;
543 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200544
545 switch (s->be->lbprm.algo & BE_LB_PARM) {
546 case BE_LB_HASH_SRC:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200547 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200548 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200549 (void *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200550 4);
551 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200552 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200553 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200554 (void *)&((struct sockaddr_in6 *)&s->req->prod->conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200555 16);
556 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200557 else {
558 /* unknown IP family */
559 err = SRV_STATUS_INTERNAL;
560 goto out;
561 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200562 break;
563
564 case BE_LB_HASH_URI:
565 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100566 if (s->txn.req.msg_state < HTTP_MSG_BODY)
567 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100568 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200569 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100570 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200572
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 case BE_LB_HASH_PRM:
574 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100575 if (s->txn.req.msg_state < HTTP_MSG_BODY)
576 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100577
Willy Tarreau827aee92011-03-10 16:55:02 +0100578 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200579 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100580 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100581
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 if (!srv && s->txn.meth == HTTP_METH_POST)
583 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200584 break;
Benoitaffb4812009-03-25 13:02:10 +0100585
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 case BE_LB_HASH_HDR:
587 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100588 if (s->txn.req.msg_state < HTTP_MSG_BODY)
589 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100590 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 break;
592
593 case BE_LB_HASH_RDP:
594 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100595 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596 break;
597
598 default:
599 /* unknown balancing algorithm */
600 err = SRV_STATUS_INTERNAL;
601 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100602 }
Emeric Brun736aa232009-06-30 17:56:00 +0200603
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 /* If the hashing parameter was not found, let's fall
605 * back to round robin on the map.
606 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200610 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200612 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613
614 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200615 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
Willy Tarreau7c669d72008-06-20 15:04:11 +0200617 default:
618 /* unknown balancing algorithm */
619 err = SRV_STATUS_INTERNAL;
620 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200624 err = SRV_STATUS_FULL;
625 goto out;
626 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100628 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100630 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100631 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200632 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200633 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100634 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100636 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200637 is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100638 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100639 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100640 }
641 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200642 err = SRV_STATUS_NOSRV;
643 goto out;
644 }
645
646 s->flags |= SN_ASSIGNED;
647 err = SRV_STATUS_OK;
648 out:
649
650 /* Either we take back our connection slot, or we offer it to someone
651 * else if we don't need it anymore.
652 */
653 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 if (conn_slot == srv) {
655 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200656 } else {
657 if (may_dequeue_tasks(conn_slot, s->be))
658 process_srv_queue(conn_slot);
659 }
660 }
661
662 out_err:
663 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664}
665
666
667/*
668 * This function assigns a server address to a session, and sets SN_ADDR_SET.
669 * The address is taken from the currently assigned server, or from the
670 * dispatch or transparent address.
671 *
672 * It may return :
673 * SRV_STATUS_OK if everything is OK.
674 * SRV_STATUS_INTERNAL for other unrecoverable errors.
675 *
676 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
677 * not cleared, so it's to the caller to clear it if required.
678 *
679 */
680int assign_server_address(struct session *s)
681{
682#ifdef DEBUG_FULL
683 fprintf(stderr,"assign_server_address : s=%p\n",s);
684#endif
685
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200686 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 /* A server is necessarily known for this session */
688 if (!(s->flags & SN_ASSIGNED))
689 return SRV_STATUS_INTERNAL;
690
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100691 s->req->cons->conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200693 if (!is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200694 /* if the server has no address, we use the same address
695 * the client asked, which is handy for remapping ports
696 * locally on multiple addresses at once.
697 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200698 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200699 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200700
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200701 if (s->req->prod->conn->addr.to.ss_family == AF_INET) {
702 ((struct sockaddr_in *)&s->req->cons->conn->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
703 } else if (s->req->prod->conn->addr.to.ss_family == AF_INET6) {
704 ((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 +0200705 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200706 }
707
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 /* if this server remaps proxied ports, we'll use
709 * the port the client connected to with an offset. */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100710 if (objt_server(s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100711 int base_port;
712
Willy Tarreau9b061e32012-04-07 18:03:52 +0200713 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200714 conn_get_to_addr(s->req->prod->conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715
716 /* First, retrieve the port from the incoming connection */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200717 base_port = get_host_port(&s->req->prod->conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100718
719 /* Second, assign the outgoing connection's port */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200720 base_port += get_host_port(&s->req->cons->conn->addr.to);
721 set_host_port(&s->req->cons->conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 }
723 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200724 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 /* connect to the defined dispatch addr */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200726 s->req->cons->conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100728 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200730 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200732 if (s->req->prod->conn->addr.to.ss_family == AF_INET || s->req->prod->conn->addr.to.ss_family == AF_INET6) {
733 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 +0200734 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100735 /* when we support IPv6 on the backend, we may add other tests */
736 //qfprintf(stderr, "Cannot get original server address.\n");
737 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100739 else if (s->be->options & PR_O_HTTP_PROXY) {
740 /* If HTTP PROXY option is set, then server is already assigned
741 * during incoming client request parsing. */
742 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100743 else {
744 /* no server and no LB algorithm ! */
745 return SRV_STATUS_INTERNAL;
746 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
748 s->flags |= SN_ADDR_SET;
749 return SRV_STATUS_OK;
750}
751
752
753/* This function assigns a server to session <s> if required, and can add the
754 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200755 * If ->srv_conn is set, the session is first released from the server.
756 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
757 * be called before any connection and after any retry or redispatch occurs.
758 *
759 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 *
761 * Returns :
762 *
763 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100764 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765 * SRV_STATUS_QUEUED if the connection has been queued.
766 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200768 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 * SRV_STATUS_INTERNAL for other unrecoverable errors.
770 *
771 */
772int assign_server_and_queue(struct session *s)
773{
774 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 int err;
777
778 if (s->pend_pos)
779 return SRV_STATUS_INTERNAL;
780
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 err = SRV_STATUS_OK;
782 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100783 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100784
Willy Tarreau7c669d72008-06-20 15:04:11 +0200785 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100786 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200787 /* This session was previously assigned to a server. We have to
788 * update the session's and the server's stats :
789 * - if the server changed :
790 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
791 * - set SN_REDISP if it was successfully redispatched
792 * - increment srv->redispatches and be->redispatches
793 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100794 */
795
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100796 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200797 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
798 s->txn.flags &= ~TX_CK_MASK;
799 s->txn.flags |= TX_CK_DOWN;
800 }
801 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100802 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100803 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200804 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100805 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100806 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100807 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100808 }
809 }
810
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 switch (err) {
812 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100814 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816 return SRV_STATUS_OK; /* dispatch or proxy mode */
817
818 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 return SRV_STATUS_OK;
821
822 /* OK, this session already has an assigned server, but no
823 * connection slot yet. Either it is a redispatch, or it was
824 * assigned from persistence information (direct mode).
825 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200827 /* server scheduled for redirection, and already assigned. We
828 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100830 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100831 return SRV_STATUS_OK;
832 }
833
Willy Tarreau7c669d72008-06-20 15:04:11 +0200834 /* We might have to queue this session if the assigned server is full.
835 * We know we have to queue it into the server's queue, so if a maxqueue
836 * is set on the server, we must also check that the server's queue is
837 * not full, in which case we have to return FULL.
838 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100839 if (srv->maxconn &&
840 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200843 return SRV_STATUS_FULL;
844
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 p = pendconn_add(s);
846 if (p)
847 return SRV_STATUS_QUEUED;
848 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200849 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200851
852 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100853 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 return SRV_STATUS_OK;
855
856 case SRV_STATUS_FULL:
857 /* queue this session into the proxy's queue */
858 p = pendconn_add(s);
859 if (p)
860 return SRV_STATUS_QUEUED;
861 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863
864 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865 return err;
866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 case SRV_STATUS_INTERNAL:
868 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 default:
871 return SRV_STATUS_INTERNAL;
872 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100873}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875/* If an explicit source binding is specified on the server and/or backend, and
876 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200877 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200878 */
879static void assign_tproxy_address(struct session *s)
880{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200881#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100882 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100883 struct conn_src *src;
Willy Tarreau827aee92011-03-10 16:55:02 +0100884
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100885 if (srv && srv->conn_src.opts & CO_SRC_BIND)
886 src = &srv->conn_src;
887 else if (s->be->conn_src.opts & CO_SRC_BIND)
888 src = &s->be->conn_src;
889 else
890 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100891
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100892 switch (src->opts & CO_SRC_TPROXY_MASK) {
893 case CO_SRC_TPROXY_ADDR:
894 s->req->cons->conn->addr.from = src->tproxy_addr;
895 break;
896 case CO_SRC_TPROXY_CLI:
897 case CO_SRC_TPROXY_CIP:
898 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
899 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
900 break;
901 case CO_SRC_TPROXY_DYN:
902 if (src->bind_hdr_occ) {
903 char *vptr;
904 int vlen;
905 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100906
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100907 /* bind to the IP in a header */
908 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
909 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
910 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100911
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100912 b_rew(s->req->buf, rewind = s->req->buf->o);
913 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
914 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
915 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
916 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200917 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100918 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200919 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100920 break;
921 default:
922 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200923 }
924#endif
925}
926
927
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928/*
929 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200930 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100931 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 * It can return one of :
933 * - SN_ERR_NONE if everything's OK
934 * - SN_ERR_SRVTO if there are no more servers
935 * - SN_ERR_SRVCL if the connection was refused by the server
936 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
937 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
938 * - SN_ERR_INTERNAL for any other purely internal errors
939 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
940 */
941int connect_server(struct session *s)
942{
Willy Tarreau827aee92011-03-10 16:55:02 +0100943 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200944 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945
946 if (!(s->flags & SN_ADDR_SET)) {
947 err = assign_server_address(s);
948 if (err != SRV_STATUS_OK)
949 return SN_ERR_INTERNAL;
950 }
951
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200952 /* the target was only on the session, assign it to the SI now */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100953 s->req->cons->conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200954
Willy Tarreau26d8c592012-05-07 18:12:14 +0200955 /* set the correct protocol on the output stream interface */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100956 if (objt_server(s->target)) {
957 si_prepare_conn(s->req->cons, objt_server(s->target)->proto, objt_server(s->target)->xprt);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200958 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100959 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200960 /* proxies exclusively run on raw_sock right now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200961 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 +0200962 if (!si_ctrl(s->req->cons))
Willy Tarreau26d8c592012-05-07 18:12:14 +0200963 return SN_ERR_INTERNAL;
964 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200965 else
966 return SN_ERR_INTERNAL; /* how did we get there ? */
967
968 /* process the case where the server requires the PROXY protocol to be sent */
969 s->req->cons->send_proxy_ofs = 0;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100970 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200971 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200972 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200973 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200974
Willy Tarreaub1d67742010-03-29 19:36:59 +0200975 assign_tproxy_address(s);
976
William Lallemandb7ff6a32012-03-02 14:35:21 +0100977 /* flag for logging source ip/port */
978 if (s->fe->options2 & PR_O2_SRC_ADDR)
979 s->req->cons->flags |= SI_FL_SRC_ADDR;
980
Willy Tarreau14f8e862012-08-30 22:23:13 +0200981 /* disable lingering */
982 if (s->be->options & PR_O_TCP_NOLING)
983 s->req->cons->flags |= SI_FL_NOLINGER;
984
Willy Tarreau73b013b2012-05-21 16:31:45 +0200985 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986
Willy Tarreau9650f372009-08-16 14:02:45 +0200987 if (err != SN_ERR_NONE)
988 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200989
Willy Tarreau14f8e862012-08-30 22:23:13 +0200990 /* set connect timeout */
991 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
992
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100993 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100994 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100995 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100996 srv->cur_sess++;
997 if (srv->cur_sess > srv->counters.cur_sess_max)
998 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100999 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001000 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 }
1002
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003 return SN_ERR_NONE; /* connection is OK */
1004}
1005
1006
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007/* This function performs the "redispatch" part of a connection attempt. It
1008 * will assign a server if required, queue the connection if required, and
1009 * handle errors that might arise at this level. It can change the server
1010 * state. It will return 1 if it encounters an error, switches the server
1011 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1012 * that the connection is ready to use.
1013 */
1014
1015int srv_redispatch_connect(struct session *t)
1016{
Willy Tarreau827aee92011-03-10 16:55:02 +01001017 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 int conn_err;
1019
1020 /* We know that we don't have any connection pending, so we will
1021 * try to get a new one, and wait in this state if it's queued
1022 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001023 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001025 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001026
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027 switch (conn_err) {
1028 case SRV_STATUS_OK:
1029 break;
1030
Willy Tarreau7c669d72008-06-20 15:04:11 +02001031 case SRV_STATUS_FULL:
1032 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1033 * and we can redispatch to another server, or it is not and we return
1034 * 503. This only makes sense in DIRECT mode however, because normal LB
1035 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001036 * would bring us on the same server again. Note that t->target is set
1037 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001038 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001039 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1040 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001041 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001042 goto redispatch;
1043 }
1044
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001045 if (!t->req->cons->err_type) {
1046 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001047 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001048 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001049
Willy Tarreau827aee92011-03-10 16:55:02 +01001050 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001051 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001052 return 1;
1053
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001055 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001056 if (!t->req->cons->err_type) {
1057 t->req->cons->err_type = SI_ET_CONN_ERR;
1058 t->req->cons->err_loc = NULL;
1059 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001060
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001061 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 return 1;
1063
1064 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001065 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001066 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067 /* do nothing else and do not wake any other session up */
1068 return 1;
1069
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 case SRV_STATUS_INTERNAL:
1071 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001072 if (!t->req->cons->err_type) {
1073 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001075 }
1076
Willy Tarreau827aee92011-03-10 16:55:02 +01001077 if (srv)
1078 srv_inc_sess_ctr(srv);
1079 if (srv)
1080 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001081 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001082
1083 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001084 if (may_dequeue_tasks(srv, t->be))
1085 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086 return 1;
1087 }
1088 /* if we get here, it's because we got SRV_STATUS_OK, which also
1089 * means that the connection has not been queued.
1090 */
1091 return 0;
1092}
1093
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001094/* Apply RDP cookie persistence to the current session. For this, the function
1095 * tries to extract an RDP cookie from the request buffer, and look for the
1096 * matching server in the list. If the server is found, it is assigned to the
1097 * session. This always returns 1, and the analyser removes itself from the
1098 * list. Nothing is performed if a server was already assigned.
1099 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001100int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001101{
1102 struct proxy *px = s->be;
1103 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001104 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001105 struct server *srv = px->srv;
1106 struct sockaddr_in addr;
1107 char *p;
1108
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001109 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 +02001110 now_ms, __FUNCTION__,
1111 s,
1112 req,
1113 req->rex, req->wex,
1114 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001115 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001116 req->analysers);
1117
1118 if (s->flags & SN_ASSIGNED)
1119 goto no_cookie;
1120
Willy Tarreau37406352012-04-23 16:16:37 +02001121 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001122
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001123 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001124 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001125 goto no_cookie;
1126
1127 memset(&addr, 0, sizeof(addr));
1128 addr.sin_family = AF_INET;
1129
Willy Tarreau664092c2011-12-16 19:11:42 +01001130 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001131 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001132 if (*p != '.')
1133 goto no_cookie;
1134 p++;
1135 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1136 if (*p != '.')
1137 goto no_cookie;
1138
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001139 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001140 while (srv) {
1141 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1142 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1143 /* we found the server and it is usable */
1144 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001145 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001146 break;
1147 }
1148 }
1149 srv = srv->next;
1150 }
1151
1152no_cookie:
1153 req->analysers &= ~an_bit;
1154 req->analyse_exp = TICK_ETERNITY;
1155 return 1;
1156}
1157
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001158int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001159 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001160 return px->down_time;
1161
1162 return now.tv_sec - px->last_change + px->down_time;
1163}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001165/*
1166 * This function returns a string containing the balancing
1167 * mode of the proxy in a format suitable for stats.
1168 */
1169
1170const char *backend_lb_algo_str(int algo) {
1171
1172 if (algo == BE_LB_ALGO_RR)
1173 return "roundrobin";
1174 else if (algo == BE_LB_ALGO_SRR)
1175 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001176 else if (algo == BE_LB_ALGO_FAS)
1177 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001178 else if (algo == BE_LB_ALGO_LC)
1179 return "leastconn";
1180 else if (algo == BE_LB_ALGO_SH)
1181 return "source";
1182 else if (algo == BE_LB_ALGO_UH)
1183 return "uri";
1184 else if (algo == BE_LB_ALGO_PH)
1185 return "url_param";
1186 else if (algo == BE_LB_ALGO_HH)
1187 return "hdr";
1188 else if (algo == BE_LB_ALGO_RCH)
1189 return "rdp-cookie";
1190 else
1191 return NULL;
1192}
1193
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001194/* This function parses a "balance" statement in a backend section describing
1195 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001196 * returns -1, it will write an error message into the <err> buffer which will
1197 * automatically be allocated and must be passed as NULL. The trailing '\n'
1198 * will not be written. The function must be called with <args> pointing to the
1199 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001200 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001201int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001202{
1203 if (!*(args[0])) {
1204 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001205 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1206 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001207 return 0;
1208 }
1209
1210 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001211 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1212 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001213 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001214 else if (!strcmp(args[0], "static-rr")) {
1215 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1216 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1217 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001218 else if (!strcmp(args[0], "first")) {
1219 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1220 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1221 }
Willy Tarreau51406232008-03-10 22:04:20 +01001222 else if (!strcmp(args[0], "leastconn")) {
1223 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1224 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1225 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001226 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001227 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1228 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001229 }
1230 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001231 int arg = 1;
1232
Willy Tarreau31682232007-11-29 15:38:04 +01001233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001235
Oskar Stolc8dc41842012-05-19 10:19:54 +01001236 curproxy->uri_whole = 0;
1237
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001238 while (*args[arg]) {
1239 if (!strcmp(args[arg], "len")) {
1240 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001241 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001242 return -1;
1243 }
1244 curproxy->uri_len_limit = atoi(args[arg+1]);
1245 arg += 2;
1246 }
1247 else if (!strcmp(args[arg], "depth")) {
1248 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001249 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001250 return -1;
1251 }
1252 /* hint: we store the position of the ending '/' (depth+1) so
1253 * that we avoid a comparison while computing the hash.
1254 */
1255 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1256 arg += 2;
1257 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001258 else if (!strcmp(args[arg], "whole")) {
1259 curproxy->uri_whole = 1;
1260 arg += 1;
1261 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001262 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001263 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001264 return -1;
1265 }
1266 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001267 }
Willy Tarreau01732802007-11-01 22:48:15 +01001268 else if (!strcmp(args[0], "url_param")) {
1269 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001270 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001271 return -1;
1272 }
Willy Tarreau31682232007-11-29 15:38:04 +01001273 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1274 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001275
1276 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001277 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001278 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001279 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001280 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001281 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001282 return -1;
1283 }
1284 if (*args[3]) {
1285 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1286 curproxy->url_param_post_limit = str2ui(args[3]);
1287 }
1288 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1289 if (!curproxy->url_param_post_limit)
1290 curproxy->url_param_post_limit = 48;
1291 else if ( curproxy->url_param_post_limit < 3 )
1292 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1293 }
Benoitaffb4812009-03-25 13:02:10 +01001294 }
1295 else if (!strncmp(args[0], "hdr(", 4)) {
1296 const char *beg, *end;
1297
1298 beg = args[0] + 4;
1299 end = strchr(beg, ')');
1300
1301 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001302 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001303 return -1;
1304 }
1305
1306 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1307 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1308
1309 free(curproxy->hh_name);
1310 curproxy->hh_len = end - beg;
1311 curproxy->hh_name = my_strndup(beg, end - beg);
1312 curproxy->hh_match_domain = 0;
1313
1314 if (*args[1]) {
1315 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001316 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001317 return -1;
1318 }
1319 curproxy->hh_match_domain = 1;
1320 }
1321
Emeric Brun736aa232009-06-30 17:56:00 +02001322 }
1323 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1324 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1325 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1326
1327 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1328 const char *beg, *end;
1329
1330 beg = args[0] + 11;
1331 end = strchr(beg, ')');
1332
1333 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001334 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001335 return -1;
1336 }
1337
1338 free(curproxy->hh_name);
1339 curproxy->hh_name = my_strndup(beg, end - beg);
1340 curproxy->hh_len = end - beg;
1341 }
1342 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1343 free(curproxy->hh_name);
1344 curproxy->hh_name = strdup("mstshash");
1345 curproxy->hh_len = strlen(curproxy->hh_name);
1346 }
1347 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001348 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001349 return -1;
1350 }
Willy Tarreau01732802007-11-01 22:48:15 +01001351 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001352 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001353 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 +01001354 return -1;
1355 }
1356 return 0;
1357}
1358
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001359
1360/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001361/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001362/************************************************************************/
1363
Willy Tarreau34db1082012-04-19 17:16:54 +02001364/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001365 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001366 * undefined behaviour.
1367 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001368static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001369smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001370 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001371{
Willy Tarreau37406352012-04-23 16:16:37 +02001372 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001373 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001374 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001375
1376 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001377 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001378 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001379 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001380 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001381 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001382
1383 return 1;
1384}
1385
Willy Tarreau37406352012-04-23 16:16:37 +02001386/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001387 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001388 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1389 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001390 */
1391static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001392smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001393 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001394{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001395 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001396
Willy Tarreau37406352012-04-23 16:16:37 +02001397 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001398 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001399 if (!(srv->state & SRV_MAINTAIN) &&
1400 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001401 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001402 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001403 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001404 return 1;
1405}
1406
Willy Tarreau34db1082012-04-19 17:16:54 +02001407/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001408 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001409 * undefined behaviour.
1410 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001411static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001412smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001413 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001414{
1415 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001416
Willy Tarreau37406352012-04-23 16:16:37 +02001417 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001418 smp->type = SMP_T_UINT;
1419 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001420
Willy Tarreau24e32d82012-04-23 23:55:44 +02001421 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001422 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001423 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001424
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001425 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001426 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001427 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001428 return 1;
1429 }
1430
Willy Tarreauf853c462012-04-23 18:53:56 +02001431 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001432 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001433 }
1434
1435 return 1;
1436}
1437
Willy Tarreaua5e37562011-12-16 17:06:15 +01001438/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001439static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001440smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001441 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001442{
Willy Tarreauf853c462012-04-23 18:53:56 +02001443 smp->flags = SMP_F_VOL_TXN;
1444 smp->type = SMP_T_UINT;
1445 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001446 return 1;
1447}
1448
Willy Tarreaua5e37562011-12-16 17:06:15 +01001449/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001450static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001451smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001452 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001453{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001454 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001455 return 0;
1456
Willy Tarreauf853c462012-04-23 18:53:56 +02001457 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001458 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001459
1460 return 1;
1461}
1462
Willy Tarreau34db1082012-04-19 17:16:54 +02001463/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001464 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001465 * undefined behaviour.
1466 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001467static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001468smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001469 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001470{
Willy Tarreau37406352012-04-23 16:16:37 +02001471 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001472 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001473 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001474 return 1;
1475}
1476
Willy Tarreau34db1082012-04-19 17:16:54 +02001477/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001478 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001479 * undefined behaviour.
1480 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001481static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001482smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001483 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001484{
Willy Tarreau37406352012-04-23 16:16:37 +02001485 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001486 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001487 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001488 return 1;
1489}
1490
Willy Tarreau34db1082012-04-19 17:16:54 +02001491/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001492 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001493 * undefined behaviour.
1494 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001495static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001496smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001497 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001498{
Willy Tarreau37406352012-04-23 16:16:37 +02001499 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001500 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001501 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001502 return 1;
1503}
1504
Willy Tarreaua5e37562011-12-16 17:06:15 +01001505/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001506 * by the number of running servers and rounded up. If there is no running
1507 * server, we return twice the total, just as if we had half a running server.
1508 * This is more or less correct anyway, since we expect the last server to come
1509 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001510 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001511 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001512 */
1513static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001514smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001515 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001516{
1517 int nbsrv;
1518
Willy Tarreau37406352012-04-23 16:16:37 +02001519 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001520 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001521 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001522
1523 if (px->srv_act)
1524 nbsrv = px->srv_act;
1525 else if (px->lbprm.fbck)
1526 nbsrv = 1;
1527 else
1528 nbsrv = px->srv_bck;
1529
1530 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001531 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001532 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001533 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001534
1535 return 1;
1536}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001537
Willy Tarreau34db1082012-04-19 17:16:54 +02001538/* set temp integer to the number of concurrent connections on the server in the backend.
1539 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1540 * undefined behaviour.
1541 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001542static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001543smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001544 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001545{
Willy Tarreauf853c462012-04-23 18:53:56 +02001546 smp->flags = SMP_F_VOL_TEST;
1547 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001548 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001549 return 1;
1550}
1551
Tait Clarridge7896d522012-12-05 21:39:31 -05001552/* set temp integer to the number of enabled servers on the proxy.
1553 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1554 * undefined behaviour.
1555 */
1556static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001557smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001558 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001559{
1560 smp->flags = SMP_F_VOL_TEST;
1561 smp->type = SMP_T_UINT;
1562 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1563 return 1;
1564}
1565
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001566
1567/* Note: must not be declared <const> as its list will be overwritten.
1568 * Please take care of keeping this list alphabetically sorted.
1569 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001570static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001571 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1572 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1573 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1574 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1575 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1576 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1577 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1578 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1579 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1580 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1581 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1582 { /* END */ },
1583}};
1584
1585
Willy Tarreau61612d42012-04-19 18:42:05 +02001586/* Note: must not be declared <const> as its list will be overwritten.
1587 * Please take care of keeping this list alphabetically sorted.
1588 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001589static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001590 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001591}};
1592
1593
1594__attribute__((constructor))
1595static void __backend_init(void)
1596{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001597 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001598 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 */