blob: 35edf75041422d680941d96447b4ff18d448fcec [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;
Cyril Bontéf607d812015-01-04 15:17:36 +0100412 p += len;
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 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100419 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100420 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100421 if (dohash) {
422 /* Rewind the pointer until the previous char
423 * is a dot, this will allow to set the start
424 * position of the domain. */
425 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100426 break;
Benoitaffb4812009-03-25 13:02:10 +0100427 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100428 else if (*p == '.') {
429 /* The pointer is rewinded to the dot before the
430 * tld, we memorize the end of the domain and
431 * can enter the domain processing. */
432 end = p;
433 dohash = 1;
434 }
Benoitaffb4812009-03-25 13:02:10 +0100435 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100436 len--;
Benoitaffb4812009-03-25 13:02:10 +0100437 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100438 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400439 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100440 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500441 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100442 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200443 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200444 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
445 return chash_get_server_hash(px, hash);
446 else
447 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100448}
449
Willy Tarreau34db1082012-04-19 17:16:54 +0200450/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200451struct server *get_server_rch(struct session *s)
452{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700453 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200454 struct proxy *px = s->be;
455 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200456 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200457 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200458 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200459
460 /* tot_weight appears to mean srv_count */
461 if (px->lbprm.tot_weight == 0)
462 return NULL;
463
Willy Tarreau37406352012-04-23 16:16:37 +0200464 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200465
Willy Tarreau9b28e032012-10-12 23:49:43 +0200466 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200467
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200468 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200469 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100470
Willy Tarreau9b28e032012-10-12 23:49:43 +0200471 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200472
Willy Tarreau37406352012-04-23 16:16:37 +0200473 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200474 return NULL;
475
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200476 /* note: we won't hash if there's only one server left */
477 if (px->lbprm.tot_used == 1)
478 goto hash_done;
479
Emeric Brun736aa232009-06-30 17:56:00 +0200480 /* Found a the hh_name in the headers.
481 * we will compute the hash based on this value ctx.val.
482 */
Bhaskar98634f02013-10-29 23:30:51 -0400483 hash = gen_hash(px, smp.data.str.str, len);
484
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500485 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100486 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200487 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200488 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
489 return chash_get_server_hash(px, hash);
490 else
491 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200492}
Benoitaffb4812009-03-25 13:02:10 +0100493
494/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200495 * This function applies the load-balancing algorithm to the session, as
496 * defined by the backend it is assigned to. The session is then marked as
497 * 'assigned'.
498 *
499 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
500 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100501 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200502 * The function tries to keep the original connection slot if it reconnects to
503 * the same server, otherwise it releases it and tries to offer it.
504 *
505 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506 *
507 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100508 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200509 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
510 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 * SRV_STATUS_INTERNAL for other unrecoverable errors.
512 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100514 * it does not need to be called anymore. This means that target_srv(&s->target)
515 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516 *
517 */
518
519int assign_server(struct session *s)
520{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200521 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100523 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200524 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100525
Simon Horman8effd3d2011-08-13 08:03:52 +0900526 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527
Willy Tarreau7c669d72008-06-20 15:04:11 +0200528 err = SRV_STATUS_INTERNAL;
529 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
530 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100531
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100532 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200533 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 /* We have to release any connection slot before applying any LB algo,
536 * otherwise we may erroneously end up with no available slot.
537 */
538 if (conn_slot)
539 sess_change_server(s, NULL);
540
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100541 /* We will now try to find the good server and store it into <objt_server(s->target)>.
542 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200543 * as well as if no server is available (check error code).
544 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100545
Willy Tarreau827aee92011-03-10 16:55:02 +0100546 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100547 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100548 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100549
Willy Tarreau068621e2013-12-23 15:11:25 +0100550 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100551 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100552 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200553 ((s->txn.flags & TX_PREFER_LAST) ||
554 ((s->be->options & PR_O_PREF_LAST) &&
555 (!s->be->max_ka_queue ||
556 server_has_room(__objt_server(conn->target)) ||
557 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200558 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100559 /* This session was relying on a server in a previous request
Godbachf2dd68d2014-12-10 10:21:30 +0800560 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100561 * let's try to reuse the same server.
562 */
563 srv = __objt_server(conn->target);
564 s->target = &srv->obj_type;
565 }
566 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200567 /* we must check if we have at least one server available */
568 if (!s->be->lbprm.tot_weight) {
569 err = SRV_STATUS_NOSRV;
570 goto out;
571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 /* First check whether we need to fetch some data or simply call
574 * the LB lookup function. Only the hashing functions will need
575 * some input data in fact, and will support multiple algorithms.
576 */
577 switch (s->be->lbprm.algo & BE_LB_LKUP) {
578 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100579 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200580 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200581
Willy Tarreauf09c6602012-02-13 17:12:08 +0100582 case BE_LB_LKUP_FSTREE:
583 srv = fas_get_next_server(s->be, prev_srv);
584 break;
585
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100587 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200588 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200589
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200590 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200592 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200593 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200595 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100596 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200597 break;
598 }
599 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200601 err = SRV_STATUS_INTERNAL;
602 goto out;
603 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604
605 switch (s->be->lbprm.algo & BE_LB_PARM) {
606 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200607 conn = objt_conn(s->req->prod->end);
608 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200609 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200610 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200611 4);
612 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200613 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200614 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200615 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200616 16);
617 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200618 else {
619 /* unknown IP family */
620 err = SRV_STATUS_INTERNAL;
621 goto out;
622 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200623 break;
624
625 case BE_LB_HASH_URI:
626 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100627 if (s->txn.req.msg_state < HTTP_MSG_BODY)
628 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200630 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200632 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200633
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200634 case BE_LB_HASH_PRM:
635 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100636 if (s->txn.req.msg_state < HTTP_MSG_BODY)
637 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100638
Willy Tarreau827aee92011-03-10 16:55:02 +0100639 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200640 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100642
Willy Tarreau827aee92011-03-10 16:55:02 +0100643 if (!srv && s->txn.meth == HTTP_METH_POST)
644 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200645 break;
Benoitaffb4812009-03-25 13:02:10 +0100646
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 case BE_LB_HASH_HDR:
648 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100649 if (s->txn.req.msg_state < HTTP_MSG_BODY)
650 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200652 break;
653
654 case BE_LB_HASH_RDP:
655 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200657 break;
658
659 default:
660 /* unknown balancing algorithm */
661 err = SRV_STATUS_INTERNAL;
662 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100663 }
Emeric Brun736aa232009-06-30 17:56:00 +0200664
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200665 /* If the hashing parameter was not found, let's fall
666 * back to round robin on the map.
667 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100668 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200669 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100670 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200671 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200673 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200674
675 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200676 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200677
Willy Tarreau7c669d72008-06-20 15:04:11 +0200678 default:
679 /* unknown balancing algorithm */
680 err = SRV_STATUS_INTERNAL;
681 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200683
Willy Tarreau827aee92011-03-10 16:55:02 +0100684 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200685 err = SRV_STATUS_FULL;
686 goto out;
687 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100688 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100689 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100690 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100691 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100692 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200693 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200694 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100695 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100697 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200698 (conn = objt_conn(s->req->cons->end)) &&
699 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100700 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100701 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100702 }
703 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200704 err = SRV_STATUS_NOSRV;
705 goto out;
706 }
707
708 s->flags |= SN_ASSIGNED;
709 err = SRV_STATUS_OK;
710 out:
711
712 /* Either we take back our connection slot, or we offer it to someone
713 * else if we don't need it anymore.
714 */
715 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100716 if (conn_slot == srv) {
717 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200718 } else {
719 if (may_dequeue_tasks(conn_slot, s->be))
720 process_srv_queue(conn_slot);
721 }
722 }
723
724 out_err:
725 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726}
727
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728/*
729 * This function assigns a server address to a session, and sets SN_ADDR_SET.
730 * The address is taken from the currently assigned server, or from the
731 * dispatch or transparent address.
732 *
733 * It may return :
734 * SRV_STATUS_OK if everything is OK.
735 * SRV_STATUS_INTERNAL for other unrecoverable errors.
736 *
737 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
738 * not cleared, so it's to the caller to clear it if required.
739 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200740 * The caller is responsible for having already assigned a connection
741 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200742 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 */
744int assign_server_address(struct session *s)
745{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200746 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200747 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200748
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749#ifdef DEBUG_FULL
750 fprintf(stderr,"assign_server_address : s=%p\n",s);
751#endif
752
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200753 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 /* A server is necessarily known for this session */
755 if (!(s->flags & SN_ASSIGNED))
756 return SRV_STATUS_INTERNAL;
757
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200758 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200760 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200761 /* if the server has no address, we use the same address
762 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200763 * locally on multiple addresses at once. Nothing is done
764 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200765 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200766 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200767
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 if (cli_conn->addr.to.ss_family == AF_INET) {
769 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
770 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
771 ((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 +0200772 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200773 }
774
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775 /* if this server remaps proxied ports, we'll use
776 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200777 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100778 int base_port;
779
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100781
782 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200783 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100784
785 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200786 base_port += get_host_port(&srv_conn->addr.to);
787 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 }
789 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200790 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200792 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200794 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200796 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200798 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
799 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100801 else if (s->be->options & PR_O_HTTP_PROXY) {
802 /* If HTTP PROXY option is set, then server is already assigned
803 * during incoming client request parsing. */
804 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100805 else {
806 /* no server and no LB algorithm ! */
807 return SRV_STATUS_INTERNAL;
808 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100810 /* Copy network namespace from client connection */
Thierry FOURNIERfe1ebcd2014-12-19 13:37:11 +0100811 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100812
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 s->flags |= SN_ADDR_SET;
814 return SRV_STATUS_OK;
815}
816
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817/* This function assigns a server to session <s> if required, and can add the
818 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200819 * If ->srv_conn is set, the session is first released from the server.
820 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
821 * be called before any connection and after any retry or redispatch occurs.
822 *
823 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200824 *
825 * Returns :
826 *
827 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100828 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 * SRV_STATUS_QUEUED if the connection has been queued.
830 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100831 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200832 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 * SRV_STATUS_INTERNAL for other unrecoverable errors.
834 *
835 */
836int assign_server_and_queue(struct session *s)
837{
838 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100839 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 int err;
841
842 if (s->pend_pos)
843 return SRV_STATUS_INTERNAL;
844
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845 err = SRV_STATUS_OK;
846 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100847 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100848
Willy Tarreau7c669d72008-06-20 15:04:11 +0200849 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100850 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200851 /* This session was previously assigned to a server. We have to
852 * update the session's and the server's stats :
853 * - if the server changed :
854 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
855 * - set SN_REDISP if it was successfully redispatched
856 * - increment srv->redispatches and be->redispatches
857 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100858 */
859
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100860 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
862 s->txn.flags &= ~TX_CK_MASK;
863 s->txn.flags |= TX_CK_DOWN;
864 }
865 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100866 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100867 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200868 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100869 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100870 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100871 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100872 }
873 }
874
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875 switch (err) {
876 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200877 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100878 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100879 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 return SRV_STATUS_OK; /* dispatch or proxy mode */
881
882 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100883 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200884 return SRV_STATUS_OK;
885
886 /* OK, this session already has an assigned server, but no
887 * connection slot yet. Either it is a redispatch, or it was
888 * assigned from persistence information (direct mode).
889 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100890 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200891 /* server scheduled for redirection, and already assigned. We
892 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100894 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100895 return SRV_STATUS_OK;
896 }
897
Willy Tarreau7c669d72008-06-20 15:04:11 +0200898 /* We might have to queue this session if the assigned server is full.
899 * We know we have to queue it into the server's queue, so if a maxqueue
900 * is set on the server, we must also check that the server's queue is
901 * not full, in which case we have to return FULL.
902 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100903 if (srv->maxconn &&
904 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200905
Willy Tarreau827aee92011-03-10 16:55:02 +0100906 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200907 return SRV_STATUS_FULL;
908
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909 p = pendconn_add(s);
910 if (p)
911 return SRV_STATUS_QUEUED;
912 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200913 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200915
916 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100917 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918 return SRV_STATUS_OK;
919
920 case SRV_STATUS_FULL:
921 /* queue this session into the proxy's queue */
922 p = pendconn_add(s);
923 if (p)
924 return SRV_STATUS_QUEUED;
925 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200926 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927
928 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200929 return err;
930
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 case SRV_STATUS_INTERNAL:
932 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 default:
935 return SRV_STATUS_INTERNAL;
936 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100937}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938
Willy Tarreaub1d67742010-03-29 19:36:59 +0200939/* If an explicit source binding is specified on the server and/or backend, and
940 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200941 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200942 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200943 */
944static void assign_tproxy_address(struct session *s)
945{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200946#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100947 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100948 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200949 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200950 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100951
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100952 if (srv && srv->conn_src.opts & CO_SRC_BIND)
953 src = &srv->conn_src;
954 else if (s->be->conn_src.opts & CO_SRC_BIND)
955 src = &s->be->conn_src;
956 else
957 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100958
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100959 switch (src->opts & CO_SRC_TPROXY_MASK) {
960 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200961 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100962 break;
963 case CO_SRC_TPROXY_CLI:
964 case CO_SRC_TPROXY_CIP:
965 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200966 cli_conn = objt_conn(s->req->prod->end);
967 if (cli_conn)
968 srv_conn->addr.from = cli_conn->addr.from;
969 else
970 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100971 break;
972 case CO_SRC_TPROXY_DYN:
973 if (src->bind_hdr_occ) {
974 char *vptr;
975 int vlen;
976 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100977
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100978 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200979 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
980 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
981 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100982
Willy Tarreau211cdec2014-04-17 20:18:08 +0200983 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100984 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
985 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200986 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100987 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200988 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100989 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200990 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100991 break;
992 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200993 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200994 }
995#endif
996}
997
998
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999/*
1000 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +02001001 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001002 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003 * It can return one of :
1004 * - SN_ERR_NONE if everything's OK
1005 * - SN_ERR_SRVTO if there are no more servers
1006 * - SN_ERR_SRVCL if the connection was refused by the server
1007 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1008 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1009 * - SN_ERR_INTERNAL for any other purely internal errors
1010 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001011 * The server-facing stream interface is expected to hold a pre-allocated connection
1012 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 */
1014int connect_server(struct session *s)
1015{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001016 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001017 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001018 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001019 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001020 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021
Willy Tarreau34601a82013-12-15 16:33:46 +01001022 srv_conn = objt_conn(s->req->cons->end);
1023 if (srv_conn)
1024 reuse = s->target == srv_conn->target;
1025
1026 if (reuse) {
1027 /* Disable connection reuse if a dynamic source is used.
1028 * As long as we don't share connections between servers,
1029 * we don't need to disable connection reuse on no-idempotent
1030 * requests nor when PROXY protocol is used.
1031 */
1032 srv = objt_server(s->target);
1033 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1034 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1035 reuse = 0;
1036 }
1037 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1038 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1039 reuse = 0;
1040 }
1041 }
1042
1043 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001044 if (!srv_conn)
1045 return SN_ERR_RESOURCE;
1046
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047 if (!(s->flags & SN_ADDR_SET)) {
1048 err = assign_server_address(s);
1049 if (err != SRV_STATUS_OK)
1050 return SN_ERR_INTERNAL;
1051 }
1052
Willy Tarreauff605db2013-12-20 11:09:51 +01001053 if (!conn_xprt_ready(srv_conn)) {
1054 /* the target was only on the session, assign it to the SI now */
1055 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001056
Willy Tarreauff605db2013-12-20 11:09:51 +01001057 /* set the correct protocol on the output stream interface */
1058 if (objt_server(s->target)) {
1059 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1060 }
1061 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1062 /* proxies exclusively run on raw_sock right now */
1063 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1064 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1065 return SN_ERR_INTERNAL;
1066 }
1067 else
1068 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001069
Willy Tarreauff605db2013-12-20 11:09:51 +01001070 /* process the case where the server requires the PROXY protocol to be sent */
1071 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001072 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001073 srv_conn->send_proxy_ofs = 1; /* must compute size */
1074 cli_conn = objt_conn(s->req->prod->end);
1075 if (cli_conn)
1076 conn_get_to_addr(cli_conn);
1077 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001078
Willy Tarreauff605db2013-12-20 11:09:51 +01001079 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001080
Willy Tarreauff605db2013-12-20 11:09:51 +01001081 assign_tproxy_address(s);
1082 }
1083 else {
1084 /* the connection is being reused, just re-attach it */
1085 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001086 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001087 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001088
William Lallemandb7ff6a32012-03-02 14:35:21 +01001089 /* flag for logging source ip/port */
1090 if (s->fe->options2 & PR_O2_SRC_ADDR)
1091 s->req->cons->flags |= SI_FL_SRC_ADDR;
1092
Willy Tarreau14f8e862012-08-30 22:23:13 +02001093 /* disable lingering */
1094 if (s->be->options & PR_O_TCP_NOLING)
1095 s->req->cons->flags |= SI_FL_NOLINGER;
1096
Willy Tarreau73b013b2012-05-21 16:31:45 +02001097 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098
Willy Tarreau9650f372009-08-16 14:02:45 +02001099 if (err != SN_ERR_NONE)
1100 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001101
Willy Tarreau14f8e862012-08-30 22:23:13 +02001102 /* set connect timeout */
1103 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1104
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001105 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001106 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001107 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001108 srv->cur_sess++;
1109 if (srv->cur_sess > srv->counters.cur_sess_max)
1110 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001111 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001112 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 }
1114
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115 return SN_ERR_NONE; /* connection is OK */
1116}
1117
1118
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119/* This function performs the "redispatch" part of a connection attempt. It
1120 * will assign a server if required, queue the connection if required, and
1121 * handle errors that might arise at this level. It can change the server
1122 * state. It will return 1 if it encounters an error, switches the server
1123 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1124 * that the connection is ready to use.
1125 */
1126
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001127int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128{
Willy Tarreau827aee92011-03-10 16:55:02 +01001129 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130 int conn_err;
1131
1132 /* We know that we don't have any connection pending, so we will
1133 * try to get a new one, and wait in this state if it's queued
1134 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001135 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001136 conn_err = assign_server_and_queue(s);
1137 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001138
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139 switch (conn_err) {
1140 case SRV_STATUS_OK:
1141 break;
1142
Willy Tarreau7c669d72008-06-20 15:04:11 +02001143 case SRV_STATUS_FULL:
1144 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1145 * and we can redispatch to another server, or it is not and we return
1146 * 503. This only makes sense in DIRECT mode however, because normal LB
1147 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001148 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001149 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001150 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001151 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1152 (s->be->options & PR_O_REDISP)) {
1153 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001154 goto redispatch;
1155 }
1156
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001157 if (!s->req->cons->err_type) {
1158 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001159 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001160
Willy Tarreau827aee92011-03-10 16:55:02 +01001161 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001162 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001163 return 1;
1164
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001166 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001167 if (!s->req->cons->err_type) {
1168 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001169 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001170
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001171 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001172 return 1;
1173
1174 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001175 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1176 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177 /* do nothing else and do not wake any other session up */
1178 return 1;
1179
Willy Tarreaubaaee002006-06-26 02:48:02 +02001180 case SRV_STATUS_INTERNAL:
1181 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001182 if (!s->req->cons->err_type) {
1183 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001184 }
1185
Willy Tarreau827aee92011-03-10 16:55:02 +01001186 if (srv)
1187 srv_inc_sess_ctr(srv);
1188 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001189 srv_set_sess_last(srv);
1190 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001191 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001192 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001193
1194 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001195 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001196 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 return 1;
1198 }
1199 /* if we get here, it's because we got SRV_STATUS_OK, which also
1200 * means that the connection has not been queued.
1201 */
1202 return 0;
1203}
1204
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001205/* sends a log message when a backend goes down, and also sets last
1206 * change date.
1207 */
1208void set_backend_down(struct proxy *be)
1209{
1210 be->last_change = now.tv_sec;
1211 be->down_trans++;
1212
1213 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1214 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1215}
1216
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001217/* Apply RDP cookie persistence to the current session. For this, the function
1218 * tries to extract an RDP cookie from the request buffer, and look for the
1219 * matching server in the list. If the server is found, it is assigned to the
1220 * session. This always returns 1, and the analyser removes itself from the
1221 * list. Nothing is performed if a server was already assigned.
1222 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001223int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001224{
1225 struct proxy *px = s->be;
1226 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001227 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001228 struct server *srv = px->srv;
1229 struct sockaddr_in addr;
1230 char *p;
1231
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001232 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 +02001233 now_ms, __FUNCTION__,
1234 s,
1235 req,
1236 req->rex, req->wex,
1237 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001238 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001239 req->analysers);
1240
1241 if (s->flags & SN_ASSIGNED)
1242 goto no_cookie;
1243
Willy Tarreau37406352012-04-23 16:16:37 +02001244 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001245
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001246 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001247 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001248 goto no_cookie;
1249
1250 memset(&addr, 0, sizeof(addr));
1251 addr.sin_family = AF_INET;
1252
Willy Tarreau664092c2011-12-16 19:11:42 +01001253 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001254 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001255 if (*p != '.')
1256 goto no_cookie;
1257 p++;
1258 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1259 if (*p != '.')
1260 goto no_cookie;
1261
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001262 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001263 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001264 if (srv->addr.ss_family == AF_INET &&
1265 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001266 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001267 /* we found the server and it is usable */
1268 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001269 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001270 break;
1271 }
1272 }
1273 srv = srv->next;
1274 }
1275
1276no_cookie:
1277 req->analysers &= ~an_bit;
1278 req->analyse_exp = TICK_ETERNITY;
1279 return 1;
1280}
1281
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001282int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001283 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001284 return px->down_time;
1285
1286 return now.tv_sec - px->last_change + px->down_time;
1287}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001288
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001289/*
1290 * This function returns a string containing the balancing
1291 * mode of the proxy in a format suitable for stats.
1292 */
1293
1294const char *backend_lb_algo_str(int algo) {
1295
1296 if (algo == BE_LB_ALGO_RR)
1297 return "roundrobin";
1298 else if (algo == BE_LB_ALGO_SRR)
1299 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001300 else if (algo == BE_LB_ALGO_FAS)
1301 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001302 else if (algo == BE_LB_ALGO_LC)
1303 return "leastconn";
1304 else if (algo == BE_LB_ALGO_SH)
1305 return "source";
1306 else if (algo == BE_LB_ALGO_UH)
1307 return "uri";
1308 else if (algo == BE_LB_ALGO_PH)
1309 return "url_param";
1310 else if (algo == BE_LB_ALGO_HH)
1311 return "hdr";
1312 else if (algo == BE_LB_ALGO_RCH)
1313 return "rdp-cookie";
1314 else
1315 return NULL;
1316}
1317
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001318/* This function parses a "balance" statement in a backend section describing
1319 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001320 * returns -1, it will write an error message into the <err> buffer which will
1321 * automatically be allocated and must be passed as NULL. The trailing '\n'
1322 * will not be written. The function must be called with <args> pointing to the
1323 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001324 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001325int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001326{
1327 if (!*(args[0])) {
1328 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001329 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1330 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001331 return 0;
1332 }
1333
1334 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001335 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1336 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001337 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001338 else if (!strcmp(args[0], "static-rr")) {
1339 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1340 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1341 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001342 else if (!strcmp(args[0], "first")) {
1343 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1344 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1345 }
Willy Tarreau51406232008-03-10 22:04:20 +01001346 else if (!strcmp(args[0], "leastconn")) {
1347 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1348 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1349 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001350 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001351 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1352 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001353 }
1354 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001355 int arg = 1;
1356
Willy Tarreau31682232007-11-29 15:38:04 +01001357 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1358 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001359
Oskar Stolc8dc41842012-05-19 10:19:54 +01001360 curproxy->uri_whole = 0;
1361
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001362 while (*args[arg]) {
1363 if (!strcmp(args[arg], "len")) {
1364 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001365 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001366 return -1;
1367 }
1368 curproxy->uri_len_limit = atoi(args[arg+1]);
1369 arg += 2;
1370 }
1371 else if (!strcmp(args[arg], "depth")) {
1372 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001373 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001374 return -1;
1375 }
1376 /* hint: we store the position of the ending '/' (depth+1) so
1377 * that we avoid a comparison while computing the hash.
1378 */
1379 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1380 arg += 2;
1381 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001382 else if (!strcmp(args[arg], "whole")) {
1383 curproxy->uri_whole = 1;
1384 arg += 1;
1385 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001386 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001387 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001388 return -1;
1389 }
1390 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001391 }
Willy Tarreau01732802007-11-01 22:48:15 +01001392 else if (!strcmp(args[0], "url_param")) {
1393 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001394 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001395 return -1;
1396 }
Willy Tarreau31682232007-11-29 15:38:04 +01001397 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1398 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001399
1400 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001401 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001402 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001403 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001404 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001405 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001406 return -1;
1407 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001408 }
Benoitaffb4812009-03-25 13:02:10 +01001409 }
1410 else if (!strncmp(args[0], "hdr(", 4)) {
1411 const char *beg, *end;
1412
1413 beg = args[0] + 4;
1414 end = strchr(beg, ')');
1415
1416 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001417 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001418 return -1;
1419 }
1420
1421 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1422 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1423
1424 free(curproxy->hh_name);
1425 curproxy->hh_len = end - beg;
1426 curproxy->hh_name = my_strndup(beg, end - beg);
1427 curproxy->hh_match_domain = 0;
1428
1429 if (*args[1]) {
1430 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001431 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001432 return -1;
1433 }
1434 curproxy->hh_match_domain = 1;
1435 }
Emeric Brun736aa232009-06-30 17:56:00 +02001436 }
1437 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1438 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1439 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1440
1441 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1442 const char *beg, *end;
1443
1444 beg = args[0] + 11;
1445 end = strchr(beg, ')');
1446
1447 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001448 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001449 return -1;
1450 }
1451
1452 free(curproxy->hh_name);
1453 curproxy->hh_name = my_strndup(beg, end - beg);
1454 curproxy->hh_len = end - beg;
1455 }
1456 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1457 free(curproxy->hh_name);
1458 curproxy->hh_name = strdup("mstshash");
1459 curproxy->hh_len = strlen(curproxy->hh_name);
1460 }
1461 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001462 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001463 return -1;
1464 }
Willy Tarreau01732802007-11-01 22:48:15 +01001465 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001466 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001467 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 +01001468 return -1;
1469 }
1470 return 0;
1471}
1472
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001473
1474/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001475/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001476/************************************************************************/
1477
Willy Tarreau34db1082012-04-19 17:16:54 +02001478/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001479 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001480 * undefined behaviour.
1481 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001482static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001483smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001484 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001485{
Willy Tarreau37406352012-04-23 16:16:37 +02001486 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001487 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001488 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001489
1490 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001491 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001492 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001493 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001494 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001495 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001496
1497 return 1;
1498}
1499
Willy Tarreau37406352012-04-23 16:16:37 +02001500/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001501 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001502 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1503 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001504 */
1505static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001506smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001507 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001508{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001509 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001510
Willy Tarreau37406352012-04-23 16:16:37 +02001511 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001512 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001513 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001514 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001515 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001516 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001517 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001518 return 1;
1519}
1520
Willy Tarreau34db1082012-04-19 17:16:54 +02001521/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001522 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001523 * undefined behaviour.
1524 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001525static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001526smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001527 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001528{
1529 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001530
Willy Tarreau37406352012-04-23 16:16:37 +02001531 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001532 smp->type = SMP_T_UINT;
1533 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001534
Willy Tarreau24e32d82012-04-23 23:55:44 +02001535 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001536 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001537 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001538
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001539 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001540 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001541 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001542 return 1;
1543 }
1544
Willy Tarreauf853c462012-04-23 18:53:56 +02001545 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001546 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001547 }
1548
1549 return 1;
1550}
1551
Willy Tarreaua5e37562011-12-16 17:06:15 +01001552/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001553static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001554smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001555 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001556{
Willy Tarreauf853c462012-04-23 18:53:56 +02001557 smp->flags = SMP_F_VOL_TXN;
1558 smp->type = SMP_T_UINT;
1559 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001560 return 1;
1561}
1562
Willy Tarreaua5e37562011-12-16 17:06:15 +01001563/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001564static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001565smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001566 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001567{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001568 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001569 return 0;
1570
Willy Tarreauf853c462012-04-23 18:53:56 +02001571 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001572 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001573
1574 return 1;
1575}
1576
Willy Tarreau34db1082012-04-19 17:16:54 +02001577/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001578 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001579 * undefined behaviour.
1580 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001581static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001582smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001583 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001584{
Willy Tarreau37406352012-04-23 16:16:37 +02001585 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001586 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001587 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001588 return 1;
1589}
1590
Willy Tarreau34db1082012-04-19 17:16:54 +02001591/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001592 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001593 * undefined behaviour.
1594 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001595static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001596smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001597 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001598{
Willy Tarreau37406352012-04-23 16:16:37 +02001599 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001600 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001601 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001602 return 1;
1603}
1604
Willy Tarreau34db1082012-04-19 17:16:54 +02001605/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001606 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001607 * undefined behaviour.
1608 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001609static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001610smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001611 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001612{
Willy Tarreau37406352012-04-23 16:16:37 +02001613 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001614 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001615 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001616 return 1;
1617}
1618
Willy Tarreaua5e37562011-12-16 17:06:15 +01001619/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001620 * by the number of running servers and rounded up. If there is no running
1621 * server, we return twice the total, just as if we had half a running server.
1622 * This is more or less correct anyway, since we expect the last server to come
1623 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001624 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001625 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001626 */
1627static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001628smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001629 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001630{
1631 int nbsrv;
1632
Willy Tarreau37406352012-04-23 16:16:37 +02001633 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001634 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001635 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001636
1637 if (px->srv_act)
1638 nbsrv = px->srv_act;
1639 else if (px->lbprm.fbck)
1640 nbsrv = 1;
1641 else
1642 nbsrv = px->srv_bck;
1643
1644 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001645 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001646 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001647 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001648
1649 return 1;
1650}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001651
Willy Tarreau34db1082012-04-19 17:16:54 +02001652/* set temp integer to the number of concurrent connections on the server in the backend.
1653 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1654 * undefined behaviour.
1655 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001656static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001657smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001658 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001659{
Willy Tarreauf853c462012-04-23 18:53:56 +02001660 smp->flags = SMP_F_VOL_TEST;
1661 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001662 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001663 return 1;
1664}
1665
Tait Clarridge7896d522012-12-05 21:39:31 -05001666/* set temp integer to the number of enabled servers on the proxy.
1667 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1668 * undefined behaviour.
1669 */
1670static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001671smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001672 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001673{
1674 smp->flags = SMP_F_VOL_TEST;
1675 smp->type = SMP_T_UINT;
1676 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1677 return 1;
1678}
1679
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001680
1681/* Note: must not be declared <const> as its list will be overwritten.
1682 * Please take care of keeping this list alphabetically sorted.
1683 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001684static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001685 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1686 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1687 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1688 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1689 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1690 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1691 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1692 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1693 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1694 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1695 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1696 { /* END */ },
1697}};
1698
1699
Willy Tarreau61612d42012-04-19 18:42:05 +02001700/* Note: must not be declared <const> as its list will be overwritten.
1701 * Please take care of keeping this list alphabetically sorted.
1702 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001703static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001704 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001705}};
1706
1707
1708__attribute__((constructor))
1709static void __backend_init(void)
1710{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001711 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001712 acl_register_keywords(&acl_kws);
1713}
1714
Willy Tarreaubaaee002006-06-26 02:48:02 +02001715/*
1716 * Local variables:
1717 * c-indent-level: 8
1718 * c-basic-offset: 8
1719 * End:
1720 */