blob: 32e19f91569435dd1ae7b3fa7d319170f3ecacc7 [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010029#include <common/namespace.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010033#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020034#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020037#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020038#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010039#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020040#include <proto/lb_fwlc.h>
41#include <proto/lb_fwrr.h>
42#include <proto/lb_map.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/log.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010044#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010045#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020046#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020048#include <proto/proto_tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010051#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020053#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020054#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010055#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/task.h>
57
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050058int be_lastsession(const struct proxy *be)
59{
60 if (be->be_counters.last_sess)
61 return now.tv_sec - be->be_counters.last_sess;
62
63 return -1;
64}
65
Bhaskar98634f02013-10-29 23:30:51 -040066/* helper function to invoke the correct hash method */
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070067static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040068{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070069 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040070
71 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
72 case BE_LB_HFCN_DJB2:
73 hash = hash_djb2(key, len);
74 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010075 case BE_LB_HFCN_WT6:
76 hash = hash_wt6(key, len);
77 break;
Bhaskar98634f02013-10-29 23:30:51 -040078 case BE_LB_HFCN_SDBM:
79 /* this is the default hash function */
80 default:
81 hash = hash_sdbm(key, len);
82 break;
83 }
84
85 return hash;
86}
87
Willy Tarreaubaaee002006-06-26 02:48:02 +020088/*
89 * This function recounts the number of usable active and backup servers for
90 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010091 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010092 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010093 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020095void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020096{
97 struct server *srv;
98
Willy Tarreau20697042007-11-15 23:26:18 +010099 px->srv_act = px->srv_bck = 0;
100 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100101 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200103 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100104 continue;
105
Willy Tarreauc93cd162014-05-13 15:54:22 +0200106 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100107 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100108 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100109 px->lbprm.fbck = srv;
110 px->srv_bck++;
111 px->lbprm.tot_wbck += srv->eweight;
112 } else {
113 px->srv_act++;
114 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 }
116 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100117}
Willy Tarreau20697042007-11-15 23:26:18 +0100118
Willy Tarreaub625a082007-11-26 01:15:43 +0100119/* This function simply updates the backend's tot_weight and tot_used values
120 * after servers weights have been updated. It is designed to be used after
121 * recount_servers() or equivalent.
122 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200123void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100124{
Willy Tarreau20697042007-11-15 23:26:18 +0100125 if (px->srv_act) {
126 px->lbprm.tot_weight = px->lbprm.tot_wact;
127 px->lbprm.tot_used = px->srv_act;
128 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100129 else if (px->lbprm.fbck) {
130 /* use only the first backup server */
131 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
132 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100133 }
134 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100135 px->lbprm.tot_weight = px->lbprm.tot_wbck;
136 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100137 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100138}
139
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200140/*
141 * This function tries to find a running server for the proxy <px> following
142 * the source hash method. 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 */
147struct server *get_server_sh(struct proxy *px, const char *addr, int len)
148{
149 unsigned int h, l;
150
151 if (px->lbprm.tot_weight == 0)
152 return NULL;
153
154 l = h = 0;
155
156 /* note: we won't hash if there's only one server left */
157 if (px->lbprm.tot_used == 1)
158 goto hash_done;
159
160 while ((l + sizeof (int)) <= len) {
161 h ^= ntohl(*(unsigned int *)(&addr[l]));
162 l += sizeof (int);
163 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500164 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100165 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200166 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200167 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
168 return chash_get_server_hash(px, h);
169 else
170 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200171}
172
173/*
174 * This function tries to find a running server for the proxy <px> following
175 * the URI hash method. In order to optimize cache hits, the hash computation
176 * ends at the question mark. Depending on the number of active/backup servers,
177 * it will either look for active servers, or for backup servers.
178 * If any server is found, it will be returned. If no valid server is found,
179 * NULL is returned.
180 *
181 * This code was contributed by Guillaume Dallaire, who also selected this hash
182 * algorithm out of a tens because it gave him the best results.
183 *
184 */
185struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
186{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700187 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200188 int c;
189 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400190 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200191
192 if (px->lbprm.tot_weight == 0)
193 return NULL;
194
195 /* note: we won't hash if there's only one server left */
196 if (px->lbprm.tot_used == 1)
197 goto hash_done;
198
199 if (px->uri_len_limit)
200 uri_len = MIN(uri_len, px->uri_len_limit);
201
Bhaskar98634f02013-10-29 23:30:51 -0400202 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200203 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200204 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200205 if (c == '/') {
206 slashes++;
207 if (slashes == px->uri_dirs_depth1) /* depth+1 */
208 break;
209 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100210 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200211 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200212 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200213 }
Bhaskar98634f02013-10-29 23:30:51 -0400214
215 hash = gen_hash(px, start, (end - start));
216
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500217 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100218 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200219 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200220 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
221 return chash_get_server_hash(px, hash);
222 else
223 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200224}
225
Willy Tarreau01732802007-11-01 22:48:15 +0100226/*
227 * This function tries to find a running server for the proxy <px> following
228 * the URL parameter hash method. It looks for a specific parameter in the
229 * URL and hashes it to compute the server ID. This is useful to optimize
230 * performance by avoiding bounces between servers in contexts where sessions
231 * are shared but cookies are not usable. If the parameter is not found, NULL
232 * is returned. If any server is found, it will be returned. If no valid server
233 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100234 */
235struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
236{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700237 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400238 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200239 const char *p;
240 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100241 int plen;
242
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200243 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100244 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100245 return NULL;
246
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200247 if ((p = memchr(uri, '?', uri_len)) == NULL)
248 return NULL;
249
Willy Tarreau01732802007-11-01 22:48:15 +0100250 p++;
251
252 uri_len -= (p - uri);
253 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100255
256 while (uri_len > plen) {
257 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258 if (params[plen] == '=') {
259 if (memcmp(params, px->url_param_name, plen) == 0) {
260 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100261 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100263 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400265 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266 uri_len -= plen + 1;
267
Bhaskar98634f02013-10-29 23:30:51 -0400268 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100269 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400270 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100271 }
Bhaskar98634f02013-10-29 23:30:51 -0400272 hash = gen_hash(px, start, (end - start));
273
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500274 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100275 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400276
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200277 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
278 return chash_get_server_hash(px, hash);
279 else
280 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100281 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200282 }
283 /* skip to next parameter */
284 p = memchr(params, '&', uri_len);
285 if (!p)
286 return NULL;
287 p++;
288 uri_len -= (p - params);
289 params = p;
290 }
291 return NULL;
292}
293
294/*
295 * this does the same as the previous server_ph, but check the body contents
296 */
297struct server *get_server_ph_post(struct session *s)
298{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700299 unsigned int hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200301 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200302 struct http_msg *msg = &txn->req;
303 struct proxy *px = s->be;
304 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200305 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200306 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100307 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400308 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309
Willy Tarreau157dd632009-12-06 19:18:09 +0100310 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 return NULL;
312
Willy Tarreau157dd632009-12-06 19:18:09 +0100313 if (px->lbprm.tot_weight == 0)
314 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200315
316 while (len > plen) {
317 /* Look for the parameter name followed by an equal symbol */
318 if (params[plen] == '=') {
319 if (memcmp(params, px->url_param_name, plen) == 0) {
320 /* OK, we have the parameter here at <params>, and
321 * the value after the equal sign, at <p>
322 * skip the equal symbol
323 */
324 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400325 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200326 len -= plen + 1;
327
Bhaskar98634f02013-10-29 23:30:51 -0400328 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200329 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100330 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400331 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100332 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200333 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
334 break;
335 return NULL; /* oh, no; this is not uri-encoded.
336 * This body does not contain parameters.
337 */
338 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200339 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400340 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200341 /* should we break if vlen exceeds limit? */
342 }
Bhaskar98634f02013-10-29 23:30:51 -0400343 hash = gen_hash(px, start, (end - start));
344
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500345 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100346 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400347
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200348 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
349 return chash_get_server_hash(px, hash);
350 else
351 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200352 }
353 }
Willy Tarreau01732802007-11-01 22:48:15 +0100354 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200355 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100356 if (!p)
357 return NULL;
358 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200359 len -= (p - params);
360 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100361 }
362 return NULL;
363}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200365
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366/*
Benoitaffb4812009-03-25 13:02:10 +0100367 * This function tries to find a running server for the proxy <px> following
368 * the Header parameter hash method. It looks for a specific parameter in the
369 * URL and hashes it to compute the server ID. This is useful to optimize
370 * performance by avoiding bounces between servers in contexts where sessions
371 * are shared but cookies are not usable. If the parameter is not found, NULL
372 * is returned. If any server is found, it will be returned. If no valid server
373 * is found, NULL is returned.
374 */
375struct server *get_server_hh(struct session *s)
376{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700377 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100378 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100379 struct proxy *px = s->be;
380 unsigned int plen = px->hh_len;
381 unsigned long len;
382 struct hdr_ctx ctx;
383 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400384 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100385
386 /* tot_weight appears to mean srv_count */
387 if (px->lbprm.tot_weight == 0)
388 return NULL;
389
Benoitaffb4812009-03-25 13:02:10 +0100390 ctx.idx = 0;
391
392 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200393 http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, -http_hdr_rewind(&txn->req)), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100394
395 /* if the header is not found or empty, let's fallback to round robin */
396 if (!ctx.idx || !ctx.vlen)
397 return NULL;
398
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200399 /* note: we won't hash if there's only one server left */
400 if (px->lbprm.tot_used == 1)
401 goto hash_done;
402
Benoitaffb4812009-03-25 13:02:10 +0100403 /* Found a the hh_name in the headers.
404 * we will compute the hash based on this value ctx.val.
405 */
406 len = ctx.vlen;
407 p = (char *)ctx.line + ctx.val;
408 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400409 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100410 } else {
411 int dohash = 0;
412 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400413 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100414 /* special computation, use only main domain name, not tld/host
415 * going back from the end of string, start hashing at first
416 * dot stop at next.
417 * This is designed to work with the 'Host' header, and requires
418 * a special option to activate this.
419 */
420 while (len) {
421 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400422 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100423 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400424 start = end = p - 1;
425 }
Benoitaffb4812009-03-25 13:02:10 +0100426 else
427 break;
428 } else {
429 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400430 start--;
Benoitaffb4812009-03-25 13:02:10 +0100431 }
432 len--;
433 p--;
434 }
Bhaskar98634f02013-10-29 23:30:51 -0400435 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100436 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500437 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100438 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200439 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200440 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
441 return chash_get_server_hash(px, hash);
442 else
443 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100444}
445
Willy Tarreau34db1082012-04-19 17:16:54 +0200446/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200447struct server *get_server_rch(struct session *s)
448{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700449 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200450 struct proxy *px = s->be;
451 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200452 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200453 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200454 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200455
456 /* tot_weight appears to mean srv_count */
457 if (px->lbprm.tot_weight == 0)
458 return NULL;
459
Willy Tarreau37406352012-04-23 16:16:37 +0200460 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200461
Willy Tarreau9b28e032012-10-12 23:49:43 +0200462 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200463
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200464 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200465 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100466
Willy Tarreau9b28e032012-10-12 23:49:43 +0200467 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200468
Willy Tarreau37406352012-04-23 16:16:37 +0200469 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200470 return NULL;
471
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200472 /* note: we won't hash if there's only one server left */
473 if (px->lbprm.tot_used == 1)
474 goto hash_done;
475
Emeric Brun736aa232009-06-30 17:56:00 +0200476 /* Found a the hh_name in the headers.
477 * we will compute the hash based on this value ctx.val.
478 */
Bhaskar98634f02013-10-29 23:30:51 -0400479 hash = gen_hash(px, smp.data.str.str, len);
480
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500481 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100482 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200483 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200484 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
485 return chash_get_server_hash(px, hash);
486 else
487 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200488}
Benoitaffb4812009-03-25 13:02:10 +0100489
490/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200491 * This function applies the load-balancing algorithm to the session, as
492 * defined by the backend it is assigned to. The session is then marked as
493 * 'assigned'.
494 *
495 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
496 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100497 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200498 * The function tries to keep the original connection slot if it reconnects to
499 * the same server, otherwise it releases it and tries to offer it.
500 *
501 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502 *
503 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100504 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200505 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
506 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507 * SRV_STATUS_INTERNAL for other unrecoverable errors.
508 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200509 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100510 * it does not need to be called anymore. This means that target_srv(&s->target)
511 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512 *
513 */
514
515int assign_server(struct session *s)
516{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200517 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200518 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100519 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100521
Simon Horman8effd3d2011-08-13 08:03:52 +0900522 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523
Willy Tarreau7c669d72008-06-20 15:04:11 +0200524 err = SRV_STATUS_INTERNAL;
525 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
526 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100527
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100528 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200529 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530
Willy Tarreau7c669d72008-06-20 15:04:11 +0200531 /* We have to release any connection slot before applying any LB algo,
532 * otherwise we may erroneously end up with no available slot.
533 */
534 if (conn_slot)
535 sess_change_server(s, NULL);
536
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100537 /* We will now try to find the good server and store it into <objt_server(s->target)>.
538 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200539 * as well as if no server is available (check error code).
540 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100541
Willy Tarreau827aee92011-03-10 16:55:02 +0100542 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100543 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100544 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100545
Willy Tarreau068621e2013-12-23 15:11:25 +0100546 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100547 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100548 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200549 ((s->txn.flags & TX_PREFER_LAST) ||
550 ((s->be->options & PR_O_PREF_LAST) &&
551 (!s->be->max_ka_queue ||
552 server_has_room(__objt_server(conn->target)) ||
553 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200554 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100555 /* This session was relying on a server in a previous request
Godbachf2dd68d2014-12-10 10:21:30 +0800556 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100557 * let's try to reuse the same server.
558 */
559 srv = __objt_server(conn->target);
560 s->target = &srv->obj_type;
561 }
562 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200563 /* we must check if we have at least one server available */
564 if (!s->be->lbprm.tot_weight) {
565 err = SRV_STATUS_NOSRV;
566 goto out;
567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200569 /* First check whether we need to fetch some data or simply call
570 * the LB lookup function. Only the hashing functions will need
571 * some input data in fact, and will support multiple algorithms.
572 */
573 switch (s->be->lbprm.algo & BE_LB_LKUP) {
574 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100575 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200576 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577
Willy Tarreauf09c6602012-02-13 17:12:08 +0100578 case BE_LB_LKUP_FSTREE:
579 srv = fas_get_next_server(s->be, prev_srv);
580 break;
581
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200582 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100583 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200584 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200586 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200587 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200588 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200589 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100590 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200591 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200593 break;
594 }
595 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200597 err = SRV_STATUS_INTERNAL;
598 goto out;
599 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600
601 switch (s->be->lbprm.algo & BE_LB_PARM) {
602 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200603 conn = objt_conn(s->req->prod->end);
604 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200605 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200606 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200607 4);
608 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200609 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200610 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200611 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200612 16);
613 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200614 else {
615 /* unknown IP family */
616 err = SRV_STATUS_INTERNAL;
617 goto out;
618 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619 break;
620
621 case BE_LB_HASH_URI:
622 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100623 if (s->txn.req.msg_state < HTTP_MSG_BODY)
624 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200626 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200628 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200629
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200630 case BE_LB_HASH_PRM:
631 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100632 if (s->txn.req.msg_state < HTTP_MSG_BODY)
633 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100634
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200636 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100637 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100638
Willy Tarreau827aee92011-03-10 16:55:02 +0100639 if (!srv && s->txn.meth == HTTP_METH_POST)
640 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200641 break;
Benoitaffb4812009-03-25 13:02:10 +0100642
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200643 case BE_LB_HASH_HDR:
644 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100645 if (s->txn.req.msg_state < HTTP_MSG_BODY)
646 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100647 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200648 break;
649
650 case BE_LB_HASH_RDP:
651 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100652 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200653 break;
654
655 default:
656 /* unknown balancing algorithm */
657 err = SRV_STATUS_INTERNAL;
658 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100659 }
Emeric Brun736aa232009-06-30 17:56:00 +0200660
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200661 /* If the hashing parameter was not found, let's fall
662 * back to round robin on the map.
663 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100664 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200665 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100666 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200667 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100668 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200669 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200670
671 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200672 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200673
Willy Tarreau7c669d72008-06-20 15:04:11 +0200674 default:
675 /* unknown balancing algorithm */
676 err = SRV_STATUS_INTERNAL;
677 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200679
Willy Tarreau827aee92011-03-10 16:55:02 +0100680 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200681 err = SRV_STATUS_FULL;
682 goto out;
683 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100684 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100685 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100686 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100687 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100688 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200689 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200690 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100691 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100693 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200694 (conn = objt_conn(s->req->cons->end)) &&
695 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100696 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100697 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100698 }
699 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200700 err = SRV_STATUS_NOSRV;
701 goto out;
702 }
703
704 s->flags |= SN_ASSIGNED;
705 err = SRV_STATUS_OK;
706 out:
707
708 /* Either we take back our connection slot, or we offer it to someone
709 * else if we don't need it anymore.
710 */
711 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100712 if (conn_slot == srv) {
713 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200714 } else {
715 if (may_dequeue_tasks(conn_slot, s->be))
716 process_srv_queue(conn_slot);
717 }
718 }
719
720 out_err:
721 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722}
723
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724/*
725 * This function assigns a server address to a session, and sets SN_ADDR_SET.
726 * The address is taken from the currently assigned server, or from the
727 * dispatch or transparent address.
728 *
729 * It may return :
730 * SRV_STATUS_OK if everything is OK.
731 * SRV_STATUS_INTERNAL for other unrecoverable errors.
732 *
733 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
734 * not cleared, so it's to the caller to clear it if required.
735 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200736 * The caller is responsible for having already assigned a connection
737 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200738 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 */
740int assign_server_address(struct session *s)
741{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200742 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200743 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200744
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745#ifdef DEBUG_FULL
746 fprintf(stderr,"assign_server_address : s=%p\n",s);
747#endif
748
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200749 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750 /* A server is necessarily known for this session */
751 if (!(s->flags & SN_ASSIGNED))
752 return SRV_STATUS_INTERNAL;
753
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200754 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200756 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200757 /* if the server has no address, we use the same address
758 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200759 * locally on multiple addresses at once. Nothing is done
760 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200761 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200762 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200763
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200764 if (cli_conn->addr.to.ss_family == AF_INET) {
765 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
766 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
767 ((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 +0200768 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200769 }
770
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 /* if this server remaps proxied ports, we'll use
772 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200773 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100774 int base_port;
775
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200776 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100777
778 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200779 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100780
781 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200782 base_port += get_host_port(&srv_conn->addr.to);
783 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784 }
785 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200786 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200788 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200790 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200792 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200794 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
795 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100797 else if (s->be->options & PR_O_HTTP_PROXY) {
798 /* If HTTP PROXY option is set, then server is already assigned
799 * during incoming client request parsing. */
800 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100801 else {
802 /* no server and no LB algorithm ! */
803 return SRV_STATUS_INTERNAL;
804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100806 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100807 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100808
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 s->flags |= SN_ADDR_SET;
810 return SRV_STATUS_OK;
811}
812
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813/* This function assigns a server to session <s> if required, and can add the
814 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200815 * If ->srv_conn is set, the session is first released from the server.
816 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
817 * be called before any connection and after any retry or redispatch occurs.
818 *
819 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820 *
821 * Returns :
822 *
823 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100824 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 * SRV_STATUS_QUEUED if the connection has been queued.
826 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200828 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 * SRV_STATUS_INTERNAL for other unrecoverable errors.
830 *
831 */
832int assign_server_and_queue(struct session *s)
833{
834 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100835 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 int err;
837
838 if (s->pend_pos)
839 return SRV_STATUS_INTERNAL;
840
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841 err = SRV_STATUS_OK;
842 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100843 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100844
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100846 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847 /* This session was previously assigned to a server. We have to
848 * update the session's and the server's stats :
849 * - if the server changed :
850 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
851 * - set SN_REDISP if it was successfully redispatched
852 * - increment srv->redispatches and be->redispatches
853 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100854 */
855
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100856 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
858 s->txn.flags &= ~TX_CK_MASK;
859 s->txn.flags |= TX_CK_DOWN;
860 }
861 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100862 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100863 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200864 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100865 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100866 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100867 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100868 }
869 }
870
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871 switch (err) {
872 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200873 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100874 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100875 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200876 return SRV_STATUS_OK; /* dispatch or proxy mode */
877
878 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100879 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 return SRV_STATUS_OK;
881
882 /* OK, this session already has an assigned server, but no
883 * connection slot yet. Either it is a redispatch, or it was
884 * assigned from persistence information (direct mode).
885 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100886 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200887 /* server scheduled for redirection, and already assigned. We
888 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100889 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100890 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100891 return SRV_STATUS_OK;
892 }
893
Willy Tarreau7c669d72008-06-20 15:04:11 +0200894 /* We might have to queue this session if the assigned server is full.
895 * We know we have to queue it into the server's queue, so if a maxqueue
896 * is set on the server, we must also check that the server's queue is
897 * not full, in which case we have to return FULL.
898 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100899 if (srv->maxconn &&
900 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901
Willy Tarreau827aee92011-03-10 16:55:02 +0100902 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200903 return SRV_STATUS_FULL;
904
Willy Tarreaubaaee002006-06-26 02:48:02 +0200905 p = pendconn_add(s);
906 if (p)
907 return SRV_STATUS_QUEUED;
908 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200909 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200911
912 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100913 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 return SRV_STATUS_OK;
915
916 case SRV_STATUS_FULL:
917 /* queue this session into the proxy's queue */
918 p = pendconn_add(s);
919 if (p)
920 return SRV_STATUS_QUEUED;
921 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200922 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923
924 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200925 return err;
926
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927 case SRV_STATUS_INTERNAL:
928 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200929
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 default:
931 return SRV_STATUS_INTERNAL;
932 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100933}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934
Willy Tarreaub1d67742010-03-29 19:36:59 +0200935/* If an explicit source binding is specified on the server and/or backend, and
936 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200937 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200938 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200939 */
940static void assign_tproxy_address(struct session *s)
941{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200942#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100943 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100944 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200945 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200946 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100947
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100948 if (srv && srv->conn_src.opts & CO_SRC_BIND)
949 src = &srv->conn_src;
950 else if (s->be->conn_src.opts & CO_SRC_BIND)
951 src = &s->be->conn_src;
952 else
953 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100954
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100955 switch (src->opts & CO_SRC_TPROXY_MASK) {
956 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200957 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100958 break;
959 case CO_SRC_TPROXY_CLI:
960 case CO_SRC_TPROXY_CIP:
961 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200962 cli_conn = objt_conn(s->req->prod->end);
963 if (cli_conn)
964 srv_conn->addr.from = cli_conn->addr.from;
965 else
966 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100967 break;
968 case CO_SRC_TPROXY_DYN:
969 if (src->bind_hdr_occ) {
970 char *vptr;
971 int vlen;
972 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100973
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100974 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200975 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
976 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
977 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100978
Willy Tarreau211cdec2014-04-17 20:18:08 +0200979 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100980 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
981 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200982 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100983 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200984 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100985 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200986 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100987 break;
988 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200989 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200990 }
991#endif
992}
993
994
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995/*
996 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200997 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100998 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999 * It can return one of :
1000 * - SN_ERR_NONE if everything's OK
1001 * - SN_ERR_SRVTO if there are no more servers
1002 * - SN_ERR_SRVCL if the connection was refused by the server
1003 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1004 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1005 * - SN_ERR_INTERNAL for any other purely internal errors
1006 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001007 * The server-facing stream interface is expected to hold a pre-allocated connection
1008 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 */
1010int connect_server(struct session *s)
1011{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001012 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001013 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001015 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001016 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017
Willy Tarreau34601a82013-12-15 16:33:46 +01001018 srv_conn = objt_conn(s->req->cons->end);
1019 if (srv_conn)
1020 reuse = s->target == srv_conn->target;
1021
1022 if (reuse) {
1023 /* Disable connection reuse if a dynamic source is used.
1024 * As long as we don't share connections between servers,
1025 * we don't need to disable connection reuse on no-idempotent
1026 * requests nor when PROXY protocol is used.
1027 */
1028 srv = objt_server(s->target);
1029 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1030 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1031 reuse = 0;
1032 }
1033 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1034 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1035 reuse = 0;
1036 }
1037 }
1038
1039 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001040 if (!srv_conn)
1041 return SN_ERR_RESOURCE;
1042
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 if (!(s->flags & SN_ADDR_SET)) {
1044 err = assign_server_address(s);
1045 if (err != SRV_STATUS_OK)
1046 return SN_ERR_INTERNAL;
1047 }
1048
Willy Tarreauff605db2013-12-20 11:09:51 +01001049 if (!conn_xprt_ready(srv_conn)) {
1050 /* the target was only on the session, assign it to the SI now */
1051 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001052
Willy Tarreauff605db2013-12-20 11:09:51 +01001053 /* set the correct protocol on the output stream interface */
1054 if (objt_server(s->target)) {
1055 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1056 }
1057 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1058 /* proxies exclusively run on raw_sock right now */
1059 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1060 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1061 return SN_ERR_INTERNAL;
1062 }
1063 else
1064 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001065
Willy Tarreauff605db2013-12-20 11:09:51 +01001066 /* process the case where the server requires the PROXY protocol to be sent */
1067 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001068 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001069 srv_conn->send_proxy_ofs = 1; /* must compute size */
1070 cli_conn = objt_conn(s->req->prod->end);
1071 if (cli_conn)
1072 conn_get_to_addr(cli_conn);
1073 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001074
Willy Tarreauff605db2013-12-20 11:09:51 +01001075 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001076
Willy Tarreauff605db2013-12-20 11:09:51 +01001077 assign_tproxy_address(s);
1078 }
1079 else {
1080 /* the connection is being reused, just re-attach it */
1081 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001082 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001083 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001084
William Lallemandb7ff6a32012-03-02 14:35:21 +01001085 /* flag for logging source ip/port */
1086 if (s->fe->options2 & PR_O2_SRC_ADDR)
1087 s->req->cons->flags |= SI_FL_SRC_ADDR;
1088
Willy Tarreau14f8e862012-08-30 22:23:13 +02001089 /* disable lingering */
1090 if (s->be->options & PR_O_TCP_NOLING)
1091 s->req->cons->flags |= SI_FL_NOLINGER;
1092
Willy Tarreau73b013b2012-05-21 16:31:45 +02001093 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094
Willy Tarreau9650f372009-08-16 14:02:45 +02001095 if (err != SN_ERR_NONE)
1096 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001097
Willy Tarreau14f8e862012-08-30 22:23:13 +02001098 /* set connect timeout */
1099 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1100
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001101 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001102 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001103 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001104 srv->cur_sess++;
1105 if (srv->cur_sess > srv->counters.cur_sess_max)
1106 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001107 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001108 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109 }
1110
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111 return SN_ERR_NONE; /* connection is OK */
1112}
1113
1114
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115/* This function performs the "redispatch" part of a connection attempt. It
1116 * will assign a server if required, queue the connection if required, and
1117 * handle errors that might arise at this level. It can change the server
1118 * state. It will return 1 if it encounters an error, switches the server
1119 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1120 * that the connection is ready to use.
1121 */
1122
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001123int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001124{
Willy Tarreau827aee92011-03-10 16:55:02 +01001125 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001126 int conn_err;
1127
1128 /* We know that we don't have any connection pending, so we will
1129 * try to get a new one, and wait in this state if it's queued
1130 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001131 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001132 conn_err = assign_server_and_queue(s);
1133 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001134
Willy Tarreaubaaee002006-06-26 02:48:02 +02001135 switch (conn_err) {
1136 case SRV_STATUS_OK:
1137 break;
1138
Willy Tarreau7c669d72008-06-20 15:04:11 +02001139 case SRV_STATUS_FULL:
1140 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1141 * and we can redispatch to another server, or it is not and we return
1142 * 503. This only makes sense in DIRECT mode however, because normal LB
1143 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001144 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001145 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001146 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001147 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1148 (s->be->options & PR_O_REDISP)) {
1149 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001150 goto redispatch;
1151 }
1152
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001153 if (!s->req->cons->err_type) {
1154 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001155 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001156
Willy Tarreau827aee92011-03-10 16:55:02 +01001157 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001158 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001159 return 1;
1160
Willy Tarreaubaaee002006-06-26 02:48:02 +02001161 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001162 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001163 if (!s->req->cons->err_type) {
1164 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001165 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001166
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001167 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168 return 1;
1169
1170 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001171 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1172 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173 /* do nothing else and do not wake any other session up */
1174 return 1;
1175
Willy Tarreaubaaee002006-06-26 02:48:02 +02001176 case SRV_STATUS_INTERNAL:
1177 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001178 if (!s->req->cons->err_type) {
1179 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001180 }
1181
Willy Tarreau827aee92011-03-10 16:55:02 +01001182 if (srv)
1183 srv_inc_sess_ctr(srv);
1184 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001185 srv_set_sess_last(srv);
1186 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001187 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001188 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189
1190 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001191 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001192 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001193 return 1;
1194 }
1195 /* if we get here, it's because we got SRV_STATUS_OK, which also
1196 * means that the connection has not been queued.
1197 */
1198 return 0;
1199}
1200
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001201/* sends a log message when a backend goes down, and also sets last
1202 * change date.
1203 */
1204void set_backend_down(struct proxy *be)
1205{
1206 be->last_change = now.tv_sec;
1207 be->down_trans++;
1208
1209 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1210 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1211}
1212
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001213/* Apply RDP cookie persistence to the current session. For this, the function
1214 * tries to extract an RDP cookie from the request buffer, and look for the
1215 * matching server in the list. If the server is found, it is assigned to the
1216 * session. This always returns 1, and the analyser removes itself from the
1217 * list. Nothing is performed if a server was already assigned.
1218 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001219int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001220{
1221 struct proxy *px = s->be;
1222 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001223 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001224 struct server *srv = px->srv;
1225 struct sockaddr_in addr;
1226 char *p;
1227
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001228 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 +02001229 now_ms, __FUNCTION__,
1230 s,
1231 req,
1232 req->rex, req->wex,
1233 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001235 req->analysers);
1236
1237 if (s->flags & SN_ASSIGNED)
1238 goto no_cookie;
1239
Willy Tarreau37406352012-04-23 16:16:37 +02001240 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001241
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001242 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001243 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001244 goto no_cookie;
1245
1246 memset(&addr, 0, sizeof(addr));
1247 addr.sin_family = AF_INET;
1248
Willy Tarreau664092c2011-12-16 19:11:42 +01001249 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001250 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001251 if (*p != '.')
1252 goto no_cookie;
1253 p++;
1254 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1255 if (*p != '.')
1256 goto no_cookie;
1257
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001258 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001259 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001260 if (srv->addr.ss_family == AF_INET &&
1261 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001262 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001263 /* we found the server and it is usable */
1264 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001265 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001266 break;
1267 }
1268 }
1269 srv = srv->next;
1270 }
1271
1272no_cookie:
1273 req->analysers &= ~an_bit;
1274 req->analyse_exp = TICK_ETERNITY;
1275 return 1;
1276}
1277
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001278int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001279 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001280 return px->down_time;
1281
1282 return now.tv_sec - px->last_change + px->down_time;
1283}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001285/*
1286 * This function returns a string containing the balancing
1287 * mode of the proxy in a format suitable for stats.
1288 */
1289
1290const char *backend_lb_algo_str(int algo) {
1291
1292 if (algo == BE_LB_ALGO_RR)
1293 return "roundrobin";
1294 else if (algo == BE_LB_ALGO_SRR)
1295 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001296 else if (algo == BE_LB_ALGO_FAS)
1297 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001298 else if (algo == BE_LB_ALGO_LC)
1299 return "leastconn";
1300 else if (algo == BE_LB_ALGO_SH)
1301 return "source";
1302 else if (algo == BE_LB_ALGO_UH)
1303 return "uri";
1304 else if (algo == BE_LB_ALGO_PH)
1305 return "url_param";
1306 else if (algo == BE_LB_ALGO_HH)
1307 return "hdr";
1308 else if (algo == BE_LB_ALGO_RCH)
1309 return "rdp-cookie";
1310 else
1311 return NULL;
1312}
1313
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001314/* This function parses a "balance" statement in a backend section describing
1315 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001316 * returns -1, it will write an error message into the <err> buffer which will
1317 * automatically be allocated and must be passed as NULL. The trailing '\n'
1318 * will not be written. The function must be called with <args> pointing to the
1319 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001320 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001321int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001322{
1323 if (!*(args[0])) {
1324 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001325 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1326 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001327 return 0;
1328 }
1329
1330 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001331 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1332 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001333 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001334 else if (!strcmp(args[0], "static-rr")) {
1335 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1336 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1337 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001338 else if (!strcmp(args[0], "first")) {
1339 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1340 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1341 }
Willy Tarreau51406232008-03-10 22:04:20 +01001342 else if (!strcmp(args[0], "leastconn")) {
1343 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1344 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1345 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001346 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001347 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1348 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001349 }
1350 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001351 int arg = 1;
1352
Willy Tarreau31682232007-11-29 15:38:04 +01001353 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1354 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001355
Oskar Stolc8dc41842012-05-19 10:19:54 +01001356 curproxy->uri_whole = 0;
1357
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001358 while (*args[arg]) {
1359 if (!strcmp(args[arg], "len")) {
1360 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001361 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001362 return -1;
1363 }
1364 curproxy->uri_len_limit = atoi(args[arg+1]);
1365 arg += 2;
1366 }
1367 else if (!strcmp(args[arg], "depth")) {
1368 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001369 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001370 return -1;
1371 }
1372 /* hint: we store the position of the ending '/' (depth+1) so
1373 * that we avoid a comparison while computing the hash.
1374 */
1375 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1376 arg += 2;
1377 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001378 else if (!strcmp(args[arg], "whole")) {
1379 curproxy->uri_whole = 1;
1380 arg += 1;
1381 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001382 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001383 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001384 return -1;
1385 }
1386 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001387 }
Willy Tarreau01732802007-11-01 22:48:15 +01001388 else if (!strcmp(args[0], "url_param")) {
1389 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001390 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001391 return -1;
1392 }
Willy Tarreau31682232007-11-29 15:38:04 +01001393 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1394 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001395
1396 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001397 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001398 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001399 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001400 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001401 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001402 return -1;
1403 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001404 }
Benoitaffb4812009-03-25 13:02:10 +01001405 }
1406 else if (!strncmp(args[0], "hdr(", 4)) {
1407 const char *beg, *end;
1408
1409 beg = args[0] + 4;
1410 end = strchr(beg, ')');
1411
1412 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001413 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001414 return -1;
1415 }
1416
1417 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1418 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1419
1420 free(curproxy->hh_name);
1421 curproxy->hh_len = end - beg;
1422 curproxy->hh_name = my_strndup(beg, end - beg);
1423 curproxy->hh_match_domain = 0;
1424
1425 if (*args[1]) {
1426 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001427 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001428 return -1;
1429 }
1430 curproxy->hh_match_domain = 1;
1431 }
Emeric Brun736aa232009-06-30 17:56:00 +02001432 }
1433 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1434 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1435 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1436
1437 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1438 const char *beg, *end;
1439
1440 beg = args[0] + 11;
1441 end = strchr(beg, ')');
1442
1443 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001444 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001445 return -1;
1446 }
1447
1448 free(curproxy->hh_name);
1449 curproxy->hh_name = my_strndup(beg, end - beg);
1450 curproxy->hh_len = end - beg;
1451 }
1452 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1453 free(curproxy->hh_name);
1454 curproxy->hh_name = strdup("mstshash");
1455 curproxy->hh_len = strlen(curproxy->hh_name);
1456 }
1457 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001458 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001459 return -1;
1460 }
Willy Tarreau01732802007-11-01 22:48:15 +01001461 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001462 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001463 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 +01001464 return -1;
1465 }
1466 return 0;
1467}
1468
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001469
1470/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001471/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001472/************************************************************************/
1473
Willy Tarreau34db1082012-04-19 17:16:54 +02001474/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001475 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001476 * undefined behaviour.
1477 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001478static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001479smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001480 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001481{
Willy Tarreau37406352012-04-23 16:16:37 +02001482 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001483 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001484 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001485
1486 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001487 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001488 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001489 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001490 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001491 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001492
1493 return 1;
1494}
1495
Willy Tarreau37406352012-04-23 16:16:37 +02001496/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001497 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001498 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1499 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001500 */
1501static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001502smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001503 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001504{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001505 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001506
Willy Tarreau37406352012-04-23 16:16:37 +02001507 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001508 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001509 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001510 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001511 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001512 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001513 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001514 return 1;
1515}
1516
Willy Tarreau34db1082012-04-19 17:16:54 +02001517/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001518 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001519 * undefined behaviour.
1520 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001521static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001522smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001523 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001524{
1525 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001526
Willy Tarreau37406352012-04-23 16:16:37 +02001527 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001528 smp->type = SMP_T_UINT;
1529 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001530
Willy Tarreau24e32d82012-04-23 23:55:44 +02001531 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001532 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001533 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001534
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001535 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001536 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001537 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001538 return 1;
1539 }
1540
Willy Tarreauf853c462012-04-23 18:53:56 +02001541 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001542 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001543 }
1544
1545 return 1;
1546}
1547
Willy Tarreaua5e37562011-12-16 17:06:15 +01001548/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001549static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001550smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001551 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001552{
Willy Tarreauf853c462012-04-23 18:53:56 +02001553 smp->flags = SMP_F_VOL_TXN;
1554 smp->type = SMP_T_UINT;
1555 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001556 return 1;
1557}
1558
Willy Tarreaua5e37562011-12-16 17:06:15 +01001559/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001560static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001561smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001562 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001563{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001564 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001565 return 0;
1566
Willy Tarreauf853c462012-04-23 18:53:56 +02001567 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001568 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001569
1570 return 1;
1571}
1572
Willy Tarreau34db1082012-04-19 17:16:54 +02001573/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001574 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001575 * undefined behaviour.
1576 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001577static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001578smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001579 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001580{
Willy Tarreau37406352012-04-23 16:16:37 +02001581 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001582 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001583 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001584 return 1;
1585}
1586
Willy Tarreau34db1082012-04-19 17:16:54 +02001587/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001588 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001589 * undefined behaviour.
1590 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001591static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001592smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001593 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001594{
Willy Tarreau37406352012-04-23 16:16:37 +02001595 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001596 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001597 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001598 return 1;
1599}
1600
Willy Tarreau34db1082012-04-19 17:16:54 +02001601/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001602 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001603 * undefined behaviour.
1604 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001605static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001606smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001607 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001608{
Willy Tarreau37406352012-04-23 16:16:37 +02001609 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001610 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001611 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001612 return 1;
1613}
1614
Willy Tarreaua5e37562011-12-16 17:06:15 +01001615/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001616 * by the number of running servers and rounded up. If there is no running
1617 * server, we return twice the total, just as if we had half a running server.
1618 * This is more or less correct anyway, since we expect the last server to come
1619 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001620 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001621 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001622 */
1623static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001624smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001625 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001626{
1627 int nbsrv;
1628
Willy Tarreau37406352012-04-23 16:16:37 +02001629 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001630 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001631 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001632
1633 if (px->srv_act)
1634 nbsrv = px->srv_act;
1635 else if (px->lbprm.fbck)
1636 nbsrv = 1;
1637 else
1638 nbsrv = px->srv_bck;
1639
1640 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001641 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001642 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001643 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001644
1645 return 1;
1646}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001647
Willy Tarreau34db1082012-04-19 17:16:54 +02001648/* set temp integer to the number of concurrent connections on the server in the backend.
1649 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1650 * undefined behaviour.
1651 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001652static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001653smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001654 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001655{
Willy Tarreauf853c462012-04-23 18:53:56 +02001656 smp->flags = SMP_F_VOL_TEST;
1657 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001658 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001659 return 1;
1660}
1661
Tait Clarridge7896d522012-12-05 21:39:31 -05001662/* set temp integer to the number of enabled servers on the proxy.
1663 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1664 * undefined behaviour.
1665 */
1666static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001667smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001668 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001669{
1670 smp->flags = SMP_F_VOL_TEST;
1671 smp->type = SMP_T_UINT;
1672 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1673 return 1;
1674}
1675
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001676
1677/* Note: must not be declared <const> as its list will be overwritten.
1678 * Please take care of keeping this list alphabetically sorted.
1679 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001680static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001681 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1682 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1683 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1684 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1685 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1686 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1687 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1688 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1689 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1690 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1691 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1692 { /* END */ },
1693}};
1694
1695
Willy Tarreau61612d42012-04-19 18:42:05 +02001696/* Note: must not be declared <const> as its list will be overwritten.
1697 * Please take care of keeping this list alphabetically sorted.
1698 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001699static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001700 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001701}};
1702
1703
1704__attribute__((constructor))
1705static void __backend_init(void)
1706{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001707 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001708 acl_register_keywords(&acl_kws);
1709}
1710
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711/*
1712 * Local variables:
1713 * c-indent-level: 8
1714 * c-basic-offset: 8
1715 * End:
1716 */