blob: 4615c90e6bf906edc1cede9c2bc8e82004984fad [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau1a7eca12013-01-07 22:38:03 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <syslog.h>
Willy Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +020019#include <ctype.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040020#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreauc7e42382012-08-24 19:22:53 +020022#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020025#include <common/debug.h>
Bhaskar98634f02013-10-29 23:30:51 -040026#include <common/hash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010032#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020033#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020037#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010038#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020039#include <proto/lb_fwlc.h>
40#include <proto/lb_fwrr.h>
41#include <proto/lb_map.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020042#include <proto/log.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010043#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010044#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020047#include <proto/proto_tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010050#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020052#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020053#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010054#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/task.h>
56
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050057int be_lastsession(const struct proxy *be)
58{
59 if (be->be_counters.last_sess)
60 return now.tv_sec - be->be_counters.last_sess;
61
62 return -1;
63}
64
Bhaskar98634f02013-10-29 23:30:51 -040065/* helper function to invoke the correct hash method */
Dan Dubovik82456752014-07-08 08:51:03 -070066static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040067{
Dan Dubovik82456752014-07-08 08:51:03 -070068 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040069
70 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
71 case BE_LB_HFCN_DJB2:
72 hash = hash_djb2(key, len);
73 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010074 case BE_LB_HFCN_WT6:
75 hash = hash_wt6(key, len);
76 break;
Bhaskar98634f02013-10-29 23:30:51 -040077 case BE_LB_HFCN_SDBM:
78 /* this is the default hash function */
79 default:
80 hash = hash_sdbm(key, len);
81 break;
82 }
83
84 return hash;
85}
86
Willy Tarreaubaaee002006-06-26 02:48:02 +020087/*
88 * This function recounts the number of usable active and backup servers for
89 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010090 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010091 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010092 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020093 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020094void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020095{
96 struct server *srv;
97
Willy Tarreau20697042007-11-15 23:26:18 +010098 px->srv_act = px->srv_bck = 0;
99 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200102 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100103 continue;
104
Willy Tarreauc93cd162014-05-13 15:54:22 +0200105 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100106 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100107 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100108 px->lbprm.fbck = srv;
109 px->srv_bck++;
110 px->lbprm.tot_wbck += srv->eweight;
111 } else {
112 px->srv_act++;
113 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 }
115 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100116}
Willy Tarreau20697042007-11-15 23:26:18 +0100117
Willy Tarreaub625a082007-11-26 01:15:43 +0100118/* This function simply updates the backend's tot_weight and tot_used values
119 * after servers weights have been updated. It is designed to be used after
120 * recount_servers() or equivalent.
121 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200122void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100123{
Willy Tarreau20697042007-11-15 23:26:18 +0100124 if (px->srv_act) {
125 px->lbprm.tot_weight = px->lbprm.tot_wact;
126 px->lbprm.tot_used = px->srv_act;
127 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100128 else if (px->lbprm.fbck) {
129 /* use only the first backup server */
130 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
131 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100132 }
133 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100134 px->lbprm.tot_weight = px->lbprm.tot_wbck;
135 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100136 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100137}
138
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200139/*
140 * This function tries to find a running server for the proxy <px> following
141 * the source hash method. Depending on the number of active/backup servers,
142 * it will either look for active servers, or for backup servers.
143 * If any server is found, it will be returned. If no valid server is found,
144 * NULL is returned.
145 */
146struct server *get_server_sh(struct proxy *px, const char *addr, int len)
147{
148 unsigned int h, l;
149
150 if (px->lbprm.tot_weight == 0)
151 return NULL;
152
153 l = h = 0;
154
155 /* note: we won't hash if there's only one server left */
156 if (px->lbprm.tot_used == 1)
157 goto hash_done;
158
159 while ((l + sizeof (int)) <= len) {
160 h ^= ntohl(*(unsigned int *)(&addr[l]));
161 l += sizeof (int);
162 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500163 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100164 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200165 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200166 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
167 return chash_get_server_hash(px, h);
168 else
169 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200170}
171
172/*
173 * This function tries to find a running server for the proxy <px> following
174 * the URI hash method. In order to optimize cache hits, the hash computation
175 * ends at the question mark. Depending on the number of active/backup servers,
176 * it will either look for active servers, or for backup servers.
177 * If any server is found, it will be returned. If no valid server is found,
178 * NULL is returned.
179 *
180 * This code was contributed by Guillaume Dallaire, who also selected this hash
181 * algorithm out of a tens because it gave him the best results.
182 *
183 */
184struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
185{
Dan Dubovik82456752014-07-08 08:51:03 -0700186 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200187 int c;
188 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400189 const char *start, *end;
natalie.chendf7a7142015-03-31 15:54:07 +0800190 int depth = 1;
natalie.chen4fcfe812018-08-15 19:14:40 +0800191 int orig_uri_len = uri_len;
192 const char *orig_start;
193 const char *p, *params;
194 int buflen = 0;
195 char *buf;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200196
197 if (px->lbprm.tot_weight == 0)
198 return NULL;
199
200 /* note: we won't hash if there's only one server left */
201 if (px->lbprm.tot_used == 1)
202 goto hash_done;
203
204 if (px->uri_len_limit)
205 uri_len = MIN(uri_len, px->uri_len_limit);
206
natalie.chen4fcfe812018-08-15 19:14:40 +0800207 orig_start = start = end = uri;
208 if (px->uri_gerrit == 1) {
209 p = memchr(uri, '?', uri_len);
210 params = p;
211 if (p) {
212 p++;
213 uri_len = p - uri;
214 params = p;
215 if (p[7] == '=') {
216 if (memcmp(p, "service", 7) == 0) {
217 p += 8;
218 depth = 2;
219 }
220 }
221 }
222
natalie.chenb320ad42016-08-01 10:57:46 +0800223 end = uri + uri_len - 1;
224 while (uri_len--) {
225 c = *end;
226 if (c == '/') {
natalie.chen4fcfe812018-08-15 19:14:40 +0800227 if (p == NULL)
228 p = end + 1;
natalie.chenb320ad42016-08-01 10:57:46 +0800229 slashes++;
230 if (slashes == depth)
231 break;
232 }
natalie.chenb320ad42016-08-01 10:57:46 +0800233 end--;
234 }
natalie.chen4fcfe812018-08-15 19:14:40 +0800235 if (!strncmp(p, "git-receive-pack", 16) || !strncmp(p, "git-upload-pack", 15)) {
236 // we have git request here
237 // get rid of redundant '/'
238 while ((end - start) > 1 && *(start+1) == '/')
239 start++;
240 while ((end - start) > 1 && *(end-1) == '/')
241 end--;
242 // get rid of trailing ".git"
243 if ((end - start) > 4 && strncmp(end-4, ".git", 4) == 0)
244 end -= 4;
natalie.chenb320ad42016-08-01 10:57:46 +0800245
natalie.chen4fcfe812018-08-15 19:14:40 +0800246 hash = gen_hash(px, start, (end - start));
247 } else {
248 if (px->uri_dirs_depth1 > 0){
249 // reset state
250 uri_len = orig_uri_len;
251 if (params != NULL)
252 uri_len = params - uri;
253 depth = px->uri_dirs_depth1;
254 slashes = 0;
255 buf = (char *)malloc(uri_len);
256 orig_start = start = end = uri;
257 while (uri_len--) {
258 c = *end;
259 if (c == '/') {
260 if (slashes == 1 && end - start == 2) {
261 depth += 1;
262 orig_start = start = end;
263 }
264 slashes++;
265 if (slashes == depth - 1) {
266 if (memcmp(start, "/changes", end - start) != 0 && memcmp(start, "/projects", end - start) != 0)
267 break;
268 start = end;
269 p = start;
270 }
271 else if (slashes == depth) { /* depth+1 */
272 memcpy(buf + buflen, p, end - p);
273 buflen += (end - p);
274 break;
275 }
276 }
277 else if (c == '%') {
278 if (orig_start != start && uri_len > 2 && memcmp(end, "%2F", 3) == 0) {
279 memcpy(buf + buflen, p, end - p);
280 buflen += (end - p) + 1;
281 buf[buflen - 1] = '/';
282 p = end + 3;
283 }
284 }
285 else if (c == '~') {
286 memcpy(buf + buflen, p, end - p);
287 buflen += (end - p);
288 break;
289 }
290 end++;
291 }
292 }
293 if (buflen > 0)
294 hash = gen_hash(px, buf, buflen);
295 if (buf != NULL)
296 free(buf);
297 if (buflen <= 0) /*let's fallback to round robin */
298 return NULL;
299 }
natalie.chenb320ad42016-08-01 10:57:46 +0800300 } else {
301 while (uri_len--) {
302 c = *end;
303 if (c == '/') {
304 slashes++;
305 if (slashes == px->uri_dirs_depth1) /* depth+1 */
306 break;
307 }
308 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200309 break;
natalie.chenb320ad42016-08-01 10:57:46 +0800310 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200311 }
natalie.chen4fcfe812018-08-15 19:14:40 +0800312 hash = gen_hash(px, start, (end - start));
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200313 }
natalie.chene22be3d2015-06-25 09:33:52 +0800314
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500315 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100316 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200317 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200318 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
319 return chash_get_server_hash(px, hash);
320 else
321 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200322}
323
Willy Tarreau01732802007-11-01 22:48:15 +0100324/*
325 * This function tries to find a running server for the proxy <px> following
326 * the URL parameter hash method. It looks for a specific parameter in the
327 * URL and hashes it to compute the server ID. This is useful to optimize
328 * performance by avoiding bounces between servers in contexts where sessions
329 * are shared but cookies are not usable. If the parameter is not found, NULL
330 * is returned. If any server is found, it will be returned. If no valid server
331 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100332 */
333struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
334{
Dan Dubovik82456752014-07-08 08:51:03 -0700335 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400336 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200337 const char *p;
338 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100339 int plen;
340
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200341 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100342 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100343 return NULL;
344
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200345 if ((p = memchr(uri, '?', uri_len)) == NULL)
346 return NULL;
347
Willy Tarreau01732802007-11-01 22:48:15 +0100348 p++;
349
350 uri_len -= (p - uri);
351 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200352 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100353
354 while (uri_len > plen) {
355 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200356 if (params[plen] == '=') {
357 if (memcmp(params, px->url_param_name, plen) == 0) {
358 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100359 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200360 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100361 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200362 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400363 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200364 uri_len -= plen + 1;
365
Bhaskar98634f02013-10-29 23:30:51 -0400366 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100367 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400368 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100369 }
Bhaskar98634f02013-10-29 23:30:51 -0400370 hash = gen_hash(px, start, (end - start));
371
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500372 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100373 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400374
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200375 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
376 return chash_get_server_hash(px, hash);
377 else
378 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100379 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200380 }
381 /* skip to next parameter */
382 p = memchr(params, '&', uri_len);
383 if (!p)
384 return NULL;
385 p++;
386 uri_len -= (p - params);
387 params = p;
388 }
389 return NULL;
390}
391
392/*
393 * this does the same as the previous server_ph, but check the body contents
394 */
395struct server *get_server_ph_post(struct session *s)
396{
Dan Dubovik82456752014-07-08 08:51:03 -0700397 unsigned int hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200398 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200399 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200400 struct http_msg *msg = &txn->req;
401 struct proxy *px = s->be;
402 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200403 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200404 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100405 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400406 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200407
Willy Tarreau157dd632009-12-06 19:18:09 +0100408 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200409 return NULL;
410
Willy Tarreau522aab32015-05-02 00:05:47 +0200411 if (len > req->buf->data + req->buf->size - p)
412 len = req->buf->data + req->buf->size - p;
413
Willy Tarreau157dd632009-12-06 19:18:09 +0100414 if (px->lbprm.tot_weight == 0)
415 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200416
417 while (len > plen) {
418 /* Look for the parameter name followed by an equal symbol */
419 if (params[plen] == '=') {
420 if (memcmp(params, px->url_param_name, plen) == 0) {
421 /* OK, we have the parameter here at <params>, and
422 * the value after the equal sign, at <p>
423 * skip the equal symbol
424 */
425 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400426 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200427 len -= plen + 1;
428
Bhaskar98634f02013-10-29 23:30:51 -0400429 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200430 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100431 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400432 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100433 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200434 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
435 break;
436 return NULL; /* oh, no; this is not uri-encoded.
437 * This body does not contain parameters.
438 */
439 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200440 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400441 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200442 /* should we break if vlen exceeds limit? */
443 }
Bhaskar98634f02013-10-29 23:30:51 -0400444 hash = gen_hash(px, start, (end - start));
445
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500446 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100447 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400448
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200449 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
450 return chash_get_server_hash(px, hash);
451 else
452 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200453 }
454 }
Willy Tarreau01732802007-11-01 22:48:15 +0100455 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200456 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100457 if (!p)
458 return NULL;
459 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200460 len -= (p - params);
461 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100462 }
463 return NULL;
464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200466
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467/*
Benoitaffb4812009-03-25 13:02:10 +0100468 * This function tries to find a running server for the proxy <px> following
469 * the Header parameter hash method. It looks for a specific parameter in the
470 * URL and hashes it to compute the server ID. This is useful to optimize
471 * performance by avoiding bounces between servers in contexts where sessions
472 * are shared but cookies are not usable. If the parameter is not found, NULL
473 * is returned. If any server is found, it will be returned. If no valid server
474 * is found, NULL is returned.
475 */
476struct server *get_server_hh(struct session *s)
477{
Dan Dubovik82456752014-07-08 08:51:03 -0700478 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100479 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100480 struct proxy *px = s->be;
481 unsigned int plen = px->hh_len;
482 unsigned long len;
483 struct hdr_ctx ctx;
484 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400485 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100486
487 /* tot_weight appears to mean srv_count */
488 if (px->lbprm.tot_weight == 0)
489 return NULL;
490
Benoitaffb4812009-03-25 13:02:10 +0100491 ctx.idx = 0;
492
493 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200494 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 +0100495
496 /* if the header is not found or empty, let's fallback to round robin */
497 if (!ctx.idx || !ctx.vlen)
498 return NULL;
499
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200500 /* note: we won't hash if there's only one server left */
501 if (px->lbprm.tot_used == 1)
502 goto hash_done;
503
Benoitaffb4812009-03-25 13:02:10 +0100504 /* Found a the hh_name in the headers.
505 * we will compute the hash based on this value ctx.val.
506 */
507 len = ctx.vlen;
508 p = (char *)ctx.line + ctx.val;
509 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400510 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100511 } else {
512 int dohash = 0;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100513 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100514 /* special computation, use only main domain name, not tld/host
515 * going back from the end of string, start hashing at first
516 * dot stop at next.
517 * This is designed to work with the 'Host' header, and requires
518 * a special option to activate this.
519 */
Cyril Bonté7ccea262015-01-04 15:17:36 +0100520 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100521 while (len) {
Cyril Bonté7ccea262015-01-04 15:17:36 +0100522 if (dohash) {
523 /* Rewind the pointer until the previous char
524 * is a dot, this will allow to set the start
525 * position of the domain. */
526 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100527 break;
Benoitaffb4812009-03-25 13:02:10 +0100528 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100529 else if (*p == '.') {
530 /* The pointer is rewinded to the dot before the
531 * tld, we memorize the end of the domain and
532 * can enter the domain processing. */
533 end = p;
534 dohash = 1;
535 }
Benoitaffb4812009-03-25 13:02:10 +0100536 p--;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100537 len--;
Benoitaffb4812009-03-25 13:02:10 +0100538 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100539 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400540 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100541 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500542 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100543 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200544 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200545 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
546 return chash_get_server_hash(px, hash);
547 else
548 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100549}
550
Willy Tarreau34db1082012-04-19 17:16:54 +0200551/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200552struct server *get_server_rch(struct session *s)
553{
Dan Dubovik82456752014-07-08 08:51:03 -0700554 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200555 struct proxy *px = s->be;
556 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200557 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200558 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200559 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200560
561 /* tot_weight appears to mean srv_count */
562 if (px->lbprm.tot_weight == 0)
563 return NULL;
564
Willy Tarreau37406352012-04-23 16:16:37 +0200565 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200566
Willy Tarreau9b28e032012-10-12 23:49:43 +0200567 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200568
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200569 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200570 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100571
Willy Tarreau9b28e032012-10-12 23:49:43 +0200572 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200573
Willy Tarreau37406352012-04-23 16:16:37 +0200574 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200575 return NULL;
576
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200577 /* note: we won't hash if there's only one server left */
578 if (px->lbprm.tot_used == 1)
579 goto hash_done;
580
Emeric Brun736aa232009-06-30 17:56:00 +0200581 /* Found a the hh_name in the headers.
582 * we will compute the hash based on this value ctx.val.
583 */
Bhaskar98634f02013-10-29 23:30:51 -0400584 hash = gen_hash(px, smp.data.str.str, len);
585
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500586 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100587 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200588 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200589 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
590 return chash_get_server_hash(px, hash);
591 else
592 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200593}
Benoitaffb4812009-03-25 13:02:10 +0100594
595/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200596 * This function applies the load-balancing algorithm to the session, as
597 * defined by the backend it is assigned to. The session is then marked as
598 * 'assigned'.
599 *
600 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
601 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100602 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200603 * The function tries to keep the original connection slot if it reconnects to
604 * the same server, otherwise it releases it and tries to offer it.
605 *
606 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200607 *
608 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100609 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200610 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
611 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612 * SRV_STATUS_INTERNAL for other unrecoverable errors.
613 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200614 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 * it does not need to be called anymore. This means that target_srv(&s->target)
616 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617 *
618 */
619
620int assign_server(struct session *s)
621{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200622 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200623 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100624 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200625 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100626
Simon Horman8effd3d2011-08-13 08:03:52 +0900627 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628
Willy Tarreau7c669d72008-06-20 15:04:11 +0200629 err = SRV_STATUS_INTERNAL;
630 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
631 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100632
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100633 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200634 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635
Willy Tarreau7c669d72008-06-20 15:04:11 +0200636 /* We have to release any connection slot before applying any LB algo,
637 * otherwise we may erroneously end up with no available slot.
638 */
639 if (conn_slot)
640 sess_change_server(s, NULL);
641
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 /* We will now try to find the good server and store it into <objt_server(s->target)>.
643 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200644 * as well as if no server is available (check error code).
645 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100646
Willy Tarreau827aee92011-03-10 16:55:02 +0100647 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100648 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100649 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100650
Willy Tarreau068621e2013-12-23 15:11:25 +0100651 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100652 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100653 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200654 ((s->txn.flags & TX_PREFER_LAST) ||
655 ((s->be->options & PR_O_PREF_LAST) &&
656 (!s->be->max_ka_queue ||
657 server_has_room(__objt_server(conn->target)) ||
658 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200659 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100660 /* This session was relying on a server in a previous request
Godbachcfbb93f2014-12-10 10:21:30 +0800661 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100662 * let's try to reuse the same server.
663 */
664 srv = __objt_server(conn->target);
665 s->target = &srv->obj_type;
666 }
667 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200668 /* we must check if we have at least one server available */
669 if (!s->be->lbprm.tot_weight) {
670 err = SRV_STATUS_NOSRV;
671 goto out;
672 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200674 /* First check whether we need to fetch some data or simply call
675 * the LB lookup function. Only the hashing functions will need
676 * some input data in fact, and will support multiple algorithms.
677 */
678 switch (s->be->lbprm.algo & BE_LB_LKUP) {
679 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100680 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200681 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200682
Willy Tarreauf09c6602012-02-13 17:12:08 +0100683 case BE_LB_LKUP_FSTREE:
684 srv = fas_get_next_server(s->be, prev_srv);
685 break;
686
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100688 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200689 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200690
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200691 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200692 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200693 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200694 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100695 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200696 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100697 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200698 break;
699 }
700 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200701 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200702 err = SRV_STATUS_INTERNAL;
703 goto out;
704 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200705
706 switch (s->be->lbprm.algo & BE_LB_PARM) {
707 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200708 conn = objt_conn(s->req->prod->end);
709 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200710 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200711 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200712 4);
713 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200714 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200715 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200716 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200717 16);
718 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200719 else {
720 /* unknown IP family */
721 err = SRV_STATUS_INTERNAL;
722 goto out;
723 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200724 break;
725
726 case BE_LB_HASH_URI:
727 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100728 if (s->txn.req.msg_state < HTTP_MSG_BODY)
729 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100730 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200731 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100732 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200733 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200734
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200735 case BE_LB_HASH_PRM:
736 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100737 if (s->txn.req.msg_state < HTTP_MSG_BODY)
738 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100739
Willy Tarreau827aee92011-03-10 16:55:02 +0100740 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200741 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100742 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100743
Willy Tarreau827aee92011-03-10 16:55:02 +0100744 if (!srv && s->txn.meth == HTTP_METH_POST)
745 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200746 break;
Benoitaffb4812009-03-25 13:02:10 +0100747
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200748 case BE_LB_HASH_HDR:
749 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100750 if (s->txn.req.msg_state < HTTP_MSG_BODY)
751 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100752 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200753 break;
754
755 case BE_LB_HASH_RDP:
756 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100757 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200758 break;
759
760 default:
761 /* unknown balancing algorithm */
762 err = SRV_STATUS_INTERNAL;
763 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100764 }
Emeric Brun736aa232009-06-30 17:56:00 +0200765
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200766 /* If the hashing parameter was not found, let's fall
767 * back to round robin on the map.
768 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100769 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200770 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200772 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200774 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200775
776 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200777 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200778
Willy Tarreau7c669d72008-06-20 15:04:11 +0200779 default:
780 /* unknown balancing algorithm */
781 err = SRV_STATUS_INTERNAL;
782 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200784
Willy Tarreau827aee92011-03-10 16:55:02 +0100785 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200786 err = SRV_STATUS_FULL;
787 goto out;
788 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100789 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100790 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100791 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100792 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100793 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200794 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200795 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100796 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100798 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200799 (conn = objt_conn(s->req->cons->end)) &&
800 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100801 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100802 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100803 }
804 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200805 err = SRV_STATUS_NOSRV;
806 goto out;
807 }
808
809 s->flags |= SN_ASSIGNED;
810 err = SRV_STATUS_OK;
811 out:
812
813 /* Either we take back our connection slot, or we offer it to someone
814 * else if we don't need it anymore.
815 */
816 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (conn_slot == srv) {
818 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200819 } else {
820 if (may_dequeue_tasks(conn_slot, s->be))
821 process_srv_queue(conn_slot);
822 }
823 }
824
825 out_err:
826 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827}
828
829
830/*
831 * This function assigns a server address to a session, and sets SN_ADDR_SET.
832 * The address is taken from the currently assigned server, or from the
833 * dispatch or transparent address.
834 *
835 * It may return :
836 * SRV_STATUS_OK if everything is OK.
837 * SRV_STATUS_INTERNAL for other unrecoverable errors.
838 *
839 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
840 * not cleared, so it's to the caller to clear it if required.
841 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200842 * The caller is responsible for having already assigned a connection
843 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200844 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 */
846int assign_server_address(struct session *s)
847{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200848 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200849 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200850
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851#ifdef DEBUG_FULL
852 fprintf(stderr,"assign_server_address : s=%p\n",s);
853#endif
854
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200855 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 /* A server is necessarily known for this session */
857 if (!(s->flags & SN_ASSIGNED))
858 return SRV_STATUS_INTERNAL;
859
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200860 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200862 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200863 /* if the server has no address, we use the same address
864 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200865 * locally on multiple addresses at once. Nothing is done
866 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200867 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200868 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200869
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200870 if (cli_conn->addr.to.ss_family == AF_INET) {
871 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
872 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
873 ((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 +0200874 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200875 }
876
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877 /* if this server remaps proxied ports, we'll use
878 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200879 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100880 int base_port;
881
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200882 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100883
884 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200885 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100886
887 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200888 base_port += get_host_port(&srv_conn->addr.to);
889 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890 }
891 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200892 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200894 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200896 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200898 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200900 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
901 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100903 else if (s->be->options & PR_O_HTTP_PROXY) {
904 /* If HTTP PROXY option is set, then server is already assigned
905 * during incoming client request parsing. */
906 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100907 else {
908 /* no server and no LB algorithm ! */
909 return SRV_STATUS_INTERNAL;
910 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911
912 s->flags |= SN_ADDR_SET;
913 return SRV_STATUS_OK;
914}
915
916
917/* This function assigns a server to session <s> if required, and can add the
918 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919 * If ->srv_conn is set, the session is first released from the server.
920 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
921 * be called before any connection and after any retry or redispatch occurs.
922 *
923 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 *
925 * Returns :
926 *
927 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100928 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 * SRV_STATUS_QUEUED if the connection has been queued.
930 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200932 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933 * SRV_STATUS_INTERNAL for other unrecoverable errors.
934 *
935 */
936int assign_server_and_queue(struct session *s)
937{
938 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100939 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 int err;
941
942 if (s->pend_pos)
943 return SRV_STATUS_INTERNAL;
944
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945 err = SRV_STATUS_OK;
946 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100947 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100948
Willy Tarreau7c669d72008-06-20 15:04:11 +0200949 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100950 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200951 /* This session was previously assigned to a server. We have to
952 * update the session's and the server's stats :
953 * - if the server changed :
954 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
955 * - set SN_REDISP if it was successfully redispatched
956 * - increment srv->redispatches and be->redispatches
957 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100958 */
959
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100960 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200961 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
962 s->txn.flags &= ~TX_CK_MASK;
963 s->txn.flags |= TX_CK_DOWN;
964 }
965 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100966 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100967 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200968 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100969 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100970 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100971 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100972 }
973 }
974
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 switch (err) {
976 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200977 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100978 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100979 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200980 return SRV_STATUS_OK; /* dispatch or proxy mode */
981
982 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100983 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200984 return SRV_STATUS_OK;
985
986 /* OK, this session already has an assigned server, but no
987 * connection slot yet. Either it is a redispatch, or it was
988 * assigned from persistence information (direct mode).
989 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100990 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200991 /* server scheduled for redirection, and already assigned. We
992 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100994 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100995 return SRV_STATUS_OK;
996 }
997
Willy Tarreau7c669d72008-06-20 15:04:11 +0200998 /* We might have to queue this session if the assigned server is full.
999 * We know we have to queue it into the server's queue, so if a maxqueue
1000 * is set on the server, we must also check that the server's queue is
1001 * not full, in which case we have to return FULL.
1002 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001003 if (srv->maxconn &&
1004 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001005
Willy Tarreau827aee92011-03-10 16:55:02 +01001006 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +02001007 return SRV_STATUS_FULL;
1008
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 p = pendconn_add(s);
1010 if (p)
1011 return SRV_STATUS_QUEUED;
1012 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001013 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001015
1016 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +01001017 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 return SRV_STATUS_OK;
1019
1020 case SRV_STATUS_FULL:
1021 /* queue this session into the proxy's queue */
1022 p = pendconn_add(s);
1023 if (p)
1024 return SRV_STATUS_QUEUED;
1025 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001026 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027
1028 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +02001029 return err;
1030
Willy Tarreaubaaee002006-06-26 02:48:02 +02001031 case SRV_STATUS_INTERNAL:
1032 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001033
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 default:
1035 return SRV_STATUS_INTERNAL;
1036 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +01001037}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038
Willy Tarreaub1d67742010-03-29 19:36:59 +02001039/* If an explicit source binding is specified on the server and/or backend, and
1040 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001041 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001042 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +02001043 */
1044static void assign_tproxy_address(struct session *s)
1045{
Pieter Baauwd551fb52013-05-08 22:49:23 +02001046#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001047 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001048 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001049 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001050 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +01001051
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001052 if (srv && srv->conn_src.opts & CO_SRC_BIND)
1053 src = &srv->conn_src;
1054 else if (s->be->conn_src.opts & CO_SRC_BIND)
1055 src = &s->be->conn_src;
1056 else
1057 return;
Willy Tarreau294c4732011-12-16 21:35:50 +01001058
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001059 switch (src->opts & CO_SRC_TPROXY_MASK) {
1060 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001061 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001062 break;
1063 case CO_SRC_TPROXY_CLI:
1064 case CO_SRC_TPROXY_CIP:
1065 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001066 cli_conn = objt_conn(s->req->prod->end);
1067 if (cli_conn)
1068 srv_conn->addr.from = cli_conn->addr.from;
1069 else
1070 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001071 break;
1072 case CO_SRC_TPROXY_DYN:
1073 if (src->bind_hdr_occ) {
1074 char *vptr;
1075 int vlen;
1076 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +01001077
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001078 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001079 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
1080 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
1081 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +01001082
Willy Tarreau211cdec2014-04-17 20:18:08 +02001083 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001084 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
1085 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001086 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001087 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +02001088 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001089 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001090 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001091 break;
1092 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001093 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001094 }
1095#endif
1096}
1097
1098
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099/*
1100 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +02001101 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001102 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103 * It can return one of :
1104 * - SN_ERR_NONE if everything's OK
1105 * - SN_ERR_SRVTO if there are no more servers
1106 * - SN_ERR_SRVCL if the connection was refused by the server
1107 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1108 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1109 * - SN_ERR_INTERNAL for any other purely internal errors
1110 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001111 * The server-facing stream interface is expected to hold a pre-allocated connection
1112 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 */
1114int connect_server(struct session *s)
1115{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001116 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001117 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001118 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001119 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001120 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121
Willy Tarreau34601a82013-12-15 16:33:46 +01001122 srv_conn = objt_conn(s->req->cons->end);
1123 if (srv_conn)
1124 reuse = s->target == srv_conn->target;
1125
1126 if (reuse) {
1127 /* Disable connection reuse if a dynamic source is used.
1128 * As long as we don't share connections between servers,
1129 * we don't need to disable connection reuse on no-idempotent
1130 * requests nor when PROXY protocol is used.
1131 */
1132 srv = objt_server(s->target);
1133 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1134 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1135 reuse = 0;
1136 }
1137 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1138 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1139 reuse = 0;
1140 }
1141 }
1142
1143 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001144 if (!srv_conn)
1145 return SN_ERR_RESOURCE;
1146
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147 if (!(s->flags & SN_ADDR_SET)) {
1148 err = assign_server_address(s);
1149 if (err != SRV_STATUS_OK)
1150 return SN_ERR_INTERNAL;
1151 }
1152
Willy Tarreauff605db2013-12-20 11:09:51 +01001153 if (!conn_xprt_ready(srv_conn)) {
1154 /* the target was only on the session, assign it to the SI now */
1155 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001156
Willy Tarreauff605db2013-12-20 11:09:51 +01001157 /* set the correct protocol on the output stream interface */
1158 if (objt_server(s->target)) {
1159 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1160 }
1161 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1162 /* proxies exclusively run on raw_sock right now */
1163 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1164 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1165 return SN_ERR_INTERNAL;
1166 }
1167 else
1168 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001169
Willy Tarreauff605db2013-12-20 11:09:51 +01001170 /* process the case where the server requires the PROXY protocol to be sent */
1171 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001172 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001173 srv_conn->send_proxy_ofs = 1; /* must compute size */
1174 cli_conn = objt_conn(s->req->prod->end);
1175 if (cli_conn)
1176 conn_get_to_addr(cli_conn);
1177 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001178
Willy Tarreauff605db2013-12-20 11:09:51 +01001179 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001180
Willy Tarreauff605db2013-12-20 11:09:51 +01001181 assign_tproxy_address(s);
1182 }
1183 else {
1184 /* the connection is being reused, just re-attach it */
1185 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001186 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001187 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001188
William Lallemandb7ff6a32012-03-02 14:35:21 +01001189 /* flag for logging source ip/port */
1190 if (s->fe->options2 & PR_O2_SRC_ADDR)
1191 s->req->cons->flags |= SI_FL_SRC_ADDR;
1192
Willy Tarreau14f8e862012-08-30 22:23:13 +02001193 /* disable lingering */
1194 if (s->be->options & PR_O_TCP_NOLING)
1195 s->req->cons->flags |= SI_FL_NOLINGER;
1196
Willy Tarreau73b013b2012-05-21 16:31:45 +02001197 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198
Willy Tarreau9650f372009-08-16 14:02:45 +02001199 if (err != SN_ERR_NONE)
1200 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001201
Willy Tarreau14f8e862012-08-30 22:23:13 +02001202 /* set connect timeout */
1203 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1204
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001205 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001206 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001207 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001208 srv->cur_sess++;
1209 if (srv->cur_sess > srv->counters.cur_sess_max)
1210 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001211 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001212 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001213 }
1214
Willy Tarreaubaaee002006-06-26 02:48:02 +02001215 return SN_ERR_NONE; /* connection is OK */
1216}
1217
1218
Willy Tarreaubaaee002006-06-26 02:48:02 +02001219/* This function performs the "redispatch" part of a connection attempt. It
1220 * will assign a server if required, queue the connection if required, and
1221 * handle errors that might arise at this level. It can change the server
1222 * state. It will return 1 if it encounters an error, switches the server
1223 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1224 * that the connection is ready to use.
1225 */
1226
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001227int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228{
Willy Tarreau827aee92011-03-10 16:55:02 +01001229 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230 int conn_err;
1231
1232 /* We know that we don't have any connection pending, so we will
1233 * try to get a new one, and wait in this state if it's queued
1234 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001235 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001236 conn_err = assign_server_and_queue(s);
1237 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001238
Willy Tarreaubaaee002006-06-26 02:48:02 +02001239 switch (conn_err) {
1240 case SRV_STATUS_OK:
1241 break;
1242
Willy Tarreau7c669d72008-06-20 15:04:11 +02001243 case SRV_STATUS_FULL:
1244 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1245 * and we can redispatch to another server, or it is not and we return
1246 * 503. This only makes sense in DIRECT mode however, because normal LB
1247 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001248 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001249 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001250 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001251 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1252 (s->be->options & PR_O_REDISP)) {
1253 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001254 goto redispatch;
1255 }
1256
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001257 if (!s->req->cons->err_type) {
1258 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001259 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001260
Willy Tarreau827aee92011-03-10 16:55:02 +01001261 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001262 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001263 return 1;
1264
Willy Tarreaubaaee002006-06-26 02:48:02 +02001265 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001266 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001267 if (!s->req->cons->err_type) {
1268 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001269 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001270
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001271 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272 return 1;
1273
1274 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001275 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1276 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277 /* do nothing else and do not wake any other session up */
1278 return 1;
1279
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280 case SRV_STATUS_INTERNAL:
1281 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001282 if (!s->req->cons->err_type) {
1283 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001284 }
1285
Willy Tarreau827aee92011-03-10 16:55:02 +01001286 if (srv)
1287 srv_inc_sess_ctr(srv);
1288 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001289 srv_set_sess_last(srv);
1290 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001291 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001292 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293
1294 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001295 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001296 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297 return 1;
1298 }
1299 /* if we get here, it's because we got SRV_STATUS_OK, which also
1300 * means that the connection has not been queued.
1301 */
1302 return 0;
1303}
1304
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001305/* sends a log message when a backend goes down, and also sets last
1306 * change date.
1307 */
1308void set_backend_down(struct proxy *be)
1309{
1310 be->last_change = now.tv_sec;
1311 be->down_trans++;
1312
1313 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1314 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1315}
1316
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001317/* Apply RDP cookie persistence to the current session. For this, the function
1318 * tries to extract an RDP cookie from the request buffer, and look for the
1319 * matching server in the list. If the server is found, it is assigned to the
1320 * session. This always returns 1, and the analyser removes itself from the
1321 * list. Nothing is performed if a server was already assigned.
1322 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001323int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001324{
1325 struct proxy *px = s->be;
1326 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001327 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001328 struct server *srv = px->srv;
1329 struct sockaddr_in addr;
1330 char *p;
1331
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001332 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 +02001333 now_ms, __FUNCTION__,
1334 s,
1335 req,
1336 req->rex, req->wex,
1337 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001338 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001339 req->analysers);
1340
1341 if (s->flags & SN_ASSIGNED)
1342 goto no_cookie;
1343
Willy Tarreau37406352012-04-23 16:16:37 +02001344 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001345
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001346 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001347 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001348 goto no_cookie;
1349
1350 memset(&addr, 0, sizeof(addr));
1351 addr.sin_family = AF_INET;
1352
Willy Tarreau664092c2011-12-16 19:11:42 +01001353 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001354 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001355 if (*p != '.')
1356 goto no_cookie;
1357 p++;
1358 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1359 if (*p != '.')
1360 goto no_cookie;
1361
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001362 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001363 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001364 if (srv->addr.ss_family == AF_INET &&
1365 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001366 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001367 /* we found the server and it is usable */
1368 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001369 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001370 break;
1371 }
1372 }
1373 srv = srv->next;
1374 }
1375
1376no_cookie:
1377 req->analysers &= ~an_bit;
1378 req->analyse_exp = TICK_ETERNITY;
1379 return 1;
1380}
1381
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001382int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001383 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001384 return px->down_time;
1385
1386 return now.tv_sec - px->last_change + px->down_time;
1387}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001388
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001389/*
1390 * This function returns a string containing the balancing
1391 * mode of the proxy in a format suitable for stats.
1392 */
1393
1394const char *backend_lb_algo_str(int algo) {
1395
1396 if (algo == BE_LB_ALGO_RR)
1397 return "roundrobin";
1398 else if (algo == BE_LB_ALGO_SRR)
1399 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001400 else if (algo == BE_LB_ALGO_FAS)
1401 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001402 else if (algo == BE_LB_ALGO_LC)
1403 return "leastconn";
1404 else if (algo == BE_LB_ALGO_SH)
1405 return "source";
1406 else if (algo == BE_LB_ALGO_UH)
1407 return "uri";
1408 else if (algo == BE_LB_ALGO_PH)
1409 return "url_param";
1410 else if (algo == BE_LB_ALGO_HH)
1411 return "hdr";
1412 else if (algo == BE_LB_ALGO_RCH)
1413 return "rdp-cookie";
1414 else
1415 return NULL;
1416}
1417
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001418/* This function parses a "balance" statement in a backend section describing
1419 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001420 * returns -1, it will write an error message into the <err> buffer which will
1421 * automatically be allocated and must be passed as NULL. The trailing '\n'
1422 * will not be written. The function must be called with <args> pointing to the
1423 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001424 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001425int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001426{
1427 if (!*(args[0])) {
1428 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001429 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1430 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001431 return 0;
1432 }
1433
1434 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001435 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1436 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001437 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001438 else if (!strcmp(args[0], "static-rr")) {
1439 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1440 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1441 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001442 else if (!strcmp(args[0], "first")) {
1443 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1444 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1445 }
Willy Tarreau51406232008-03-10 22:04:20 +01001446 else if (!strcmp(args[0], "leastconn")) {
1447 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1448 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1449 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001450 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001451 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1452 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001453 }
1454 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001455 int arg = 1;
1456
Willy Tarreau31682232007-11-29 15:38:04 +01001457 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1458 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001459
Oskar Stolc8dc41842012-05-19 10:19:54 +01001460 curproxy->uri_whole = 0;
natalie.chen4fcfe812018-08-15 19:14:40 +08001461 curproxy->uri_gerrit = 0;
Oskar Stolc8dc41842012-05-19 10:19:54 +01001462
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001463 while (*args[arg]) {
1464 if (!strcmp(args[arg], "len")) {
1465 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001466 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001467 return -1;
1468 }
1469 curproxy->uri_len_limit = atoi(args[arg+1]);
1470 arg += 2;
1471 }
1472 else if (!strcmp(args[arg], "depth")) {
1473 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001474 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001475 return -1;
1476 }
1477 /* hint: we store the position of the ending '/' (depth+1) so
1478 * that we avoid a comparison while computing the hash.
1479 */
1480 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1481 arg += 2;
1482 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001483 else if (!strcmp(args[arg], "whole")) {
1484 curproxy->uri_whole = 1;
1485 arg += 1;
natalie.chen4fcfe812018-08-15 19:14:40 +08001486 }
1487 else if (!strcmp(args[arg], "gerrit")) {
1488 if (!*args[arg+1] || (atoi(args[arg+1]) < 0)) {
1489 memprintf(err, "%s : '%s' expects a non negative integer (got '%s').", args[0], args[arg], args[arg+1]);
1490 return -1;
1491 }
1492 curproxy->uri_gerrit = 1;
1493 // 0 means whole uri
1494 curproxy->uri_dirs_depth1 = atoi(args[arg+1]);
1495 arg += 2;
Oskar Stolc8dc41842012-05-19 10:19:54 +01001496 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001497 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001498 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001499 return -1;
1500 }
1501 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001502 }
Willy Tarreau01732802007-11-01 22:48:15 +01001503 else if (!strcmp(args[0], "url_param")) {
1504 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001505 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001506 return -1;
1507 }
Willy Tarreau31682232007-11-29 15:38:04 +01001508 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1509 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001510
1511 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001512 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001513 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001514 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001515 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001516 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001517 return -1;
1518 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001519 }
Benoitaffb4812009-03-25 13:02:10 +01001520 }
1521 else if (!strncmp(args[0], "hdr(", 4)) {
1522 const char *beg, *end;
1523
1524 beg = args[0] + 4;
1525 end = strchr(beg, ')');
1526
1527 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001528 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001529 return -1;
1530 }
1531
1532 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1533 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1534
1535 free(curproxy->hh_name);
1536 curproxy->hh_len = end - beg;
1537 curproxy->hh_name = my_strndup(beg, end - beg);
1538 curproxy->hh_match_domain = 0;
1539
1540 if (*args[1]) {
1541 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001542 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001543 return -1;
1544 }
1545 curproxy->hh_match_domain = 1;
1546 }
Emeric Brun736aa232009-06-30 17:56:00 +02001547 }
1548 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1549 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1550 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1551
1552 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1553 const char *beg, *end;
1554
1555 beg = args[0] + 11;
1556 end = strchr(beg, ')');
1557
1558 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001559 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001560 return -1;
1561 }
1562
1563 free(curproxy->hh_name);
1564 curproxy->hh_name = my_strndup(beg, end - beg);
1565 curproxy->hh_len = end - beg;
1566 }
1567 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1568 free(curproxy->hh_name);
1569 curproxy->hh_name = strdup("mstshash");
1570 curproxy->hh_len = strlen(curproxy->hh_name);
1571 }
1572 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001573 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001574 return -1;
1575 }
Willy Tarreau01732802007-11-01 22:48:15 +01001576 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001577 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001578 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 +01001579 return -1;
1580 }
1581 return 0;
1582}
1583
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001584
1585/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001586/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001587/************************************************************************/
1588
Willy Tarreau34db1082012-04-19 17:16:54 +02001589/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001590 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001591 * undefined behaviour.
1592 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001593static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001594smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001595 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001596{
Willy Tarreau37406352012-04-23 16:16:37 +02001597 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001598 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001599 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001600
1601 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001602 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001603 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001604 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001605 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001606 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001607
1608 return 1;
1609}
1610
Willy Tarreau37406352012-04-23 16:16:37 +02001611/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001612 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001613 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1614 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001615 */
1616static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001617smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001618 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001619{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001620 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001621
Willy Tarreau37406352012-04-23 16:16:37 +02001622 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001623 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001624 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001625 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001626 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001627 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001628 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001629 return 1;
1630}
1631
Willy Tarreau34db1082012-04-19 17:16:54 +02001632/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001633 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001634 * undefined behaviour.
1635 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001636static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001637smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001638 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001639{
1640 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001641
Willy Tarreau37406352012-04-23 16:16:37 +02001642 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001643 smp->type = SMP_T_UINT;
1644 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001645
Willy Tarreau24e32d82012-04-23 23:55:44 +02001646 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001647 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001648 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001649
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001650 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001651 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001652 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001653 return 1;
1654 }
1655
Willy Tarreauf853c462012-04-23 18:53:56 +02001656 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001657 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001658 }
1659
1660 return 1;
1661}
1662
Willy Tarreaua5e37562011-12-16 17:06:15 +01001663/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001664static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001665smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001666 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001667{
Willy Tarreauf853c462012-04-23 18:53:56 +02001668 smp->flags = SMP_F_VOL_TXN;
1669 smp->type = SMP_T_UINT;
1670 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001671 return 1;
1672}
1673
Willy Tarreaua5e37562011-12-16 17:06:15 +01001674/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001675static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001676smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001677 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001678{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001679 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001680 return 0;
1681
Willy Tarreauf853c462012-04-23 18:53:56 +02001682 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001683 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001684
1685 return 1;
1686}
1687
Willy Tarreau34db1082012-04-19 17:16:54 +02001688/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001689 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001690 * undefined behaviour.
1691 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001692static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001693smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001694 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001695{
Willy Tarreau37406352012-04-23 16:16:37 +02001696 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001697 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001698 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001699 return 1;
1700}
1701
Willy Tarreau34db1082012-04-19 17:16:54 +02001702/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001703 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001704 * undefined behaviour.
1705 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001706static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001707smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001708 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001709{
Willy Tarreau37406352012-04-23 16:16:37 +02001710 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001711 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001712 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001713 return 1;
1714}
1715
Willy Tarreau34db1082012-04-19 17:16:54 +02001716/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001717 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001718 * undefined behaviour.
1719 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001720static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001721smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001722 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001723{
Willy Tarreau37406352012-04-23 16:16:37 +02001724 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001725 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001726 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001727 return 1;
1728}
1729
Willy Tarreaua5e37562011-12-16 17:06:15 +01001730/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001731 * by the number of running servers and rounded up. If there is no running
1732 * server, we return twice the total, just as if we had half a running server.
1733 * This is more or less correct anyway, since we expect the last server to come
1734 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001735 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001736 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001737 */
1738static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001739smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001740 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001741{
1742 int nbsrv;
1743
Willy Tarreau37406352012-04-23 16:16:37 +02001744 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001745 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001746 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001747
1748 if (px->srv_act)
1749 nbsrv = px->srv_act;
1750 else if (px->lbprm.fbck)
1751 nbsrv = 1;
1752 else
1753 nbsrv = px->srv_bck;
1754
1755 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001756 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001757 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001758 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001759
1760 return 1;
1761}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001762
Willy Tarreau34db1082012-04-19 17:16:54 +02001763/* set temp integer to the number of concurrent connections on the server in the backend.
1764 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1765 * undefined behaviour.
1766 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001767static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001768smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001769 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001770{
Willy Tarreauf853c462012-04-23 18:53:56 +02001771 smp->flags = SMP_F_VOL_TEST;
1772 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001773 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001774 return 1;
1775}
1776
Tait Clarridge7896d522012-12-05 21:39:31 -05001777/* set temp integer to the number of enabled servers on the proxy.
1778 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1779 * undefined behaviour.
1780 */
1781static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001782smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001783 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001784{
1785 smp->flags = SMP_F_VOL_TEST;
1786 smp->type = SMP_T_UINT;
1787 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1788 return 1;
1789}
1790
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001791
1792/* Note: must not be declared <const> as its list will be overwritten.
1793 * Please take care of keeping this list alphabetically sorted.
1794 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001795static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001796 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1797 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1798 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1799 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1800 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1801 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1802 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1803 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1804 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1805 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1806 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1807 { /* END */ },
1808}};
1809
1810
Willy Tarreau61612d42012-04-19 18:42:05 +02001811/* Note: must not be declared <const> as its list will be overwritten.
1812 * Please take care of keeping this list alphabetically sorted.
1813 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001814static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001815 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001816}};
1817
1818
1819__attribute__((constructor))
1820static void __backend_init(void)
1821{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001822 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001823 acl_register_keywords(&acl_kws);
1824}
1825
Willy Tarreaubaaee002006-06-26 02:48:02 +02001826/*
1827 * Local variables:
1828 * c-indent-level: 8
1829 * c-basic-offset: 8
1830 * End:
1831 */