blob: e561919b016f3e5d1b1cb6ff5d4abf65d7c02559 [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>
Bhaskar98634f02013-10-29 23:30:51 -040026#include <common/hash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010032#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020033#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020037#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010038#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020039#include <proto/lb_fwlc.h>
40#include <proto/lb_fwrr.h>
41#include <proto/lb_map.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010042#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010043#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020046#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010048#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020050#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020051#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010052#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/task.h>
54
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050055int be_lastsession(const struct proxy *be)
56{
57 if (be->be_counters.last_sess)
58 return now.tv_sec - be->be_counters.last_sess;
59
60 return -1;
61}
62
Bhaskar98634f02013-10-29 23:30:51 -040063/* helper function to invoke the correct hash method */
64static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len)
65{
66 unsigned long hash;
67
68 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
69 case BE_LB_HFCN_DJB2:
70 hash = hash_djb2(key, len);
71 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010072 case BE_LB_HFCN_WT6:
73 hash = hash_wt6(key, len);
74 break;
Bhaskar98634f02013-10-29 23:30:51 -040075 case BE_LB_HFCN_SDBM:
76 /* this is the default hash function */
77 default:
78 hash = hash_sdbm(key, len);
79 break;
80 }
81
82 return hash;
83}
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085/*
86 * This function recounts the number of usable active and backup servers for
87 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010088 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010089 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010090 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020092void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020093{
94 struct server *srv;
95
Willy Tarreau20697042007-11-15 23:26:18 +010096 px->srv_act = px->srv_bck = 0;
97 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010098 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 if (!srv_is_usable(srv->state, srv->eweight))
101 continue;
102
103 if (srv->state & SRV_BACKUP) {
104 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100105 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100106 px->lbprm.fbck = srv;
107 px->srv_bck++;
108 px->lbprm.tot_wbck += srv->eweight;
109 } else {
110 px->srv_act++;
111 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 }
113 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100114}
Willy Tarreau20697042007-11-15 23:26:18 +0100115
Willy Tarreaub625a082007-11-26 01:15:43 +0100116/* This function simply updates the backend's tot_weight and tot_used values
117 * after servers weights have been updated. It is designed to be used after
118 * recount_servers() or equivalent.
119 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200120void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100121{
Willy Tarreau20697042007-11-15 23:26:18 +0100122 if (px->srv_act) {
123 px->lbprm.tot_weight = px->lbprm.tot_wact;
124 px->lbprm.tot_used = px->srv_act;
125 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100126 else if (px->lbprm.fbck) {
127 /* use only the first backup server */
128 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
129 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100130 }
131 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100132 px->lbprm.tot_weight = px->lbprm.tot_wbck;
133 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100134 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100135}
136
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200137/*
138 * This function tries to find a running server for the proxy <px> following
139 * the source hash method. 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 */
144struct server *get_server_sh(struct proxy *px, const char *addr, int len)
145{
146 unsigned int h, l;
147
148 if (px->lbprm.tot_weight == 0)
149 return NULL;
150
151 l = h = 0;
152
153 /* note: we won't hash if there's only one server left */
154 if (px->lbprm.tot_used == 1)
155 goto hash_done;
156
157 while ((l + sizeof (int)) <= len) {
158 h ^= ntohl(*(unsigned int *)(&addr[l]));
159 l += sizeof (int);
160 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500161 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100162 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200163 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200164 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
165 return chash_get_server_hash(px, h);
166 else
167 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200168}
169
170/*
171 * This function tries to find a running server for the proxy <px> following
172 * the URI hash method. In order to optimize cache hits, the hash computation
173 * ends at the question mark. Depending on the number of active/backup servers,
174 * it will either look for active servers, or for backup servers.
175 * If any server is found, it will be returned. If no valid server is found,
176 * NULL is returned.
177 *
178 * This code was contributed by Guillaume Dallaire, who also selected this hash
179 * algorithm out of a tens because it gave him the best results.
180 *
181 */
182struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
183{
184 unsigned long hash = 0;
185 int c;
186 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400187 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200188
189 if (px->lbprm.tot_weight == 0)
190 return NULL;
191
192 /* note: we won't hash if there's only one server left */
193 if (px->lbprm.tot_used == 1)
194 goto hash_done;
195
196 if (px->uri_len_limit)
197 uri_len = MIN(uri_len, px->uri_len_limit);
198
Bhaskar98634f02013-10-29 23:30:51 -0400199 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200200 while (uri_len--) {
Bhaskar98634f02013-10-29 23:30:51 -0400201 c = *end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200202 if (c == '/') {
203 slashes++;
204 if (slashes == px->uri_dirs_depth1) /* depth+1 */
205 break;
206 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100207 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200208 break;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200209 }
Bhaskar98634f02013-10-29 23:30:51 -0400210
211 hash = gen_hash(px, start, (end - start));
212
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500213 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100214 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200215 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200216 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
217 return chash_get_server_hash(px, hash);
218 else
219 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200220}
221
Willy Tarreau01732802007-11-01 22:48:15 +0100222/*
223 * This function tries to find a running server for the proxy <px> following
224 * the URL parameter hash method. It looks for a specific parameter in the
225 * URL and hashes it to compute the server ID. This is useful to optimize
226 * performance by avoiding bounces between servers in contexts where sessions
227 * are shared but cookies are not usable. If the parameter is not found, NULL
228 * is returned. If any server is found, it will be returned. If no valid server
229 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100230 */
231struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
232{
233 unsigned long hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400234 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200235 const char *p;
236 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100237 int plen;
238
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200239 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100240 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100241 return NULL;
242
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200243 if ((p = memchr(uri, '?', uri_len)) == NULL)
244 return NULL;
245
Willy Tarreau01732802007-11-01 22:48:15 +0100246 p++;
247
248 uri_len -= (p - uri);
249 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100251
252 while (uri_len > plen) {
253 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 if (params[plen] == '=') {
255 if (memcmp(params, px->url_param_name, plen) == 0) {
256 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100257 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100259 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400261 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 uri_len -= plen + 1;
263
Bhaskar98634f02013-10-29 23:30:51 -0400264 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100265 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400266 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100267 }
Bhaskar98634f02013-10-29 23:30:51 -0400268 hash = gen_hash(px, start, (end - start));
269
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500270 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100271 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400272
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200273 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
274 return chash_get_server_hash(px, hash);
275 else
276 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100277 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200278 }
279 /* skip to next parameter */
280 p = memchr(params, '&', uri_len);
281 if (!p)
282 return NULL;
283 p++;
284 uri_len -= (p - params);
285 params = p;
286 }
287 return NULL;
288}
289
290/*
291 * this does the same as the previous server_ph, but check the body contents
292 */
293struct server *get_server_ph_post(struct session *s)
294{
295 unsigned long hash = 0;
296 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200297 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200298 struct http_msg *msg = &txn->req;
299 struct proxy *px = s->be;
300 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100301 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200302 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100303 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400304 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305
Willy Tarreau9b28e032012-10-12 23:49:43 +0200306 if (len > buffer_len(req->buf) - msg->sov)
307 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308
Willy Tarreau157dd632009-12-06 19:18:09 +0100309 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 return NULL;
311
Willy Tarreau157dd632009-12-06 19:18:09 +0100312 if (px->lbprm.tot_weight == 0)
313 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200314
315 while (len > plen) {
316 /* Look for the parameter name followed by an equal symbol */
317 if (params[plen] == '=') {
318 if (memcmp(params, px->url_param_name, plen) == 0) {
319 /* OK, we have the parameter here at <params>, and
320 * the value after the equal sign, at <p>
321 * skip the equal symbol
322 */
323 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400324 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200325 len -= plen + 1;
326
Bhaskar98634f02013-10-29 23:30:51 -0400327 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100329 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400330 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100331 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200332 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
333 break;
334 return NULL; /* oh, no; this is not uri-encoded.
335 * This body does not contain parameters.
336 */
337 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200338 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400339 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200340 /* should we break if vlen exceeds limit? */
341 }
Bhaskar98634f02013-10-29 23:30:51 -0400342 hash = gen_hash(px, start, (end - start));
343
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500344 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100345 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400346
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200347 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
348 return chash_get_server_hash(px, hash);
349 else
350 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200351 }
352 }
Willy Tarreau01732802007-11-01 22:48:15 +0100353 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200354 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100355 if (!p)
356 return NULL;
357 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200358 len -= (p - params);
359 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100360 }
361 return NULL;
362}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365/*
Benoitaffb4812009-03-25 13:02:10 +0100366 * This function tries to find a running server for the proxy <px> following
367 * the Header parameter hash method. It looks for a specific parameter in the
368 * URL and hashes it to compute the server ID. This is useful to optimize
369 * performance by avoiding bounces between servers in contexts where sessions
370 * are shared but cookies are not usable. If the parameter is not found, NULL
371 * is returned. If any server is found, it will be returned. If no valid server
372 * is found, NULL is returned.
373 */
374struct server *get_server_hh(struct session *s)
375{
376 unsigned long hash = 0;
377 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100378 struct proxy *px = s->be;
379 unsigned int plen = px->hh_len;
380 unsigned long len;
381 struct hdr_ctx ctx;
382 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400383 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100384
385 /* tot_weight appears to mean srv_count */
386 if (px->lbprm.tot_weight == 0)
387 return NULL;
388
Benoitaffb4812009-03-25 13:02:10 +0100389 ctx.idx = 0;
390
391 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200392 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 +0100393
394 /* if the header is not found or empty, let's fallback to round robin */
395 if (!ctx.idx || !ctx.vlen)
396 return NULL;
397
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200398 /* note: we won't hash if there's only one server left */
399 if (px->lbprm.tot_used == 1)
400 goto hash_done;
401
Benoitaffb4812009-03-25 13:02:10 +0100402 /* Found a the hh_name in the headers.
403 * we will compute the hash based on this value ctx.val.
404 */
405 len = ctx.vlen;
406 p = (char *)ctx.line + ctx.val;
407 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400408 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100409 } else {
410 int dohash = 0;
411 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400412 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100413 /* special computation, use only main domain name, not tld/host
414 * going back from the end of string, start hashing at first
415 * dot stop at next.
416 * This is designed to work with the 'Host' header, and requires
417 * a special option to activate this.
418 */
419 while (len) {
420 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400421 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100422 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400423 start = end = p - 1;
424 }
Benoitaffb4812009-03-25 13:02:10 +0100425 else
426 break;
427 } else {
428 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400429 start--;
Benoitaffb4812009-03-25 13:02:10 +0100430 }
431 len--;
432 p--;
433 }
Bhaskar98634f02013-10-29 23:30:51 -0400434 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100435 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500436 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100437 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200438 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200439 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
440 return chash_get_server_hash(px, hash);
441 else
442 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100443}
444
Willy Tarreau34db1082012-04-19 17:16:54 +0200445/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200446struct server *get_server_rch(struct session *s)
447{
448 unsigned long hash = 0;
449 struct proxy *px = s->be;
450 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200451 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200452 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200453 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200454
455 /* tot_weight appears to mean srv_count */
456 if (px->lbprm.tot_weight == 0)
457 return NULL;
458
Willy Tarreau37406352012-04-23 16:16:37 +0200459 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200460
Willy Tarreau9b28e032012-10-12 23:49:43 +0200461 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200462
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200463 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200464 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100465
Willy Tarreau9b28e032012-10-12 23:49:43 +0200466 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200467
Willy Tarreau37406352012-04-23 16:16:37 +0200468 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200469 return NULL;
470
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200471 /* note: we won't hash if there's only one server left */
472 if (px->lbprm.tot_used == 1)
473 goto hash_done;
474
Emeric Brun736aa232009-06-30 17:56:00 +0200475 /* Found a the hh_name in the headers.
476 * we will compute the hash based on this value ctx.val.
477 */
Bhaskar98634f02013-10-29 23:30:51 -0400478 hash = gen_hash(px, smp.data.str.str, len);
479
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500480 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100481 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200482 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200483 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
484 return chash_get_server_hash(px, hash);
485 else
486 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200487}
Benoitaffb4812009-03-25 13:02:10 +0100488
489/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200490 * This function applies the load-balancing algorithm to the session, as
491 * defined by the backend it is assigned to. The session is then marked as
492 * 'assigned'.
493 *
494 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
495 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100496 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 * The function tries to keep the original connection slot if it reconnects to
498 * the same server, otherwise it releases it and tries to offer it.
499 *
500 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501 *
502 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100503 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
505 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506 * SRV_STATUS_INTERNAL for other unrecoverable errors.
507 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200508 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100509 * it does not need to be called anymore. This means that target_srv(&s->target)
510 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 *
512 */
513
514int assign_server(struct session *s)
515{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200516 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100518 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200519 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100520
Simon Horman8effd3d2011-08-13 08:03:52 +0900521 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau7c669d72008-06-20 15:04:11 +0200523 err = SRV_STATUS_INTERNAL;
524 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
525 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100526
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100527 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200528 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 /* We have to release any connection slot before applying any LB algo,
531 * otherwise we may erroneously end up with no available slot.
532 */
533 if (conn_slot)
534 sess_change_server(s, NULL);
535
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100536 /* We will now try to find the good server and store it into <objt_server(s->target)>.
537 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200538 * as well as if no server is available (check error code).
539 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100540
Willy Tarreau827aee92011-03-10 16:55:02 +0100541 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100542 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100543 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100544
Willy Tarreau068621e2013-12-23 15:11:25 +0100545 if (conn &&
546 ((s->be->options & PR_O_PREF_LAST) || (s->txn.flags & TX_PREFER_LAST)) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100547 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
548 srv_is_usable(__objt_server(conn->target)->state, __objt_server(conn->target)->eweight)) {
549 /* This session was relying on a server in a previous request
550 * and the proxy has "option prefer-current-server" set, so
551 * let's try to reuse the same server.
552 */
553 srv = __objt_server(conn->target);
554 s->target = &srv->obj_type;
555 }
556 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200557 /* we must check if we have at least one server available */
558 if (!s->be->lbprm.tot_weight) {
559 err = SRV_STATUS_NOSRV;
560 goto out;
561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200563 /* First check whether we need to fetch some data or simply call
564 * the LB lookup function. Only the hashing functions will need
565 * some input data in fact, and will support multiple algorithms.
566 */
567 switch (s->be->lbprm.algo & BE_LB_LKUP) {
568 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100569 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200570 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571
Willy Tarreauf09c6602012-02-13 17:12:08 +0100572 case BE_LB_LKUP_FSTREE:
573 srv = fas_get_next_server(s->be, prev_srv);
574 break;
575
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200576 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100577 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200578 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200580 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200581 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200582 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200583 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100584 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200585 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100586 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200587 break;
588 }
589 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200590 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200591 err = SRV_STATUS_INTERNAL;
592 goto out;
593 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594
595 switch (s->be->lbprm.algo & BE_LB_PARM) {
596 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200597 conn = objt_conn(s->req->prod->end);
598 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200599 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200600 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200601 4);
602 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200603 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200604 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200605 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200606 16);
607 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200608 else {
609 /* unknown IP family */
610 err = SRV_STATUS_INTERNAL;
611 goto out;
612 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613 break;
614
615 case BE_LB_HASH_URI:
616 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100617 if (s->txn.req.msg_state < HTTP_MSG_BODY)
618 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100619 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200620 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100621 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200623
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200624 case BE_LB_HASH_PRM:
625 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100626 if (s->txn.req.msg_state < HTTP_MSG_BODY)
627 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100628
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200630 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100632
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 if (!srv && s->txn.meth == HTTP_METH_POST)
634 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200635 break;
Benoitaffb4812009-03-25 13:02:10 +0100636
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200637 case BE_LB_HASH_HDR:
638 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100639 if (s->txn.req.msg_state < HTTP_MSG_BODY)
640 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200642 break;
643
644 case BE_LB_HASH_RDP:
645 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100646 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 break;
648
649 default:
650 /* unknown balancing algorithm */
651 err = SRV_STATUS_INTERNAL;
652 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100653 }
Emeric Brun736aa232009-06-30 17:56:00 +0200654
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200655 /* If the hashing parameter was not found, let's fall
656 * back to round robin on the map.
657 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100658 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200659 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100660 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200661 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100662 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200663 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200664
665 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200666 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667
Willy Tarreau7c669d72008-06-20 15:04:11 +0200668 default:
669 /* unknown balancing algorithm */
670 err = SRV_STATUS_INTERNAL;
671 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200673
Willy Tarreau827aee92011-03-10 16:55:02 +0100674 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200675 err = SRV_STATUS_FULL;
676 goto out;
677 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100678 else if (srv != prev_srv) {
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500679 be_set_sess_last(s->be);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100680 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100681 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100682 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100683 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200684 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200685 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100686 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100688 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200689 (conn = objt_conn(s->req->cons->end)) &&
690 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100691 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100692 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100693 }
694 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200695 err = SRV_STATUS_NOSRV;
696 goto out;
697 }
698
699 s->flags |= SN_ASSIGNED;
700 err = SRV_STATUS_OK;
701 out:
702
703 /* Either we take back our connection slot, or we offer it to someone
704 * else if we don't need it anymore.
705 */
706 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100707 if (conn_slot == srv) {
708 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200709 } else {
710 if (may_dequeue_tasks(conn_slot, s->be))
711 process_srv_queue(conn_slot);
712 }
713 }
714
715 out_err:
716 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717}
718
719
720/*
721 * This function assigns a server address to a session, and sets SN_ADDR_SET.
722 * The address is taken from the currently assigned server, or from the
723 * dispatch or transparent address.
724 *
725 * It may return :
726 * SRV_STATUS_OK if everything is OK.
727 * SRV_STATUS_INTERNAL for other unrecoverable errors.
728 *
729 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
730 * not cleared, so it's to the caller to clear it if required.
731 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200732 * The caller is responsible for having already assigned a connection
733 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200734 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 */
736int assign_server_address(struct session *s)
737{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200738 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200739 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200740
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741#ifdef DEBUG_FULL
742 fprintf(stderr,"assign_server_address : s=%p\n",s);
743#endif
744
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200745 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 /* A server is necessarily known for this session */
747 if (!(s->flags & SN_ASSIGNED))
748 return SRV_STATUS_INTERNAL;
749
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200750 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200752 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200753 /* if the server has no address, we use the same address
754 * the client asked, which is handy for remapping ports
755 * locally on multiple addresses at once.
756 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200757 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200758
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200759 if (cli_conn->addr.to.ss_family == AF_INET) {
760 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
761 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
762 ((struct sockaddr_in6 *)&srv_conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200763 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200764 }
765
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 /* if this server remaps proxied ports, we'll use
767 * the port the client connected to with an offset. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 if ((objt_server(s->target)->state & SRV_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100769 int base_port;
770
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200771 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100772
773 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100775
776 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200777 base_port += get_host_port(&srv_conn->addr.to);
778 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 }
780 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200781 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200783 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200785 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200787 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200789 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
790 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100792 else if (s->be->options & PR_O_HTTP_PROXY) {
793 /* If HTTP PROXY option is set, then server is already assigned
794 * during incoming client request parsing. */
795 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100796 else {
797 /* no server and no LB algorithm ! */
798 return SRV_STATUS_INTERNAL;
799 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800
801 s->flags |= SN_ADDR_SET;
802 return SRV_STATUS_OK;
803}
804
805
806/* This function assigns a server to session <s> if required, and can add the
807 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200808 * If ->srv_conn is set, the session is first released from the server.
809 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
810 * be called before any connection and after any retry or redispatch occurs.
811 *
812 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 *
814 * Returns :
815 *
816 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100817 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200818 * SRV_STATUS_QUEUED if the connection has been queued.
819 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200821 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 * SRV_STATUS_INTERNAL for other unrecoverable errors.
823 *
824 */
825int assign_server_and_queue(struct session *s)
826{
827 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100828 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 int err;
830
831 if (s->pend_pos)
832 return SRV_STATUS_INTERNAL;
833
Willy Tarreau7c669d72008-06-20 15:04:11 +0200834 err = SRV_STATUS_OK;
835 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100836 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100837
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100839 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200840 /* This session was previously assigned to a server. We have to
841 * update the session's and the server's stats :
842 * - if the server changed :
843 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
844 * - set SN_REDISP if it was successfully redispatched
845 * - increment srv->redispatches and be->redispatches
846 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100847 */
848
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100849 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200850 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
851 s->txn.flags &= ~TX_CK_MASK;
852 s->txn.flags |= TX_CK_DOWN;
853 }
854 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100855 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100856 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100858 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100859 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100860 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100861 }
862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 switch (err) {
865 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100867 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100868 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869 return SRV_STATUS_OK; /* dispatch or proxy mode */
870
871 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100872 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200873 return SRV_STATUS_OK;
874
875 /* OK, this session already has an assigned server, but no
876 * connection slot yet. Either it is a redispatch, or it was
877 * assigned from persistence information (direct mode).
878 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100879 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 /* server scheduled for redirection, and already assigned. We
881 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100882 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100883 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100884 return SRV_STATUS_OK;
885 }
886
Willy Tarreau7c669d72008-06-20 15:04:11 +0200887 /* We might have to queue this session if the assigned server is full.
888 * We know we have to queue it into the server's queue, so if a maxqueue
889 * is set on the server, we must also check that the server's queue is
890 * not full, in which case we have to return FULL.
891 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100892 if (srv->maxconn &&
893 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200894
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200896 return SRV_STATUS_FULL;
897
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898 p = pendconn_add(s);
899 if (p)
900 return SRV_STATUS_QUEUED;
901 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200902 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200904
905 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100906 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907 return SRV_STATUS_OK;
908
909 case SRV_STATUS_FULL:
910 /* queue this session into the proxy's queue */
911 p = pendconn_add(s);
912 if (p)
913 return SRV_STATUS_QUEUED;
914 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200915 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916
917 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200918 return err;
919
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920 case SRV_STATUS_INTERNAL:
921 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200922
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923 default:
924 return SRV_STATUS_INTERNAL;
925 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100926}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927
Willy Tarreaub1d67742010-03-29 19:36:59 +0200928/* If an explicit source binding is specified on the server and/or backend, and
929 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200930 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200931 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200932 */
933static void assign_tproxy_address(struct session *s)
934{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200935#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100936 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100937 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200938 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200939 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100940
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100941 if (srv && srv->conn_src.opts & CO_SRC_BIND)
942 src = &srv->conn_src;
943 else if (s->be->conn_src.opts & CO_SRC_BIND)
944 src = &s->be->conn_src;
945 else
946 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100947
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100948 switch (src->opts & CO_SRC_TPROXY_MASK) {
949 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200950 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100951 break;
952 case CO_SRC_TPROXY_CLI:
953 case CO_SRC_TPROXY_CIP:
954 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200955 cli_conn = objt_conn(s->req->prod->end);
956 if (cli_conn)
957 srv_conn->addr.from = cli_conn->addr.from;
958 else
959 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100960 break;
961 case CO_SRC_TPROXY_DYN:
962 if (src->bind_hdr_occ) {
963 char *vptr;
964 int vlen;
965 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100966
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100967 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200968 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
969 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
970 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100971
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100972 b_rew(s->req->buf, rewind = s->req->buf->o);
973 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
974 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200975 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100976 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200977 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100978 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200979 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100980 break;
981 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200982 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200983 }
984#endif
985}
986
987
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988/*
989 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200990 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100991 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 * It can return one of :
993 * - SN_ERR_NONE if everything's OK
994 * - SN_ERR_SRVTO if there are no more servers
995 * - SN_ERR_SRVCL if the connection was refused by the server
996 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
997 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
998 * - SN_ERR_INTERNAL for any other purely internal errors
999 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001000 * The server-facing stream interface is expected to hold a pre-allocated connection
1001 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 */
1003int connect_server(struct session *s)
1004{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001006 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001007 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001008 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001009 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010
Willy Tarreau34601a82013-12-15 16:33:46 +01001011 srv_conn = objt_conn(s->req->cons->end);
1012 if (srv_conn)
1013 reuse = s->target == srv_conn->target;
1014
1015 if (reuse) {
1016 /* Disable connection reuse if a dynamic source is used.
1017 * As long as we don't share connections between servers,
1018 * we don't need to disable connection reuse on no-idempotent
1019 * requests nor when PROXY protocol is used.
1020 */
1021 srv = objt_server(s->target);
1022 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1023 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1024 reuse = 0;
1025 }
1026 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1027 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1028 reuse = 0;
1029 }
1030 }
1031
1032 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001033 if (!srv_conn)
1034 return SN_ERR_RESOURCE;
1035
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 if (!(s->flags & SN_ADDR_SET)) {
1037 err = assign_server_address(s);
1038 if (err != SRV_STATUS_OK)
1039 return SN_ERR_INTERNAL;
1040 }
1041
Willy Tarreauff605db2013-12-20 11:09:51 +01001042 if (!conn_xprt_ready(srv_conn)) {
1043 /* the target was only on the session, assign it to the SI now */
1044 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001045
Willy Tarreauff605db2013-12-20 11:09:51 +01001046 /* set the correct protocol on the output stream interface */
1047 if (objt_server(s->target)) {
1048 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1049 }
1050 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1051 /* proxies exclusively run on raw_sock right now */
1052 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1053 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1054 return SN_ERR_INTERNAL;
1055 }
1056 else
1057 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001058
Willy Tarreauff605db2013-12-20 11:09:51 +01001059 /* process the case where the server requires the PROXY protocol to be sent */
1060 srv_conn->send_proxy_ofs = 0;
1061 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
1062 srv_conn->send_proxy_ofs = 1; /* must compute size */
1063 cli_conn = objt_conn(s->req->prod->end);
1064 if (cli_conn)
1065 conn_get_to_addr(cli_conn);
1066 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001067
Willy Tarreauff605db2013-12-20 11:09:51 +01001068 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001069
Willy Tarreauff605db2013-12-20 11:09:51 +01001070 assign_tproxy_address(s);
1071 }
1072 else {
1073 /* the connection is being reused, just re-attach it */
1074 si_attach_conn(s->req->cons, srv_conn);
1075 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001076
William Lallemandb7ff6a32012-03-02 14:35:21 +01001077 /* flag for logging source ip/port */
1078 if (s->fe->options2 & PR_O2_SRC_ADDR)
1079 s->req->cons->flags |= SI_FL_SRC_ADDR;
1080
Willy Tarreau14f8e862012-08-30 22:23:13 +02001081 /* disable lingering */
1082 if (s->be->options & PR_O_TCP_NOLING)
1083 s->req->cons->flags |= SI_FL_NOLINGER;
1084
Willy Tarreau73b013b2012-05-21 16:31:45 +02001085 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086
Willy Tarreau9650f372009-08-16 14:02:45 +02001087 if (err != SN_ERR_NONE)
1088 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001089
Willy Tarreau14f8e862012-08-30 22:23:13 +02001090 /* set connect timeout */
1091 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1092
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001093 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001094 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001095 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001096 srv->cur_sess++;
1097 if (srv->cur_sess > srv->counters.cur_sess_max)
1098 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001099 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001100 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101 }
1102
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103 return SN_ERR_NONE; /* connection is OK */
1104}
1105
1106
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107/* This function performs the "redispatch" part of a connection attempt. It
1108 * will assign a server if required, queue the connection if required, and
1109 * handle errors that might arise at this level. It can change the server
1110 * state. It will return 1 if it encounters an error, switches the server
1111 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1112 * that the connection is ready to use.
1113 */
1114
1115int srv_redispatch_connect(struct session *t)
1116{
Willy Tarreau827aee92011-03-10 16:55:02 +01001117 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118 int conn_err;
1119
1120 /* We know that we don't have any connection pending, so we will
1121 * try to get a new one, and wait in this state if it's queued
1122 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001123 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001124 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001125 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001126
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127 switch (conn_err) {
1128 case SRV_STATUS_OK:
1129 break;
1130
Willy Tarreau7c669d72008-06-20 15:04:11 +02001131 case SRV_STATUS_FULL:
1132 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1133 * and we can redispatch to another server, or it is not and we return
1134 * 503. This only makes sense in DIRECT mode however, because normal LB
1135 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001136 * would bring us on the same server again. Note that t->target is set
1137 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001138 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001139 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1140 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001141 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001142 goto redispatch;
1143 }
1144
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001145 if (!t->req->cons->err_type) {
1146 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001147 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001148
Willy Tarreau827aee92011-03-10 16:55:02 +01001149 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001150 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001151 return 1;
1152
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001154 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001155 if (!t->req->cons->err_type) {
1156 t->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001157 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001158
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001159 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 return 1;
1161
1162 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001163 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001164 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 /* do nothing else and do not wake any other session up */
1166 return 1;
1167
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168 case SRV_STATUS_INTERNAL:
1169 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001170 if (!t->req->cons->err_type) {
1171 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001172 }
1173
Willy Tarreau827aee92011-03-10 16:55:02 +01001174 if (srv)
1175 srv_inc_sess_ctr(srv);
1176 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001177 srv_set_sess_last(srv);
1178 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001179 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001180 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181
1182 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001183 if (may_dequeue_tasks(srv, t->be))
1184 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185 return 1;
1186 }
1187 /* if we get here, it's because we got SRV_STATUS_OK, which also
1188 * means that the connection has not been queued.
1189 */
1190 return 0;
1191}
1192
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001193/* Apply RDP cookie persistence to the current session. For this, the function
1194 * tries to extract an RDP cookie from the request buffer, and look for the
1195 * matching server in the list. If the server is found, it is assigned to the
1196 * session. This always returns 1, and the analyser removes itself from the
1197 * list. Nothing is performed if a server was already assigned.
1198 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001199int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001200{
1201 struct proxy *px = s->be;
1202 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001203 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001204 struct server *srv = px->srv;
1205 struct sockaddr_in addr;
1206 char *p;
1207
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001208 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 +02001209 now_ms, __FUNCTION__,
1210 s,
1211 req,
1212 req->rex, req->wex,
1213 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001214 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001215 req->analysers);
1216
1217 if (s->flags & SN_ASSIGNED)
1218 goto no_cookie;
1219
Willy Tarreau37406352012-04-23 16:16:37 +02001220 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001221
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001222 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001223 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001224 goto no_cookie;
1225
1226 memset(&addr, 0, sizeof(addr));
1227 addr.sin_family = AF_INET;
1228
Willy Tarreau664092c2011-12-16 19:11:42 +01001229 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001230 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001231 if (*p != '.')
1232 goto no_cookie;
1233 p++;
1234 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1235 if (*p != '.')
1236 goto no_cookie;
1237
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001238 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001239 while (srv) {
1240 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1241 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1242 /* we found the server and it is usable */
1243 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001244 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001245 break;
1246 }
1247 }
1248 srv = srv->next;
1249 }
1250
1251no_cookie:
1252 req->analysers &= ~an_bit;
1253 req->analyse_exp = TICK_ETERNITY;
1254 return 1;
1255}
1256
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001257int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001258 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001259 return px->down_time;
1260
1261 return now.tv_sec - px->last_change + px->down_time;
1262}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001264/*
1265 * This function returns a string containing the balancing
1266 * mode of the proxy in a format suitable for stats.
1267 */
1268
1269const char *backend_lb_algo_str(int algo) {
1270
1271 if (algo == BE_LB_ALGO_RR)
1272 return "roundrobin";
1273 else if (algo == BE_LB_ALGO_SRR)
1274 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001275 else if (algo == BE_LB_ALGO_FAS)
1276 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001277 else if (algo == BE_LB_ALGO_LC)
1278 return "leastconn";
1279 else if (algo == BE_LB_ALGO_SH)
1280 return "source";
1281 else if (algo == BE_LB_ALGO_UH)
1282 return "uri";
1283 else if (algo == BE_LB_ALGO_PH)
1284 return "url_param";
1285 else if (algo == BE_LB_ALGO_HH)
1286 return "hdr";
1287 else if (algo == BE_LB_ALGO_RCH)
1288 return "rdp-cookie";
1289 else
1290 return NULL;
1291}
1292
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001293/* This function parses a "balance" statement in a backend section describing
1294 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001295 * returns -1, it will write an error message into the <err> buffer which will
1296 * automatically be allocated and must be passed as NULL. The trailing '\n'
1297 * will not be written. The function must be called with <args> pointing to the
1298 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001299 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001300int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001301{
1302 if (!*(args[0])) {
1303 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001304 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1305 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001306 return 0;
1307 }
1308
1309 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001310 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1311 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001312 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001313 else if (!strcmp(args[0], "static-rr")) {
1314 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1315 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1316 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001317 else if (!strcmp(args[0], "first")) {
1318 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1319 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1320 }
Willy Tarreau51406232008-03-10 22:04:20 +01001321 else if (!strcmp(args[0], "leastconn")) {
1322 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1323 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1324 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001325 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001326 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1327 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001328 }
1329 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001330 int arg = 1;
1331
Willy Tarreau31682232007-11-29 15:38:04 +01001332 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1333 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001334
Oskar Stolc8dc41842012-05-19 10:19:54 +01001335 curproxy->uri_whole = 0;
1336
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001337 while (*args[arg]) {
1338 if (!strcmp(args[arg], "len")) {
1339 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001340 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001341 return -1;
1342 }
1343 curproxy->uri_len_limit = atoi(args[arg+1]);
1344 arg += 2;
1345 }
1346 else if (!strcmp(args[arg], "depth")) {
1347 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001348 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001349 return -1;
1350 }
1351 /* hint: we store the position of the ending '/' (depth+1) so
1352 * that we avoid a comparison while computing the hash.
1353 */
1354 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1355 arg += 2;
1356 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001357 else if (!strcmp(args[arg], "whole")) {
1358 curproxy->uri_whole = 1;
1359 arg += 1;
1360 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001361 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001362 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001363 return -1;
1364 }
1365 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001366 }
Willy Tarreau01732802007-11-01 22:48:15 +01001367 else if (!strcmp(args[0], "url_param")) {
1368 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001369 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001370 return -1;
1371 }
Willy Tarreau31682232007-11-29 15:38:04 +01001372 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1373 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001374
1375 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001376 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001377 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001378 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001379 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001380 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001381 return -1;
1382 }
1383 if (*args[3]) {
1384 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1385 curproxy->url_param_post_limit = str2ui(args[3]);
1386 }
1387 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1388 if (!curproxy->url_param_post_limit)
1389 curproxy->url_param_post_limit = 48;
1390 else if ( curproxy->url_param_post_limit < 3 )
1391 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1392 }
Benoitaffb4812009-03-25 13:02:10 +01001393 }
1394 else if (!strncmp(args[0], "hdr(", 4)) {
1395 const char *beg, *end;
1396
1397 beg = args[0] + 4;
1398 end = strchr(beg, ')');
1399
1400 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001401 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001402 return -1;
1403 }
1404
1405 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1406 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1407
1408 free(curproxy->hh_name);
1409 curproxy->hh_len = end - beg;
1410 curproxy->hh_name = my_strndup(beg, end - beg);
1411 curproxy->hh_match_domain = 0;
1412
1413 if (*args[1]) {
1414 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001415 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001416 return -1;
1417 }
1418 curproxy->hh_match_domain = 1;
1419 }
Emeric Brun736aa232009-06-30 17:56:00 +02001420 }
1421 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1422 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1423 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1424
1425 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1426 const char *beg, *end;
1427
1428 beg = args[0] + 11;
1429 end = strchr(beg, ')');
1430
1431 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001432 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001433 return -1;
1434 }
1435
1436 free(curproxy->hh_name);
1437 curproxy->hh_name = my_strndup(beg, end - beg);
1438 curproxy->hh_len = end - beg;
1439 }
1440 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1441 free(curproxy->hh_name);
1442 curproxy->hh_name = strdup("mstshash");
1443 curproxy->hh_len = strlen(curproxy->hh_name);
1444 }
1445 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001446 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001447 return -1;
1448 }
Willy Tarreau01732802007-11-01 22:48:15 +01001449 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001450 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001451 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 +01001452 return -1;
1453 }
1454 return 0;
1455}
1456
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001457
1458/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001459/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001460/************************************************************************/
1461
Willy Tarreau34db1082012-04-19 17:16:54 +02001462/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001463 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001464 * undefined behaviour.
1465 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001466static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001467smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001468 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001469{
Willy Tarreau37406352012-04-23 16:16:37 +02001470 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001471 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001472 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001473
1474 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001475 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001476 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001477 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001478 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001479 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001480
1481 return 1;
1482}
1483
Willy Tarreau37406352012-04-23 16:16:37 +02001484/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001485 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001486 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1487 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001488 */
1489static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001490smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001491 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001492{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001493 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001494
Willy Tarreau37406352012-04-23 16:16:37 +02001495 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001496 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001497 if (!(srv->state & SRV_MAINTAIN) &&
Willy Tarreauff5ae352013-12-11 20:36:34 +01001498 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001499 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001500 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001501 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001502 return 1;
1503}
1504
Willy Tarreau34db1082012-04-19 17:16:54 +02001505/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001506 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001507 * undefined behaviour.
1508 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001509static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001510smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001511 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001512{
1513 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001514
Willy Tarreau37406352012-04-23 16:16:37 +02001515 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001516 smp->type = SMP_T_UINT;
1517 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001518
Willy Tarreau24e32d82012-04-23 23:55:44 +02001519 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001520 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001521 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001522
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001523 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001524 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001525 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001526 return 1;
1527 }
1528
Willy Tarreauf853c462012-04-23 18:53:56 +02001529 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001530 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001531 }
1532
1533 return 1;
1534}
1535
Willy Tarreaua5e37562011-12-16 17:06:15 +01001536/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001537static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001538smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001539 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001540{
Willy Tarreauf853c462012-04-23 18:53:56 +02001541 smp->flags = SMP_F_VOL_TXN;
1542 smp->type = SMP_T_UINT;
1543 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001544 return 1;
1545}
1546
Willy Tarreaua5e37562011-12-16 17:06:15 +01001547/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001548static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001549smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001550 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001551{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001552 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001553 return 0;
1554
Willy Tarreauf853c462012-04-23 18:53:56 +02001555 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001556 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001557
1558 return 1;
1559}
1560
Willy Tarreau34db1082012-04-19 17:16:54 +02001561/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001562 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001563 * undefined behaviour.
1564 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001565static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001566smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001567 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001568{
Willy Tarreau37406352012-04-23 16:16:37 +02001569 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001570 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001571 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001572 return 1;
1573}
1574
Willy Tarreau34db1082012-04-19 17:16:54 +02001575/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001576 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001577 * undefined behaviour.
1578 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001579static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001580smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001581 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001582{
Willy Tarreau37406352012-04-23 16:16:37 +02001583 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001584 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001585 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001586 return 1;
1587}
1588
Willy Tarreau34db1082012-04-19 17:16:54 +02001589/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001590 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001591 * undefined behaviour.
1592 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001593static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001594smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001595 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001596{
Willy Tarreau37406352012-04-23 16:16:37 +02001597 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001598 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001599 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001600 return 1;
1601}
1602
Willy Tarreaua5e37562011-12-16 17:06:15 +01001603/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001604 * by the number of running servers and rounded up. If there is no running
1605 * server, we return twice the total, just as if we had half a running server.
1606 * This is more or less correct anyway, since we expect the last server to come
1607 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001608 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001609 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001610 */
1611static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001612smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001613 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001614{
1615 int nbsrv;
1616
Willy Tarreau37406352012-04-23 16:16:37 +02001617 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001618 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001619 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001620
1621 if (px->srv_act)
1622 nbsrv = px->srv_act;
1623 else if (px->lbprm.fbck)
1624 nbsrv = 1;
1625 else
1626 nbsrv = px->srv_bck;
1627
1628 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001629 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001630 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001631 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001632
1633 return 1;
1634}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001635
Willy Tarreau34db1082012-04-19 17:16:54 +02001636/* set temp integer to the number of concurrent connections on the server in the backend.
1637 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1638 * undefined behaviour.
1639 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001640static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001641smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001642 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001643{
Willy Tarreauf853c462012-04-23 18:53:56 +02001644 smp->flags = SMP_F_VOL_TEST;
1645 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001646 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001647 return 1;
1648}
1649
Tait Clarridge7896d522012-12-05 21:39:31 -05001650/* set temp integer to the number of enabled servers on the proxy.
1651 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1652 * undefined behaviour.
1653 */
1654static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001655smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001656 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001657{
1658 smp->flags = SMP_F_VOL_TEST;
1659 smp->type = SMP_T_UINT;
1660 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1661 return 1;
1662}
1663
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001664
1665/* Note: must not be declared <const> as its list will be overwritten.
1666 * Please take care of keeping this list alphabetically sorted.
1667 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001668static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001669 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1670 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1671 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1672 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1673 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1674 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1675 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1676 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1677 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1678 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1679 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1680 { /* END */ },
1681}};
1682
1683
Willy Tarreau61612d42012-04-19 18:42:05 +02001684/* Note: must not be declared <const> as its list will be overwritten.
1685 * Please take care of keeping this list alphabetically sorted.
1686 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001687static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001688 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001689}};
1690
1691
1692__attribute__((constructor))
1693static void __backend_init(void)
1694{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001695 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001696 acl_register_keywords(&acl_kws);
1697}
1698
Willy Tarreaubaaee002006-06-26 02:48:02 +02001699/*
1700 * Local variables:
1701 * c-indent-level: 8
1702 * c-basic-offset: 8
1703 * End:
1704 */