blob: bf4a81ddc47e8a0c375cfdde26374bfb2faf87f8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau1a7eca12013-01-07 22:38:03 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <syslog.h>
Willy Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +020019#include <ctype.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040020#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreauc7e42382012-08-24 19:22:53 +020022#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020025#include <common/debug.h>
Bhaskar98634f02013-10-29 23:30:51 -040026#include <common/hash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010032#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020033#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020037#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010038#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020039#include <proto/lb_fwlc.h>
40#include <proto/lb_fwrr.h>
41#include <proto/lb_map.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010042#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010043#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020046#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010048#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020050#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020051#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010052#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/task.h>
54
Bhaskar98634f02013-10-29 23:30:51 -040055/* helper function to invoke the correct hash method */
56static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len)
57{
58 unsigned long hash;
59
60 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
61 case BE_LB_HFCN_DJB2:
62 hash = hash_djb2(key, len);
63 break;
64 case BE_LB_HFCN_SDBM:
65 /* this is the default hash function */
66 default:
67 hash = hash_sdbm(key, len);
68 break;
69 }
70
71 return hash;
72}
73
Willy Tarreaubaaee002006-06-26 02:48:02 +020074/*
75 * This function recounts the number of usable active and backup servers for
76 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010077 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010078 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010079 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020081void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082{
83 struct server *srv;
84
Willy Tarreau20697042007-11-15 23:26:18 +010085 px->srv_act = px->srv_bck = 0;
86 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010087 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010089 if (!srv_is_usable(srv->state, srv->eweight))
90 continue;
91
92 if (srv->state & SRV_BACKUP) {
93 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010094 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010095 px->lbprm.fbck = srv;
96 px->srv_bck++;
97 px->lbprm.tot_wbck += srv->eweight;
98 } else {
99 px->srv_act++;
100 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 }
102 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100103}
Willy Tarreau20697042007-11-15 23:26:18 +0100104
Willy Tarreaub625a082007-11-26 01:15:43 +0100105/* This function simply updates the backend's tot_weight and tot_used values
106 * after servers weights have been updated. It is designed to be used after
107 * recount_servers() or equivalent.
108 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200109void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100110{
Willy Tarreau20697042007-11-15 23:26:18 +0100111 if (px->srv_act) {
112 px->lbprm.tot_weight = px->lbprm.tot_wact;
113 px->lbprm.tot_used = px->srv_act;
114 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100115 else if (px->lbprm.fbck) {
116 /* use only the first backup server */
117 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
118 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100119 }
120 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100121 px->lbprm.tot_weight = px->lbprm.tot_wbck;
122 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100123 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100124}
125
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200126/*
127 * This function tries to find a running server for the proxy <px> following
128 * the source hash method. Depending on the number of active/backup servers,
129 * it will either look for active servers, or for backup servers.
130 * If any server is found, it will be returned. If no valid server is found,
131 * NULL is returned.
132 */
133struct server *get_server_sh(struct proxy *px, const char *addr, int len)
134{
135 unsigned int h, l;
136
137 if (px->lbprm.tot_weight == 0)
138 return NULL;
139
140 l = h = 0;
141
142 /* note: we won't hash if there's only one server left */
143 if (px->lbprm.tot_used == 1)
144 goto hash_done;
145
146 while ((l + sizeof (int)) <= len) {
147 h ^= ntohl(*(unsigned int *)(&addr[l]));
148 l += sizeof (int);
149 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100150 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
151 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200152 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200153 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
154 return chash_get_server_hash(px, h);
155 else
156 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200157}
158
159/*
160 * This function tries to find a running server for the proxy <px> following
161 * the URI hash method. In order to optimize cache hits, the hash computation
162 * ends at the question mark. Depending on the number of active/backup servers,
163 * it will either look for active servers, or for backup servers.
164 * If any server is found, it will be returned. If no valid server is found,
165 * NULL is returned.
166 *
167 * This code was contributed by Guillaume Dallaire, who also selected this hash
168 * algorithm out of a tens because it gave him the best results.
169 *
170 */
171struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
172{
173 unsigned long hash = 0;
174 int c;
175 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400176 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200177
178 if (px->lbprm.tot_weight == 0)
179 return NULL;
180
181 /* note: we won't hash if there's only one server left */
182 if (px->lbprm.tot_used == 1)
183 goto hash_done;
184
185 if (px->uri_len_limit)
186 uri_len = MIN(uri_len, px->uri_len_limit);
187
Bhaskar98634f02013-10-29 23:30:51 -0400188 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200189 while (uri_len--) {
Bhaskar98634f02013-10-29 23:30:51 -0400190 c = *end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200191 if (c == '/') {
192 slashes++;
193 if (slashes == px->uri_dirs_depth1) /* depth+1 */
194 break;
195 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100196 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200197 break;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200198 }
Bhaskar98634f02013-10-29 23:30:51 -0400199
200 hash = gen_hash(px, start, (end - start));
201
Willy Tarreau798a39c2010-11-24 15:04:29 +0100202 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
203 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200204 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200205 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
206 return chash_get_server_hash(px, hash);
207 else
208 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200209}
210
Willy Tarreau01732802007-11-01 22:48:15 +0100211/*
212 * This function tries to find a running server for the proxy <px> following
213 * the URL parameter hash method. It looks for a specific parameter in the
214 * URL and hashes it to compute the server ID. This is useful to optimize
215 * performance by avoiding bounces between servers in contexts where sessions
216 * are shared but cookies are not usable. If the parameter is not found, NULL
217 * is returned. If any server is found, it will be returned. If no valid server
218 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100219 */
220struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
221{
222 unsigned long hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400223 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200224 const char *p;
225 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100226 int plen;
227
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200228 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100229 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100230 return NULL;
231
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200232 if ((p = memchr(uri, '?', uri_len)) == NULL)
233 return NULL;
234
Willy Tarreau01732802007-11-01 22:48:15 +0100235 p++;
236
237 uri_len -= (p - uri);
238 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200239 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100240
241 while (uri_len > plen) {
242 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200243 if (params[plen] == '=') {
244 if (memcmp(params, px->url_param_name, plen) == 0) {
245 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100246 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200247 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100248 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200249 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400250 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200251 uri_len -= plen + 1;
252
Bhaskar98634f02013-10-29 23:30:51 -0400253 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100254 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400255 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100256 }
Bhaskar98634f02013-10-29 23:30:51 -0400257 hash = gen_hash(px, start, (end - start));
258
Willy Tarreau798a39c2010-11-24 15:04:29 +0100259 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
260 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400261
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200262 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
263 return chash_get_server_hash(px, hash);
264 else
265 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100266 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267 }
268 /* skip to next parameter */
269 p = memchr(params, '&', uri_len);
270 if (!p)
271 return NULL;
272 p++;
273 uri_len -= (p - params);
274 params = p;
275 }
276 return NULL;
277}
278
279/*
280 * this does the same as the previous server_ph, but check the body contents
281 */
282struct server *get_server_ph_post(struct session *s)
283{
284 unsigned long hash = 0;
285 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200286 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 struct http_msg *msg = &txn->req;
288 struct proxy *px = s->be;
289 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100290 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200291 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100292 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400293 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200294
Willy Tarreau9b28e032012-10-12 23:49:43 +0200295 if (len > buffer_len(req->buf) - msg->sov)
296 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200297
Willy Tarreau157dd632009-12-06 19:18:09 +0100298 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200299 return NULL;
300
Willy Tarreau157dd632009-12-06 19:18:09 +0100301 if (px->lbprm.tot_weight == 0)
302 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200303
304 while (len > plen) {
305 /* Look for the parameter name followed by an equal symbol */
306 if (params[plen] == '=') {
307 if (memcmp(params, px->url_param_name, plen) == 0) {
308 /* OK, we have the parameter here at <params>, and
309 * the value after the equal sign, at <p>
310 * skip the equal symbol
311 */
312 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400313 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200314 len -= plen + 1;
315
Bhaskar98634f02013-10-29 23:30:51 -0400316 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100318 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400319 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100320 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200321 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
322 break;
323 return NULL; /* oh, no; this is not uri-encoded.
324 * This body does not contain parameters.
325 */
326 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400328 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200329 /* should we break if vlen exceeds limit? */
330 }
Bhaskar98634f02013-10-29 23:30:51 -0400331 hash = gen_hash(px, start, (end - start));
332
Willy Tarreau798a39c2010-11-24 15:04:29 +0100333 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
334 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400335
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200336 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
337 return chash_get_server_hash(px, hash);
338 else
339 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200340 }
341 }
Willy Tarreau01732802007-11-01 22:48:15 +0100342 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200343 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100344 if (!p)
345 return NULL;
346 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200347 len -= (p - params);
348 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100349 }
350 return NULL;
351}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200353
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354/*
Benoitaffb4812009-03-25 13:02:10 +0100355 * This function tries to find a running server for the proxy <px> following
356 * the Header parameter hash method. It looks for a specific parameter in the
357 * URL and hashes it to compute the server ID. This is useful to optimize
358 * performance by avoiding bounces between servers in contexts where sessions
359 * are shared but cookies are not usable. If the parameter is not found, NULL
360 * is returned. If any server is found, it will be returned. If no valid server
361 * is found, NULL is returned.
362 */
363struct server *get_server_hh(struct session *s)
364{
365 unsigned long hash = 0;
366 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100367 struct proxy *px = s->be;
368 unsigned int plen = px->hh_len;
369 unsigned long len;
370 struct hdr_ctx ctx;
371 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400372 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100373
374 /* tot_weight appears to mean srv_count */
375 if (px->lbprm.tot_weight == 0)
376 return NULL;
377
Benoitaffb4812009-03-25 13:02:10 +0100378 ctx.idx = 0;
379
380 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200381 http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, (int)-s->req->buf->o), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100382
383 /* if the header is not found or empty, let's fallback to round robin */
384 if (!ctx.idx || !ctx.vlen)
385 return NULL;
386
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200387 /* note: we won't hash if there's only one server left */
388 if (px->lbprm.tot_used == 1)
389 goto hash_done;
390
Benoitaffb4812009-03-25 13:02:10 +0100391 /* Found a the hh_name in the headers.
392 * we will compute the hash based on this value ctx.val.
393 */
394 len = ctx.vlen;
395 p = (char *)ctx.line + ctx.val;
396 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400397 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100398 } else {
399 int dohash = 0;
400 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400401 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100402 /* special computation, use only main domain name, not tld/host
403 * going back from the end of string, start hashing at first
404 * dot stop at next.
405 * This is designed to work with the 'Host' header, and requires
406 * a special option to activate this.
407 */
408 while (len) {
409 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400410 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100411 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400412 start = end = p - 1;
413 }
Benoitaffb4812009-03-25 13:02:10 +0100414 else
415 break;
416 } else {
417 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400418 start--;
Benoitaffb4812009-03-25 13:02:10 +0100419 }
420 len--;
421 p--;
422 }
Bhaskar98634f02013-10-29 23:30:51 -0400423 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100424 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100425 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
426 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200427 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200428 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
429 return chash_get_server_hash(px, hash);
430 else
431 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100432}
433
Willy Tarreau34db1082012-04-19 17:16:54 +0200434/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200435struct server *get_server_rch(struct session *s)
436{
437 unsigned long hash = 0;
438 struct proxy *px = s->be;
439 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200440 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200441 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200442 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200443
444 /* tot_weight appears to mean srv_count */
445 if (px->lbprm.tot_weight == 0)
446 return NULL;
447
Willy Tarreau37406352012-04-23 16:16:37 +0200448 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200449
Willy Tarreau9b28e032012-10-12 23:49:43 +0200450 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200451
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200452 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200453 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100454
Willy Tarreau9b28e032012-10-12 23:49:43 +0200455 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200456
Willy Tarreau37406352012-04-23 16:16:37 +0200457 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200458 return NULL;
459
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200460 /* note: we won't hash if there's only one server left */
461 if (px->lbprm.tot_used == 1)
462 goto hash_done;
463
Emeric Brun736aa232009-06-30 17:56:00 +0200464 /* Found a the hh_name in the headers.
465 * we will compute the hash based on this value ctx.val.
466 */
Bhaskar98634f02013-10-29 23:30:51 -0400467 hash = gen_hash(px, smp.data.str.str, len);
468
Willy Tarreau798a39c2010-11-24 15:04:29 +0100469 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
470 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200471 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200472 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
473 return chash_get_server_hash(px, hash);
474 else
475 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200476}
Benoitaffb4812009-03-25 13:02:10 +0100477
478/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200479 * This function applies the load-balancing algorithm to the session, as
480 * defined by the backend it is assigned to. The session is then marked as
481 * 'assigned'.
482 *
483 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
484 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100485 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200486 * The function tries to keep the original connection slot if it reconnects to
487 * the same server, otherwise it releases it and tries to offer it.
488 *
489 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200490 *
491 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100492 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200493 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
494 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495 * SRV_STATUS_INTERNAL for other unrecoverable errors.
496 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100498 * it does not need to be called anymore. This means that target_srv(&s->target)
499 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200500 *
501 */
502
503int assign_server(struct session *s)
504{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100505
Willy Tarreau7c669d72008-06-20 15:04:11 +0200506 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100507 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200508 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100509
Simon Horman8effd3d2011-08-13 08:03:52 +0900510 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511
Willy Tarreau7c669d72008-06-20 15:04:11 +0200512 err = SRV_STATUS_INTERNAL;
513 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
514 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100515
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100516 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518
Willy Tarreau7c669d72008-06-20 15:04:11 +0200519 /* We have to release any connection slot before applying any LB algo,
520 * otherwise we may erroneously end up with no available slot.
521 */
522 if (conn_slot)
523 sess_change_server(s, NULL);
524
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100525 /* We will now try to find the good server and store it into <objt_server(s->target)>.
526 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200527 * as well as if no server is available (check error code).
528 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100529
Willy Tarreau827aee92011-03-10 16:55:02 +0100530 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100531 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100532
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200533 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 /* we must check if we have at least one server available */
535 if (!s->be->lbprm.tot_weight) {
536 err = SRV_STATUS_NOSRV;
537 goto out;
538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 /* First check whether we need to fetch some data or simply call
541 * the LB lookup function. Only the hashing functions will need
542 * some input data in fact, and will support multiple algorithms.
543 */
544 switch (s->be->lbprm.algo & BE_LB_LKUP) {
545 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100546 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200547 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200548
Willy Tarreauf09c6602012-02-13 17:12:08 +0100549 case BE_LB_LKUP_FSTREE:
550 srv = fas_get_next_server(s->be, prev_srv);
551 break;
552
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200553 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100554 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200555 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200556
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200557 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200558 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200559 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200560 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100561 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200562 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100563 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200564 break;
565 }
566 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200567 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200568 err = SRV_STATUS_INTERNAL;
569 goto out;
570 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571
572 switch (s->be->lbprm.algo & BE_LB_PARM) {
573 case BE_LB_HASH_SRC:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200574 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200575 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200576 (void *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200577 4);
578 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200579 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200580 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200581 (void *)&((struct sockaddr_in6 *)&s->req->prod->conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200582 16);
583 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200584 else {
585 /* unknown IP family */
586 err = SRV_STATUS_INTERNAL;
587 goto out;
588 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200589 break;
590
591 case BE_LB_HASH_URI:
592 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100593 if (s->txn.req.msg_state < HTTP_MSG_BODY)
594 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100595 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200596 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100597 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200598 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200599
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600 case BE_LB_HASH_PRM:
601 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100602 if (s->txn.req.msg_state < HTTP_MSG_BODY)
603 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100604
Willy Tarreau827aee92011-03-10 16:55:02 +0100605 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200606 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100608
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 if (!srv && s->txn.meth == HTTP_METH_POST)
610 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200611 break;
Benoitaffb4812009-03-25 13:02:10 +0100612
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613 case BE_LB_HASH_HDR:
614 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100615 if (s->txn.req.msg_state < HTTP_MSG_BODY)
616 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100617 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200618 break;
619
620 case BE_LB_HASH_RDP:
621 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100622 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200623 break;
624
625 default:
626 /* unknown balancing algorithm */
627 err = SRV_STATUS_INTERNAL;
628 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100629 }
Emeric Brun736aa232009-06-30 17:56:00 +0200630
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200631 /* If the hashing parameter was not found, let's fall
632 * back to round robin on the map.
633 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100634 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200635 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100636 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200637 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100638 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200639 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200640
641 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200642 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200643
Willy Tarreau7c669d72008-06-20 15:04:11 +0200644 default:
645 /* unknown balancing algorithm */
646 err = SRV_STATUS_INTERNAL;
647 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200649
Willy Tarreau827aee92011-03-10 16:55:02 +0100650 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200651 err = SRV_STATUS_FULL;
652 goto out;
653 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100655 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100657 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100658 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200659 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200660 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100661 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100663 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200664 is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100665 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100666 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100667 }
668 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200669 err = SRV_STATUS_NOSRV;
670 goto out;
671 }
672
673 s->flags |= SN_ASSIGNED;
674 err = SRV_STATUS_OK;
675 out:
676
677 /* Either we take back our connection slot, or we offer it to someone
678 * else if we don't need it anymore.
679 */
680 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100681 if (conn_slot == srv) {
682 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200683 } else {
684 if (may_dequeue_tasks(conn_slot, s->be))
685 process_srv_queue(conn_slot);
686 }
687 }
688
689 out_err:
690 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691}
692
693
694/*
695 * This function assigns a server address to a session, and sets SN_ADDR_SET.
696 * The address is taken from the currently assigned server, or from the
697 * dispatch or transparent address.
698 *
699 * It may return :
700 * SRV_STATUS_OK if everything is OK.
701 * SRV_STATUS_INTERNAL for other unrecoverable errors.
702 *
703 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
704 * not cleared, so it's to the caller to clear it if required.
705 *
706 */
707int assign_server_address(struct session *s)
708{
709#ifdef DEBUG_FULL
710 fprintf(stderr,"assign_server_address : s=%p\n",s);
711#endif
712
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200713 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 /* A server is necessarily known for this session */
715 if (!(s->flags & SN_ASSIGNED))
716 return SRV_STATUS_INTERNAL;
717
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100718 s->req->cons->conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200720 if (!is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200721 /* if the server has no address, we use the same address
722 * the client asked, which is handy for remapping ports
723 * locally on multiple addresses at once.
724 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200725 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200726 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200727
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200728 if (s->req->prod->conn->addr.to.ss_family == AF_INET) {
729 ((struct sockaddr_in *)&s->req->cons->conn->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
730 } else if (s->req->prod->conn->addr.to.ss_family == AF_INET6) {
731 ((struct sockaddr_in6 *)&s->req->cons->conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&s->req->prod->conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200732 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200733 }
734
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 /* if this server remaps proxied ports, we'll use
736 * the port the client connected to with an offset. */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100737 if (objt_server(s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100738 int base_port;
739
Willy Tarreau9b061e32012-04-07 18:03:52 +0200740 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200741 conn_get_to_addr(s->req->prod->conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100742
743 /* First, retrieve the port from the incoming connection */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200744 base_port = get_host_port(&s->req->prod->conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100745
746 /* Second, assign the outgoing connection's port */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200747 base_port += get_host_port(&s->req->cons->conn->addr.to);
748 set_host_port(&s->req->cons->conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749 }
750 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200751 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 /* connect to the defined dispatch addr */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200753 s->req->cons->conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100755 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200757 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200759 if (s->req->prod->conn->addr.to.ss_family == AF_INET || s->req->prod->conn->addr.to.ss_family == AF_INET6) {
760 memcpy(&s->req->cons->conn->addr.to, &s->req->prod->conn->addr.to, MIN(sizeof(s->req->cons->conn->addr.to), sizeof(s->req->prod->conn->addr.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200761 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100762 /* when we support IPv6 on the backend, we may add other tests */
763 //qfprintf(stderr, "Cannot get original server address.\n");
764 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100766 else if (s->be->options & PR_O_HTTP_PROXY) {
767 /* If HTTP PROXY option is set, then server is already assigned
768 * during incoming client request parsing. */
769 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100770 else {
771 /* no server and no LB algorithm ! */
772 return SRV_STATUS_INTERNAL;
773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774
775 s->flags |= SN_ADDR_SET;
776 return SRV_STATUS_OK;
777}
778
779
780/* This function assigns a server to session <s> if required, and can add the
781 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200782 * If ->srv_conn is set, the session is first released from the server.
783 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
784 * be called before any connection and after any retry or redispatch occurs.
785 *
786 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 *
788 * Returns :
789 *
790 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100791 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 * SRV_STATUS_QUEUED if the connection has been queued.
793 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100794 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200795 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 * SRV_STATUS_INTERNAL for other unrecoverable errors.
797 *
798 */
799int assign_server_and_queue(struct session *s)
800{
801 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 int err;
804
805 if (s->pend_pos)
806 return SRV_STATUS_INTERNAL;
807
Willy Tarreau7c669d72008-06-20 15:04:11 +0200808 err = SRV_STATUS_OK;
809 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100810 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100811
Willy Tarreau7c669d72008-06-20 15:04:11 +0200812 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100813 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814 /* This session was previously assigned to a server. We have to
815 * update the session's and the server's stats :
816 * - if the server changed :
817 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
818 * - set SN_REDISP if it was successfully redispatched
819 * - increment srv->redispatches and be->redispatches
820 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100821 */
822
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100823 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200824 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
825 s->txn.flags &= ~TX_CK_MASK;
826 s->txn.flags |= TX_CK_DOWN;
827 }
828 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100829 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100830 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100832 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100833 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100834 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100835 }
836 }
837
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 switch (err) {
839 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200840 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100841 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200843 return SRV_STATUS_OK; /* dispatch or proxy mode */
844
845 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847 return SRV_STATUS_OK;
848
849 /* OK, this session already has an assigned server, but no
850 * connection slot yet. Either it is a redispatch, or it was
851 * assigned from persistence information (direct mode).
852 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100853 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200854 /* server scheduled for redirection, and already assigned. We
855 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100856 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100857 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100858 return SRV_STATUS_OK;
859 }
860
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 /* We might have to queue this session if the assigned server is full.
862 * We know we have to queue it into the server's queue, so if a maxqueue
863 * is set on the server, we must also check that the server's queue is
864 * not full, in which case we have to return FULL.
865 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100866 if (srv->maxconn &&
867 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200868
Willy Tarreau827aee92011-03-10 16:55:02 +0100869 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200870 return SRV_STATUS_FULL;
871
Willy Tarreaubaaee002006-06-26 02:48:02 +0200872 p = pendconn_add(s);
873 if (p)
874 return SRV_STATUS_QUEUED;
875 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200876 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200878
879 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100880 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 return SRV_STATUS_OK;
882
883 case SRV_STATUS_FULL:
884 /* queue this session into the proxy's queue */
885 p = pendconn_add(s);
886 if (p)
887 return SRV_STATUS_QUEUED;
888 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200889 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890
891 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200892 return err;
893
Willy Tarreaubaaee002006-06-26 02:48:02 +0200894 case SRV_STATUS_INTERNAL:
895 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200896
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 default:
898 return SRV_STATUS_INTERNAL;
899 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100900}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901
Willy Tarreaub1d67742010-03-29 19:36:59 +0200902/* If an explicit source binding is specified on the server and/or backend, and
903 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200904 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200905 */
906static void assign_tproxy_address(struct session *s)
907{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200908#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100909 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100910 struct conn_src *src;
Willy Tarreau827aee92011-03-10 16:55:02 +0100911
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100912 if (srv && srv->conn_src.opts & CO_SRC_BIND)
913 src = &srv->conn_src;
914 else if (s->be->conn_src.opts & CO_SRC_BIND)
915 src = &s->be->conn_src;
916 else
917 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100918
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100919 switch (src->opts & CO_SRC_TPROXY_MASK) {
920 case CO_SRC_TPROXY_ADDR:
921 s->req->cons->conn->addr.from = src->tproxy_addr;
922 break;
923 case CO_SRC_TPROXY_CLI:
924 case CO_SRC_TPROXY_CIP:
925 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
926 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
927 break;
928 case CO_SRC_TPROXY_DYN:
929 if (src->bind_hdr_occ) {
930 char *vptr;
931 int vlen;
932 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100933
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100934 /* bind to the IP in a header */
935 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
936 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
937 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100938
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100939 b_rew(s->req->buf, rewind = s->req->buf->o);
940 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
941 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
942 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
943 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200944 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100945 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200946 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100947 break;
948 default:
949 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200950 }
951#endif
952}
953
954
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955/*
956 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200957 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100958 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959 * It can return one of :
960 * - SN_ERR_NONE if everything's OK
961 * - SN_ERR_SRVTO if there are no more servers
962 * - SN_ERR_SRVCL if the connection was refused by the server
963 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
964 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
965 * - SN_ERR_INTERNAL for any other purely internal errors
966 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
967 */
968int connect_server(struct session *s)
969{
Willy Tarreau827aee92011-03-10 16:55:02 +0100970 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200971 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972
973 if (!(s->flags & SN_ADDR_SET)) {
974 err = assign_server_address(s);
975 if (err != SRV_STATUS_OK)
976 return SN_ERR_INTERNAL;
977 }
978
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200979 /* the target was only on the session, assign it to the SI now */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100980 s->req->cons->conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200981
Willy Tarreau26d8c592012-05-07 18:12:14 +0200982 /* set the correct protocol on the output stream interface */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100983 if (objt_server(s->target)) {
984 si_prepare_conn(s->req->cons, objt_server(s->target)->proto, objt_server(s->target)->xprt);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200985 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100986 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200987 /* proxies exclusively run on raw_sock right now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200988 si_prepare_conn(s->req->cons, protocol_by_family(s->req->cons->conn->addr.to.ss_family), &raw_sock);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200989 if (!si_ctrl(s->req->cons))
Willy Tarreau26d8c592012-05-07 18:12:14 +0200990 return SN_ERR_INTERNAL;
991 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200992 else
993 return SN_ERR_INTERNAL; /* how did we get there ? */
994
995 /* process the case where the server requires the PROXY protocol to be sent */
996 s->req->cons->send_proxy_ofs = 0;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100997 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200998 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200999 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud02394b2012-05-11 18:32:18 +02001000 }
Willy Tarreau26d8c592012-05-07 18:12:14 +02001001
Willy Tarreaub1d67742010-03-29 19:36:59 +02001002 assign_tproxy_address(s);
1003
William Lallemandb7ff6a32012-03-02 14:35:21 +01001004 /* flag for logging source ip/port */
1005 if (s->fe->options2 & PR_O2_SRC_ADDR)
1006 s->req->cons->flags |= SI_FL_SRC_ADDR;
1007
Willy Tarreau14f8e862012-08-30 22:23:13 +02001008 /* disable lingering */
1009 if (s->be->options & PR_O_TCP_NOLING)
1010 s->req->cons->flags |= SI_FL_NOLINGER;
1011
Willy Tarreau73b013b2012-05-21 16:31:45 +02001012 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013
Willy Tarreau9650f372009-08-16 14:02:45 +02001014 if (err != SN_ERR_NONE)
1015 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001016
Willy Tarreau14f8e862012-08-30 22:23:13 +02001017 /* set connect timeout */
1018 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1019
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001020 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001021 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001022 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001023 srv->cur_sess++;
1024 if (srv->cur_sess > srv->counters.cur_sess_max)
1025 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001026 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001027 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 }
1029
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030 return SN_ERR_NONE; /* connection is OK */
1031}
1032
1033
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034/* This function performs the "redispatch" part of a connection attempt. It
1035 * will assign a server if required, queue the connection if required, and
1036 * handle errors that might arise at this level. It can change the server
1037 * state. It will return 1 if it encounters an error, switches the server
1038 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1039 * that the connection is ready to use.
1040 */
1041
1042int srv_redispatch_connect(struct session *t)
1043{
Willy Tarreau827aee92011-03-10 16:55:02 +01001044 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045 int conn_err;
1046
1047 /* We know that we don't have any connection pending, so we will
1048 * try to get a new one, and wait in this state if it's queued
1049 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001050 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001051 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001052 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001053
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 switch (conn_err) {
1055 case SRV_STATUS_OK:
1056 break;
1057
Willy Tarreau7c669d72008-06-20 15:04:11 +02001058 case SRV_STATUS_FULL:
1059 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1060 * and we can redispatch to another server, or it is not and we return
1061 * 503. This only makes sense in DIRECT mode however, because normal LB
1062 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001063 * would bring us on the same server again. Note that t->target is set
1064 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001065 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001066 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1067 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001068 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001069 goto redispatch;
1070 }
1071
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001072 if (!t->req->cons->err_type) {
1073 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001075 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001076
Willy Tarreau827aee92011-03-10 16:55:02 +01001077 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001078 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001079 return 1;
1080
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001082 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001083 if (!t->req->cons->err_type) {
1084 t->req->cons->err_type = SI_ET_CONN_ERR;
1085 t->req->cons->err_loc = NULL;
1086 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001087
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001088 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089 return 1;
1090
1091 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001092 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001093 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 /* do nothing else and do not wake any other session up */
1095 return 1;
1096
Willy Tarreaubaaee002006-06-26 02:48:02 +02001097 case SRV_STATUS_INTERNAL:
1098 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001099 if (!t->req->cons->err_type) {
1100 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001101 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001102 }
1103
Willy Tarreau827aee92011-03-10 16:55:02 +01001104 if (srv)
1105 srv_inc_sess_ctr(srv);
1106 if (srv)
1107 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001108 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109
1110 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001111 if (may_dequeue_tasks(srv, t->be))
1112 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 return 1;
1114 }
1115 /* if we get here, it's because we got SRV_STATUS_OK, which also
1116 * means that the connection has not been queued.
1117 */
1118 return 0;
1119}
1120
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001121/* Apply RDP cookie persistence to the current session. For this, the function
1122 * tries to extract an RDP cookie from the request buffer, and look for the
1123 * matching server in the list. If the server is found, it is assigned to the
1124 * session. This always returns 1, and the analyser removes itself from the
1125 * list. Nothing is performed if a server was already assigned.
1126 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001127int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001128{
1129 struct proxy *px = s->be;
1130 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001131 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001132 struct server *srv = px->srv;
1133 struct sockaddr_in addr;
1134 char *p;
1135
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001136 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 +02001137 now_ms, __FUNCTION__,
1138 s,
1139 req,
1140 req->rex, req->wex,
1141 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001142 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001143 req->analysers);
1144
1145 if (s->flags & SN_ASSIGNED)
1146 goto no_cookie;
1147
Willy Tarreau37406352012-04-23 16:16:37 +02001148 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001149
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001150 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001151 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001152 goto no_cookie;
1153
1154 memset(&addr, 0, sizeof(addr));
1155 addr.sin_family = AF_INET;
1156
Willy Tarreau664092c2011-12-16 19:11:42 +01001157 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001158 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001159 if (*p != '.')
1160 goto no_cookie;
1161 p++;
1162 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1163 if (*p != '.')
1164 goto no_cookie;
1165
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001166 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001167 while (srv) {
1168 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1169 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1170 /* we found the server and it is usable */
1171 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001172 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001173 break;
1174 }
1175 }
1176 srv = srv->next;
1177 }
1178
1179no_cookie:
1180 req->analysers &= ~an_bit;
1181 req->analyse_exp = TICK_ETERNITY;
1182 return 1;
1183}
1184
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001185int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001186 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001187 return px->down_time;
1188
1189 return now.tv_sec - px->last_change + px->down_time;
1190}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001192/*
1193 * This function returns a string containing the balancing
1194 * mode of the proxy in a format suitable for stats.
1195 */
1196
1197const char *backend_lb_algo_str(int algo) {
1198
1199 if (algo == BE_LB_ALGO_RR)
1200 return "roundrobin";
1201 else if (algo == BE_LB_ALGO_SRR)
1202 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001203 else if (algo == BE_LB_ALGO_FAS)
1204 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001205 else if (algo == BE_LB_ALGO_LC)
1206 return "leastconn";
1207 else if (algo == BE_LB_ALGO_SH)
1208 return "source";
1209 else if (algo == BE_LB_ALGO_UH)
1210 return "uri";
1211 else if (algo == BE_LB_ALGO_PH)
1212 return "url_param";
1213 else if (algo == BE_LB_ALGO_HH)
1214 return "hdr";
1215 else if (algo == BE_LB_ALGO_RCH)
1216 return "rdp-cookie";
1217 else
1218 return NULL;
1219}
1220
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001221/* This function parses a "balance" statement in a backend section describing
1222 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001223 * returns -1, it will write an error message into the <err> buffer which will
1224 * automatically be allocated and must be passed as NULL. The trailing '\n'
1225 * will not be written. The function must be called with <args> pointing to the
1226 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001227 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001228int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001229{
1230 if (!*(args[0])) {
1231 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001232 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1233 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001234 return 0;
1235 }
1236
1237 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001238 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1239 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001240 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001241 else if (!strcmp(args[0], "static-rr")) {
1242 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1243 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1244 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001245 else if (!strcmp(args[0], "first")) {
1246 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1247 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1248 }
Willy Tarreau51406232008-03-10 22:04:20 +01001249 else if (!strcmp(args[0], "leastconn")) {
1250 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1251 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1252 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001253 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001254 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1255 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001256 }
1257 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001258 int arg = 1;
1259
Willy Tarreau31682232007-11-29 15:38:04 +01001260 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1261 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001262
Oskar Stolc8dc41842012-05-19 10:19:54 +01001263 curproxy->uri_whole = 0;
1264
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001265 while (*args[arg]) {
1266 if (!strcmp(args[arg], "len")) {
1267 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001268 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001269 return -1;
1270 }
1271 curproxy->uri_len_limit = atoi(args[arg+1]);
1272 arg += 2;
1273 }
1274 else if (!strcmp(args[arg], "depth")) {
1275 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001276 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001277 return -1;
1278 }
1279 /* hint: we store the position of the ending '/' (depth+1) so
1280 * that we avoid a comparison while computing the hash.
1281 */
1282 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1283 arg += 2;
1284 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001285 else if (!strcmp(args[arg], "whole")) {
1286 curproxy->uri_whole = 1;
1287 arg += 1;
1288 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001289 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001290 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001291 return -1;
1292 }
1293 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001294 }
Willy Tarreau01732802007-11-01 22:48:15 +01001295 else if (!strcmp(args[0], "url_param")) {
1296 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001297 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001298 return -1;
1299 }
Willy Tarreau31682232007-11-29 15:38:04 +01001300 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1301 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001302
1303 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001304 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001305 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001306 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001307 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001308 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001309 return -1;
1310 }
1311 if (*args[3]) {
1312 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1313 curproxy->url_param_post_limit = str2ui(args[3]);
1314 }
1315 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1316 if (!curproxy->url_param_post_limit)
1317 curproxy->url_param_post_limit = 48;
1318 else if ( curproxy->url_param_post_limit < 3 )
1319 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1320 }
Benoitaffb4812009-03-25 13:02:10 +01001321 }
1322 else if (!strncmp(args[0], "hdr(", 4)) {
1323 const char *beg, *end;
1324
1325 beg = args[0] + 4;
1326 end = strchr(beg, ')');
1327
1328 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001329 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001330 return -1;
1331 }
1332
1333 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1334 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1335
1336 free(curproxy->hh_name);
1337 curproxy->hh_len = end - beg;
1338 curproxy->hh_name = my_strndup(beg, end - beg);
1339 curproxy->hh_match_domain = 0;
1340
1341 if (*args[1]) {
1342 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001343 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001344 return -1;
1345 }
1346 curproxy->hh_match_domain = 1;
1347 }
Emeric Brun736aa232009-06-30 17:56:00 +02001348 }
1349 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1350 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1351 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1352
1353 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1354 const char *beg, *end;
1355
1356 beg = args[0] + 11;
1357 end = strchr(beg, ')');
1358
1359 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001360 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001361 return -1;
1362 }
1363
1364 free(curproxy->hh_name);
1365 curproxy->hh_name = my_strndup(beg, end - beg);
1366 curproxy->hh_len = end - beg;
1367 }
1368 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1369 free(curproxy->hh_name);
1370 curproxy->hh_name = strdup("mstshash");
1371 curproxy->hh_len = strlen(curproxy->hh_name);
1372 }
1373 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001374 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001375 return -1;
1376 }
Willy Tarreau01732802007-11-01 22:48:15 +01001377 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001378 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001379 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 +01001380 return -1;
1381 }
1382 return 0;
1383}
1384
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001385
1386/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001387/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001388/************************************************************************/
1389
Willy Tarreau34db1082012-04-19 17:16:54 +02001390/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001391 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001392 * undefined behaviour.
1393 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001394static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001395smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001396 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001397{
Willy Tarreau37406352012-04-23 16:16:37 +02001398 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001399 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001400 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001401
1402 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001403 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001404 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001405 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001406 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001407 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001408
1409 return 1;
1410}
1411
Willy Tarreau37406352012-04-23 16:16:37 +02001412/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001413 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001414 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1415 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001416 */
1417static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001418smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001419 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001420{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001421 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001422
Willy Tarreau37406352012-04-23 16:16:37 +02001423 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001424 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001425 if (!(srv->state & SRV_MAINTAIN) &&
1426 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001427 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001428 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001429 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001430 return 1;
1431}
1432
Willy Tarreau34db1082012-04-19 17:16:54 +02001433/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001434 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001435 * undefined behaviour.
1436 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001437static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001438smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001439 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001440{
1441 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001442
Willy Tarreau37406352012-04-23 16:16:37 +02001443 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001444 smp->type = SMP_T_UINT;
1445 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001446
Willy Tarreau24e32d82012-04-23 23:55:44 +02001447 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001448 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001449 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001450
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001451 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001452 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001453 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001454 return 1;
1455 }
1456
Willy Tarreauf853c462012-04-23 18:53:56 +02001457 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001458 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001459 }
1460
1461 return 1;
1462}
1463
Willy Tarreaua5e37562011-12-16 17:06:15 +01001464/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001465static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001466smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001467 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001468{
Willy Tarreauf853c462012-04-23 18:53:56 +02001469 smp->flags = SMP_F_VOL_TXN;
1470 smp->type = SMP_T_UINT;
1471 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001472 return 1;
1473}
1474
Willy Tarreaua5e37562011-12-16 17:06:15 +01001475/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001476static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001477smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001478 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001479{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001480 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001481 return 0;
1482
Willy Tarreauf853c462012-04-23 18:53:56 +02001483 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001484 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001485
1486 return 1;
1487}
1488
Willy Tarreau34db1082012-04-19 17:16:54 +02001489/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001490 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001491 * undefined behaviour.
1492 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001493static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001494smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001495 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001496{
Willy Tarreau37406352012-04-23 16:16:37 +02001497 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001498 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001499 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001500 return 1;
1501}
1502
Willy Tarreau34db1082012-04-19 17:16:54 +02001503/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001504 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001505 * undefined behaviour.
1506 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001507static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001508smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001509 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001510{
Willy Tarreau37406352012-04-23 16:16:37 +02001511 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001512 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001513 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001514 return 1;
1515}
1516
Willy Tarreau34db1082012-04-19 17:16:54 +02001517/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001518 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001519 * undefined behaviour.
1520 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001521static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001522smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001523 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001524{
Willy Tarreau37406352012-04-23 16:16:37 +02001525 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001526 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001527 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001528 return 1;
1529}
1530
Willy Tarreaua5e37562011-12-16 17:06:15 +01001531/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001532 * by the number of running servers and rounded up. If there is no running
1533 * server, we return twice the total, just as if we had half a running server.
1534 * This is more or less correct anyway, since we expect the last server to come
1535 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001536 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001537 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001538 */
1539static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001540smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001541 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001542{
1543 int nbsrv;
1544
Willy Tarreau37406352012-04-23 16:16:37 +02001545 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001546 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001547 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001548
1549 if (px->srv_act)
1550 nbsrv = px->srv_act;
1551 else if (px->lbprm.fbck)
1552 nbsrv = 1;
1553 else
1554 nbsrv = px->srv_bck;
1555
1556 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001557 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001558 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001559 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001560
1561 return 1;
1562}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001563
Willy Tarreau34db1082012-04-19 17:16:54 +02001564/* set temp integer to the number of concurrent connections on the server in the backend.
1565 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1566 * undefined behaviour.
1567 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001568static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001569smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001570 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001571{
Willy Tarreauf853c462012-04-23 18:53:56 +02001572 smp->flags = SMP_F_VOL_TEST;
1573 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001574 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001575 return 1;
1576}
1577
Tait Clarridge7896d522012-12-05 21:39:31 -05001578/* set temp integer to the number of enabled servers on the proxy.
1579 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1580 * undefined behaviour.
1581 */
1582static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001583smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001584 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001585{
1586 smp->flags = SMP_F_VOL_TEST;
1587 smp->type = SMP_T_UINT;
1588 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1589 return 1;
1590}
1591
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001592
1593/* Note: must not be declared <const> as its list will be overwritten.
1594 * Please take care of keeping this list alphabetically sorted.
1595 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001596static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001597 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1598 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1599 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1600 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1601 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1602 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1603 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1604 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1605 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1606 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1607 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1608 { /* END */ },
1609}};
1610
1611
Willy Tarreau61612d42012-04-19 18:42:05 +02001612/* Note: must not be declared <const> as its list will be overwritten.
1613 * Please take care of keeping this list alphabetically sorted.
1614 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001615static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001616 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001617}};
1618
1619
1620__attribute__((constructor))
1621static void __backend_init(void)
1622{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001623 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001624 acl_register_keywords(&acl_kws);
1625}
1626
Willy Tarreaubaaee002006-06-26 02:48:02 +02001627/*
1628 * Local variables:
1629 * c-indent-level: 8
1630 * c-basic-offset: 8
1631 * End:
1632 */