blob: 05ce1ef21ba74d22836e3853b9b3ef9b31c312dd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 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 Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020024#include <common/debug.h>
Willy Tarreaub625a082007-11-26 01:15:43 +010025#include <common/eb32tree.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010031#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/backend.h>
Willy Tarreau14c8aac2007-05-08 19:46:30 +020033#include <proto/client.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010035#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010037#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020038#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/task.h>
40
Willy Tarreaub625a082007-11-26 01:15:43 +010041static inline void fwrr_remove_from_tree(struct server *s);
42static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
43static inline void fwrr_dequeue_srv(struct server *s);
44static void fwrr_get_srv(struct server *s);
45static void fwrr_queue_srv(struct server *s);
46
47/* This function returns non-zero if a server with the given weight and state
48 * is usable for LB, otherwise zero.
49 */
50static inline int srv_is_usable(int state, int weight)
51{
52 if (!weight)
53 return 0;
Willy Tarreau48494c02007-11-30 10:41:39 +010054 if (state & SRV_GOINGDOWN)
55 return 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010056 if (!(state & SRV_RUNNING))
57 return 0;
58 return 1;
59}
60
Willy Tarreaubaaee002006-06-26 02:48:02 +020061/*
62 * This function recounts the number of usable active and backup servers for
63 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010064 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010065 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010066 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 */
Willy Tarreaub625a082007-11-26 01:15:43 +010068static void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020069{
70 struct server *srv;
71
Willy Tarreau20697042007-11-15 23:26:18 +010072 px->srv_act = px->srv_bck = 0;
73 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010074 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010076 if (!srv_is_usable(srv->state, srv->eweight))
77 continue;
78
79 if (srv->state & SRV_BACKUP) {
80 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010081 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010082 px->lbprm.fbck = srv;
83 px->srv_bck++;
84 px->lbprm.tot_wbck += srv->eweight;
85 } else {
86 px->srv_act++;
87 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 }
89 }
Willy Tarreaub625a082007-11-26 01:15:43 +010090}
Willy Tarreau20697042007-11-15 23:26:18 +010091
Willy Tarreaub625a082007-11-26 01:15:43 +010092/* This function simply updates the backend's tot_weight and tot_used values
93 * after servers weights have been updated. It is designed to be used after
94 * recount_servers() or equivalent.
95 */
96static void update_backend_weight(struct proxy *px)
97{
Willy Tarreau20697042007-11-15 23:26:18 +010098 if (px->srv_act) {
99 px->lbprm.tot_weight = px->lbprm.tot_wact;
100 px->lbprm.tot_used = px->srv_act;
101 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100102 else if (px->lbprm.fbck) {
103 /* use only the first backup server */
104 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
105 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100106 }
107 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100108 px->lbprm.tot_weight = px->lbprm.tot_wbck;
109 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100110 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100111}
112
113/* this function updates the map according to server <srv>'s new state */
114static void map_set_server_status_down(struct server *srv)
115{
116 struct proxy *p = srv->proxy;
117
118 if (srv->state == srv->prev_state &&
119 srv->eweight == srv->prev_eweight)
120 return;
121
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100122 if (srv_is_usable(srv->state, srv->eweight))
123 goto out_update_state;
124
Willy Tarreaub625a082007-11-26 01:15:43 +0100125 /* FIXME: could be optimized since we know what changed */
126 recount_servers(p);
127 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100128 p->lbprm.map.state |= PR_MAP_RECALC;
129 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100130 srv->prev_state = srv->state;
131 srv->prev_eweight = srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132}
133
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100134/* This function updates the map according to server <srv>'s new state */
Willy Tarreaub625a082007-11-26 01:15:43 +0100135static void map_set_server_status_up(struct server *srv)
136{
137 struct proxy *p = srv->proxy;
138
139 if (srv->state == srv->prev_state &&
140 srv->eweight == srv->prev_eweight)
141 return;
142
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100143 if (!srv_is_usable(srv->state, srv->eweight))
144 goto out_update_state;
145
Willy Tarreaub625a082007-11-26 01:15:43 +0100146 /* FIXME: could be optimized since we know what changed */
147 recount_servers(p);
148 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100149 p->lbprm.map.state |= PR_MAP_RECALC;
150 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100151 srv->prev_state = srv->state;
152 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100153}
154
Willy Tarreau20697042007-11-15 23:26:18 +0100155/* This function recomputes the server map for proxy px. It relies on
156 * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be
157 * called after recount_servers(). It also expects px->lbprm.map.srv
158 * to be allocated with the largest size needed. It updates tot_weight.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 */
160void recalc_server_map(struct proxy *px)
161{
162 int o, tot, flag;
163 struct server *cur, *best;
164
Willy Tarreau20697042007-11-15 23:26:18 +0100165 switch (px->lbprm.tot_used) {
166 case 0: /* no server */
167 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 return;
Willy Tarreau20697042007-11-15 23:26:18 +0100169 case 1: /* only one server, just fill first entry */
170 tot = 1;
171 break;
172 default:
173 tot = px->lbprm.tot_weight;
174 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 }
176
Willy Tarreau20697042007-11-15 23:26:18 +0100177 /* here we *know* that we have some servers */
178 if (px->srv_act)
179 flag = SRV_RUNNING;
180 else
181 flag = SRV_RUNNING | SRV_BACKUP;
182
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 /* this algorithm gives priority to the first server, which means that
184 * it will respect the declaration order for equivalent weights, and
185 * that whatever the weights, the first server called will always be
Willy Tarreau20697042007-11-15 23:26:18 +0100186 * the first declared. This is an important asumption for the backup
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 * case, where we want the first server only.
188 */
189 for (cur = px->srv; cur; cur = cur->next)
190 cur->wscore = 0;
191
192 for (o = 0; o < tot; o++) {
193 int max = 0;
194 best = NULL;
195 for (cur = px->srv; cur; cur = cur->next) {
Willy Tarreau6704d672009-06-15 10:56:05 +0200196 if (cur->eweight &&
197 flag == (cur->state &
Willy Tarreau48494c02007-11-30 10:41:39 +0100198 (SRV_RUNNING | SRV_GOINGDOWN | SRV_BACKUP))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199 int v;
200
201 /* If we are forced to return only one server, we don't want to
202 * go further, because we would return the wrong one due to
203 * divide overflow.
204 */
205 if (tot == 1) {
206 best = cur;
Willy Tarreau20697042007-11-15 23:26:18 +0100207 /* note that best->wscore will be wrong but we don't care */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 break;
209 }
210
Willy Tarreau417fae02007-03-25 21:16:40 +0200211 cur->wscore += cur->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212 v = (cur->wscore + tot) / tot; /* result between 0 and 3 */
213 if (best == NULL || v > max) {
214 max = v;
215 best = cur;
216 }
217 }
218 }
Willy Tarreau20697042007-11-15 23:26:18 +0100219 px->lbprm.map.srv[o] = best;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220 best->wscore -= tot;
221 }
Willy Tarreau20697042007-11-15 23:26:18 +0100222 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223}
224
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100225/* This function is responsible of building the server MAP for map-based LB
226 * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the
227 * weights if applicable. It should be called only once per proxy, at config
228 * time.
229 */
230void init_server_map(struct proxy *p)
231{
232 struct server *srv;
233 int pgcd;
234 int act, bck;
235
Willy Tarreaub625a082007-11-26 01:15:43 +0100236 p->lbprm.set_server_status_up = map_set_server_status_up;
237 p->lbprm.set_server_status_down = map_set_server_status_down;
238 p->lbprm.update_server_eweight = NULL;
239
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100240 if (!p->srv)
241 return;
242
243 /* We will factor the weights to reduce the table,
Willy Tarreau6704d672009-06-15 10:56:05 +0200244 * using Euclide's largest common divisor algorithm.
245 * Since we may have zero weights, we have to first
246 * find a non-zero weight server.
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100247 */
Willy Tarreau6704d672009-06-15 10:56:05 +0200248 pgcd = 1;
249 srv = p->srv;
250 while (srv && !srv->uweight)
251 srv = srv->next;
252
253 if (srv) {
254 pgcd = srv->uweight; /* note: cannot be zero */
255 while (pgcd > 1 && (srv = srv->next)) {
256 int w = srv->uweight;
257 while (w) {
258 int t = pgcd % w;
259 pgcd = w;
260 w = t;
261 }
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100262 }
263 }
264
265 /* It is sometimes useful to know what factor to apply
266 * to the backend's effective weight to know its real
267 * weight.
268 */
269 p->lbprm.wmult = pgcd;
270
271 act = bck = 0;
272 for (srv = p->srv; srv; srv = srv->next) {
273 srv->eweight = srv->uweight / pgcd;
Willy Tarreaub625a082007-11-26 01:15:43 +0100274 srv->prev_eweight = srv->eweight;
275 srv->prev_state = srv->state;
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100276 if (srv->state & SRV_BACKUP)
277 bck += srv->eweight;
278 else
279 act += srv->eweight;
280 }
281
282 /* this is the largest map we will ever need for this servers list */
283 if (act < bck)
284 act = bck;
285
Willy Tarreau6704d672009-06-15 10:56:05 +0200286 if (!act)
287 act = 1;
288
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100289 p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *));
290 /* recounts servers and their weights */
291 p->lbprm.map.state = PR_MAP_RECALC;
292 recount_servers(p);
Willy Tarreaub625a082007-11-26 01:15:43 +0100293 update_backend_weight(p);
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100294 recalc_server_map(p);
295}
296
Willy Tarreaub625a082007-11-26 01:15:43 +0100297/* This function updates the server trees according to server <srv>'s new
298 * state. It should be called when server <srv>'s status changes to down.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100299 * It is not important whether the server was already down or not. It is not
300 * important either that the new state is completely down (the caller may not
301 * know all the variables of a server's state).
Willy Tarreaub625a082007-11-26 01:15:43 +0100302 */
303static void fwrr_set_server_status_down(struct server *srv)
304{
305 struct proxy *p = srv->proxy;
306 struct fwrr_group *grp;
307
308 if (srv->state == srv->prev_state &&
309 srv->eweight == srv->prev_eweight)
310 return;
311
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100312 if (srv_is_usable(srv->state, srv->eweight))
313 goto out_update_state;
314
Willy Tarreaub625a082007-11-26 01:15:43 +0100315 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
316 /* server was already down */
317 goto out_update_backend;
318
319 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
320 grp->next_weight -= srv->prev_eweight;
321
322 if (srv->state & SRV_BACKUP) {
323 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
324 p->srv_bck--;
325
326 if (srv == p->lbprm.fbck) {
327 /* we lost the first backup server in a single-backup
328 * configuration, we must search another one.
329 */
330 struct server *srv2 = p->lbprm.fbck;
331 do {
332 srv2 = srv2->next;
333 } while (srv2 &&
334 !((srv2->state & SRV_BACKUP) &&
335 srv_is_usable(srv2->state, srv2->eweight)));
336 p->lbprm.fbck = srv2;
337 }
338 } else {
339 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
340 p->srv_act--;
341 }
342
343 fwrr_dequeue_srv(srv);
344 fwrr_remove_from_tree(srv);
345
346out_update_backend:
347 /* check/update tot_used, tot_weight */
348 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100349 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100350 srv->prev_state = srv->state;
351 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100352}
353
354/* This function updates the server trees according to server <srv>'s new
355 * state. It should be called when server <srv>'s status changes to up.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100356 * It is not important whether the server was already down or not. It is not
357 * important either that the new state is completely UP (the caller may not
358 * know all the variables of a server's state). This function will not change
Willy Tarreaub625a082007-11-26 01:15:43 +0100359 * the weight of a server which was already up.
360 */
361static void fwrr_set_server_status_up(struct server *srv)
362{
363 struct proxy *p = srv->proxy;
364 struct fwrr_group *grp;
365
366 if (srv->state == srv->prev_state &&
367 srv->eweight == srv->prev_eweight)
368 return;
369
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100370 if (!srv_is_usable(srv->state, srv->eweight))
371 goto out_update_state;
372
Willy Tarreaub625a082007-11-26 01:15:43 +0100373 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
374 /* server was already up */
375 goto out_update_backend;
376
377 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
378 grp->next_weight += srv->eweight;
379
380 if (srv->state & SRV_BACKUP) {
381 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
382 p->srv_bck++;
383
Willy Tarreauf4cca452008-03-08 21:42:54 +0100384 if (!(p->options & PR_O_USE_ALL_BK)) {
385 if (!p->lbprm.fbck) {
386 /* there was no backup server anymore */
Willy Tarreaub625a082007-11-26 01:15:43 +0100387 p->lbprm.fbck = srv;
Willy Tarreauf4cca452008-03-08 21:42:54 +0100388 } else {
389 /* we may have restored a backup server prior to fbck,
390 * in which case it should replace it.
391 */
392 struct server *srv2 = srv;
393 do {
394 srv2 = srv2->next;
395 } while (srv2 && (srv2 != p->lbprm.fbck));
396 if (srv2)
397 p->lbprm.fbck = srv;
398 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100399 }
400 } else {
401 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
402 p->srv_act++;
403 }
404
405 /* note that eweight cannot be 0 here */
406 fwrr_get_srv(srv);
407 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
408 fwrr_queue_srv(srv);
409
410out_update_backend:
411 /* check/update tot_used, tot_weight */
412 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100413 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100414 srv->prev_state = srv->state;
415 srv->prev_eweight = srv->eweight;
416}
417
418/* This function must be called after an update to server <srv>'s effective
419 * weight. It may be called after a state change too.
420 */
421static void fwrr_update_server_weight(struct server *srv)
422{
423 int old_state, new_state;
424 struct proxy *p = srv->proxy;
425 struct fwrr_group *grp;
426
427 if (srv->state == srv->prev_state &&
428 srv->eweight == srv->prev_eweight)
429 return;
430
431 /* If changing the server's weight changes its state, we simply apply
432 * the procedures we already have for status change. If the state
433 * remains down, the server is not in any tree, so it's as easy as
434 * updating its values. If the state remains up with different weights,
435 * there are some computations to perform to find a new place and
436 * possibly a new tree for this server.
437 */
438
439 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
440 new_state = srv_is_usable(srv->state, srv->eweight);
441
442 if (!old_state && !new_state) {
443 srv->prev_state = srv->state;
444 srv->prev_eweight = srv->eweight;
445 return;
446 }
447 else if (!old_state && new_state) {
448 fwrr_set_server_status_up(srv);
449 return;
450 }
451 else if (old_state && !new_state) {
452 fwrr_set_server_status_down(srv);
453 return;
454 }
455
456 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
457 grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight;
458
459 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
460 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
461
462 if (srv->lb_tree == grp->init) {
463 fwrr_dequeue_srv(srv);
464 fwrr_queue_by_weight(grp->init, srv);
465 }
466 else if (!srv->lb_tree) {
467 /* FIXME: server was down. This is not possible right now but
468 * may be needed soon for slowstart or graceful shutdown.
469 */
470 fwrr_dequeue_srv(srv);
471 fwrr_get_srv(srv);
472 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
473 fwrr_queue_srv(srv);
474 } else {
475 /* The server is either active or in the next queue. If it's
476 * still in the active queue and it has not consumed all of its
477 * places, let's adjust its next position.
478 */
479 fwrr_get_srv(srv);
480
481 if (srv->eweight > 0) {
482 int prev_next = srv->npos;
483 int step = grp->next_weight / srv->eweight;
484
485 srv->npos = srv->lpos + step;
486 srv->rweight = 0;
487
488 if (srv->npos > prev_next)
489 srv->npos = prev_next;
490 if (srv->npos < grp->curr_pos + 2)
491 srv->npos = grp->curr_pos + step;
492 } else {
493 /* push it into the next tree */
494 srv->npos = grp->curr_pos + grp->curr_weight;
495 }
496
497 fwrr_dequeue_srv(srv);
498 fwrr_queue_srv(srv);
499 }
500
501 update_backend_weight(p);
502 srv->prev_state = srv->state;
503 srv->prev_eweight = srv->eweight;
504}
505
506/* Remove a server from a tree. It must have previously been dequeued. This
507 * function is meant to be called when a server is going down or has its
508 * weight disabled.
509 */
510static inline void fwrr_remove_from_tree(struct server *s)
511{
512 s->lb_tree = NULL;
513}
514
515/* Queue a server in the weight tree <root>, assuming the weight is >0.
516 * We want to sort them by inverted weights, because we need to place
517 * heavy servers first in order to get a smooth distribution.
518 */
519static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
520{
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100521 s->lb_node.key = SRV_EWGHT_MAX - s->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100522 eb32_insert(root, &s->lb_node);
523 s->lb_tree = root;
524}
525
526/* This function is responsible for building the weight trees in case of fast
527 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
528 * ratio. Both active and backup groups are initialized.
529 */
530void fwrr_init_server_groups(struct proxy *p)
531{
532 struct server *srv;
533 struct eb_root init_head = EB_ROOT;
534
535 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
536 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
537 p->lbprm.update_server_eweight = fwrr_update_server_weight;
538
539 p->lbprm.wdiv = BE_WEIGHT_SCALE;
540 for (srv = p->srv; srv; srv = srv->next) {
541 srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE;
542 srv->prev_state = srv->state;
543 }
544
545 recount_servers(p);
546 update_backend_weight(p);
547
548 /* prepare the active servers group */
549 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
550 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
551 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
552 p->lbprm.fwrr.act.t1 = init_head;
553 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
554 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
555
556 /* prepare the backup servers group */
557 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
558 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
559 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
560 p->lbprm.fwrr.bck.t1 = init_head;
561 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
562 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
563
564 /* queue active and backup servers in two distinct groups */
565 for (srv = p->srv; srv; srv = srv->next) {
566 if (!srv_is_usable(srv->state, srv->eweight))
567 continue;
568 fwrr_queue_by_weight((srv->state & SRV_BACKUP) ?
569 p->lbprm.fwrr.bck.init :
570 p->lbprm.fwrr.act.init,
571 srv);
572 }
573}
574
575/* simply removes a server from a weight tree */
576static inline void fwrr_dequeue_srv(struct server *s)
577{
578 eb32_delete(&s->lb_node);
579}
580
581/* queues a server into the appropriate group and tree depending on its
582 * backup status, and ->npos. If the server is disabled, simply assign
583 * it to the NULL tree.
584 */
585static void fwrr_queue_srv(struct server *s)
586{
587 struct proxy *p = s->proxy;
588 struct fwrr_group *grp;
589
590 grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
591
592 /* Delay everything which does not fit into the window and everything
593 * which does not fit into the theorical new window.
594 */
595 if (!srv_is_usable(s->state, s->eweight)) {
596 fwrr_remove_from_tree(s);
597 }
598 else if (s->eweight <= 0 ||
599 s->npos >= 2 * grp->curr_weight ||
600 s->npos >= grp->curr_weight + grp->next_weight) {
601 /* put into next tree, and readjust npos in case we could
602 * finally take this back to current. */
603 s->npos -= grp->curr_weight;
604 fwrr_queue_by_weight(grp->next, s);
605 }
606 else {
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100607 /* The sorting key is stored in units of s->npos * user_weight
608 * in order to avoid overflows. As stated in backend.h, the
609 * lower the scale, the rougher the weights modulation, and the
610 * higher the scale, the lower the number of servers without
611 * overflow. With this formula, the result is always positive,
612 * so we can use eb3é_insert().
Willy Tarreaub625a082007-11-26 01:15:43 +0100613 */
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100614 s->lb_node.key = SRV_UWGHT_RANGE * s->npos +
615 (unsigned)(SRV_EWGHT_MAX + s->rweight - s->eweight) / BE_WEIGHT_SCALE;
616
617 eb32_insert(&grp->curr, &s->lb_node);
Willy Tarreaub625a082007-11-26 01:15:43 +0100618 s->lb_tree = &grp->curr;
619 }
620}
621
622/* prepares a server when extracting it from the "init" tree */
623static inline void fwrr_get_srv_init(struct server *s)
624{
625 s->npos = s->rweight = 0;
626}
627
628/* prepares a server when extracting it from the "next" tree */
629static inline void fwrr_get_srv_next(struct server *s)
630{
631 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
632 &s->proxy->lbprm.fwrr.bck :
633 &s->proxy->lbprm.fwrr.act;
634
635 s->npos += grp->curr_weight;
636}
637
638/* prepares a server when it was marked down */
639static inline void fwrr_get_srv_down(struct server *s)
640{
641 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
642 &s->proxy->lbprm.fwrr.bck :
643 &s->proxy->lbprm.fwrr.act;
644
645 s->npos = grp->curr_pos;
646}
647
648/* prepares a server when extracting it from its tree */
649static void fwrr_get_srv(struct server *s)
650{
651 struct proxy *p = s->proxy;
652 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
653 &p->lbprm.fwrr.bck :
654 &p->lbprm.fwrr.act;
655
656 if (s->lb_tree == grp->init) {
657 fwrr_get_srv_init(s);
658 }
659 else if (s->lb_tree == grp->next) {
660 fwrr_get_srv_next(s);
661 }
662 else if (s->lb_tree == NULL) {
663 fwrr_get_srv_down(s);
664 }
665}
666
667/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
668 * when this happens, and "next" filled with servers sorted by weights.
669 */
670static inline void fwrr_switch_trees(struct fwrr_group *grp)
671{
672 struct eb_root *swap;
673 swap = grp->init;
674 grp->init = grp->next;
675 grp->next = swap;
676 grp->curr_weight = grp->next_weight;
677 grp->curr_pos = grp->curr_weight;
678}
679
680/* return next server from the current tree in FWRR group <grp>, or a server
681 * from the "init" tree if appropriate. If both trees are empty, return NULL.
682 */
683static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
684{
685 struct eb32_node *node;
686 struct server *s;
687
688 node = eb32_first(&grp->curr);
689 s = eb32_entry(node, struct server, lb_node);
690
691 if (!node || s->npos > grp->curr_pos) {
692 /* either we have no server left, or we have a hole */
693 struct eb32_node *node2;
694 node2 = eb32_first(grp->init);
695 if (node2) {
696 node = node2;
697 s = eb32_entry(node, struct server, lb_node);
698 fwrr_get_srv_init(s);
699 if (s->eweight == 0) /* FIXME: is it possible at all ? */
700 node = NULL;
701 }
702 }
703 if (node)
704 return s;
705 else
706 return NULL;
707}
708
709/* Computes next position of server <s> in the group. It is mandatory for <s>
710 * to have a non-zero, positive eweight.
711*/
712static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
713{
714 if (!s->npos) {
715 /* first time ever for this server */
716 s->lpos = grp->curr_pos;
717 s->npos = grp->curr_pos + grp->next_weight / s->eweight;
718 s->rweight += grp->next_weight % s->eweight;
719
720 if (s->rweight >= s->eweight) {
721 s->rweight -= s->eweight;
722 s->npos++;
723 }
724 } else {
725 s->lpos = s->npos;
726 s->npos += grp->next_weight / s->eweight;
727 s->rweight += grp->next_weight % s->eweight;
728
729 if (s->rweight >= s->eweight) {
730 s->rweight -= s->eweight;
731 s->npos++;
732 }
733 }
734}
735
736/* Return next server from the current tree in backend <p>, or a server from
737 * the init tree if appropriate. If both trees are empty, return NULL.
738 * Saturated servers are skipped and requeued.
739 */
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100740static struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
Willy Tarreaub625a082007-11-26 01:15:43 +0100741{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100742 struct server *srv, *full, *avoided;
Willy Tarreaub625a082007-11-26 01:15:43 +0100743 struct fwrr_group *grp;
Willy Tarreaub625a082007-11-26 01:15:43 +0100744 int switched;
745
746 if (p->srv_act)
747 grp = &p->lbprm.fwrr.act;
748 else if (p->lbprm.fbck)
749 return p->lbprm.fbck;
750 else if (p->srv_bck)
751 grp = &p->lbprm.fwrr.bck;
752 else
753 return NULL;
754
755 switched = 0;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100756 avoided = NULL;
Willy Tarreaub625a082007-11-26 01:15:43 +0100757 full = NULL; /* NULL-terminated list of saturated servers */
758 while (1) {
759 /* if we see an empty group, let's first try to collect weights
760 * which might have recently changed.
761 */
762 if (!grp->curr_weight)
763 grp->curr_pos = grp->curr_weight = grp->next_weight;
764
765 /* get first server from the "current" tree. When the end of
766 * the tree is reached, we may have to switch, but only once.
767 */
768 while (1) {
769 srv = fwrr_get_server_from_group(grp);
770 if (srv)
771 break;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100772 if (switched) {
773 if (avoided) {
774 srv = avoided;
775 break;
776 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100777 goto requeue_servers;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100778 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100779 switched = 1;
780 fwrr_switch_trees(grp);
781
782 }
783
784 /* OK, we have a server. However, it may be saturated, in which
785 * case we don't want to reconsider it for now. We'll update
786 * its position and dequeue it anyway, so that we can move it
787 * to a better place afterwards.
788 */
789 fwrr_update_position(grp, srv);
790 fwrr_dequeue_srv(srv);
791 grp->curr_pos++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200792 if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) {
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100793 /* make sure it is not the server we are trying to exclude... */
794 if (srv != srvtoavoid || avoided)
795 break;
796
797 avoided = srv; /* ...but remember that is was selected yet avoided */
798 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100799
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100800 /* the server is saturated or avoided, let's chain it for later reinsertion */
Willy Tarreaub625a082007-11-26 01:15:43 +0100801 srv->next_full = full;
802 full = srv;
803 }
804
805 /* OK, we got the best server, let's update it */
806 fwrr_queue_srv(srv);
807
808 requeue_servers:
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100809 /* Requeue all extracted servers. If full==srv then it was
810 * avoided (unsucessfully) and chained, omit it now.
811 */
Willy Tarreau70bcfb72008-01-27 02:21:53 +0100812 if (unlikely(full != NULL)) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100813 if (switched) {
814 /* the tree has switched, requeue all extracted servers
815 * into "init", because their place was lost, and only
816 * their weight matters.
817 */
818 do {
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100819 if (likely(full != srv))
820 fwrr_queue_by_weight(grp->init, full);
Willy Tarreaub625a082007-11-26 01:15:43 +0100821 full = full->next_full;
822 } while (full);
823 } else {
824 /* requeue all extracted servers just as if they were consumed
825 * so that they regain their expected place.
826 */
827 do {
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100828 if (likely(full != srv))
829 fwrr_queue_srv(full);
Willy Tarreaub625a082007-11-26 01:15:43 +0100830 full = full->next_full;
831 } while (full);
832 }
833 }
834 return srv;
835}
836
Willy Tarreau51406232008-03-10 22:04:20 +0100837/* Remove a server from a tree. It must have previously been dequeued. This
838 * function is meant to be called when a server is going down or has its
839 * weight disabled.
840 */
841static inline void fwlc_remove_from_tree(struct server *s)
842{
843 s->lb_tree = NULL;
844}
845
846/* simply removes a server from a tree */
847static inline void fwlc_dequeue_srv(struct server *s)
848{
849 eb32_delete(&s->lb_node);
850}
851
852/* Queue a server in its associated tree, assuming the weight is >0.
853 * Servers are sorted by #conns/weight. To ensure maximum accuracy,
854 * we use #conns*SRV_EWGHT_MAX/eweight as the sorting key.
855 */
856static inline void fwlc_queue_srv(struct server *s)
857{
Willy Tarreau7c669d72008-06-20 15:04:11 +0200858 s->lb_node.key = s->served * SRV_EWGHT_MAX / s->eweight;
Willy Tarreau51406232008-03-10 22:04:20 +0100859 eb32_insert(s->lb_tree, &s->lb_node);
860}
861
862/* Re-position the server in the FWLC tree after it has been assigned one
863 * connection or after it has released one. Note that it is possible that
864 * the server has been moved out of the tree due to failed health-checks.
865 */
866static void fwlc_srv_reposition(struct server *s)
867{
868 if (!s->lb_tree)
869 return;
870 fwlc_dequeue_srv(s);
871 fwlc_queue_srv(s);
872}
873
874/* This function updates the server trees according to server <srv>'s new
875 * state. It should be called when server <srv>'s status changes to down.
876 * It is not important whether the server was already down or not. It is not
877 * important either that the new state is completely down (the caller may not
878 * know all the variables of a server's state).
879 */
880static void fwlc_set_server_status_down(struct server *srv)
881{
882 struct proxy *p = srv->proxy;
883
884 if (srv->state == srv->prev_state &&
885 srv->eweight == srv->prev_eweight)
886 return;
887
888 if (srv_is_usable(srv->state, srv->eweight))
889 goto out_update_state;
890
891 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
892 /* server was already down */
893 goto out_update_backend;
894
895 if (srv->state & SRV_BACKUP) {
896 p->lbprm.tot_wbck -= srv->prev_eweight;
897 p->srv_bck--;
898
899 if (srv == p->lbprm.fbck) {
900 /* we lost the first backup server in a single-backup
901 * configuration, we must search another one.
902 */
903 struct server *srv2 = p->lbprm.fbck;
904 do {
905 srv2 = srv2->next;
906 } while (srv2 &&
907 !((srv2->state & SRV_BACKUP) &&
908 srv_is_usable(srv2->state, srv2->eweight)));
909 p->lbprm.fbck = srv2;
910 }
911 } else {
912 p->lbprm.tot_wact -= srv->prev_eweight;
913 p->srv_act--;
914 }
915
916 fwlc_dequeue_srv(srv);
917 fwlc_remove_from_tree(srv);
918
919out_update_backend:
920 /* check/update tot_used, tot_weight */
921 update_backend_weight(p);
922 out_update_state:
923 srv->prev_state = srv->state;
924 srv->prev_eweight = srv->eweight;
925}
926
927/* This function updates the server trees according to server <srv>'s new
928 * state. It should be called when server <srv>'s status changes to up.
929 * It is not important whether the server was already down or not. It is not
930 * important either that the new state is completely UP (the caller may not
931 * know all the variables of a server's state). This function will not change
932 * the weight of a server which was already up.
933 */
934static void fwlc_set_server_status_up(struct server *srv)
935{
936 struct proxy *p = srv->proxy;
937
938 if (srv->state == srv->prev_state &&
939 srv->eweight == srv->prev_eweight)
940 return;
941
942 if (!srv_is_usable(srv->state, srv->eweight))
943 goto out_update_state;
944
945 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
946 /* server was already up */
947 goto out_update_backend;
948
949 if (srv->state & SRV_BACKUP) {
950 srv->lb_tree = &p->lbprm.fwlc.bck;
951 p->lbprm.tot_wbck += srv->eweight;
952 p->srv_bck++;
953
954 if (!(p->options & PR_O_USE_ALL_BK)) {
955 if (!p->lbprm.fbck) {
956 /* there was no backup server anymore */
957 p->lbprm.fbck = srv;
958 } else {
959 /* we may have restored a backup server prior to fbck,
960 * in which case it should replace it.
961 */
962 struct server *srv2 = srv;
963 do {
964 srv2 = srv2->next;
965 } while (srv2 && (srv2 != p->lbprm.fbck));
966 if (srv2)
967 p->lbprm.fbck = srv;
968 }
969 }
970 } else {
971 srv->lb_tree = &p->lbprm.fwlc.act;
972 p->lbprm.tot_wact += srv->eweight;
973 p->srv_act++;
974 }
975
976 /* note that eweight cannot be 0 here */
977 fwlc_queue_srv(srv);
978
979 out_update_backend:
980 /* check/update tot_used, tot_weight */
981 update_backend_weight(p);
982 out_update_state:
983 srv->prev_state = srv->state;
984 srv->prev_eweight = srv->eweight;
985}
986
987/* This function must be called after an update to server <srv>'s effective
988 * weight. It may be called after a state change too.
989 */
990static void fwlc_update_server_weight(struct server *srv)
991{
992 int old_state, new_state;
993 struct proxy *p = srv->proxy;
994
995 if (srv->state == srv->prev_state &&
996 srv->eweight == srv->prev_eweight)
997 return;
998
999 /* If changing the server's weight changes its state, we simply apply
1000 * the procedures we already have for status change. If the state
1001 * remains down, the server is not in any tree, so it's as easy as
1002 * updating its values. If the state remains up with different weights,
1003 * there are some computations to perform to find a new place and
1004 * possibly a new tree for this server.
1005 */
1006
1007 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
1008 new_state = srv_is_usable(srv->state, srv->eweight);
1009
1010 if (!old_state && !new_state) {
1011 srv->prev_state = srv->state;
1012 srv->prev_eweight = srv->eweight;
1013 return;
1014 }
1015 else if (!old_state && new_state) {
1016 fwlc_set_server_status_up(srv);
1017 return;
1018 }
1019 else if (old_state && !new_state) {
1020 fwlc_set_server_status_down(srv);
1021 return;
1022 }
1023
1024 if (srv->lb_tree)
1025 fwlc_dequeue_srv(srv);
1026
1027 if (srv->state & SRV_BACKUP) {
1028 p->lbprm.tot_wbck += srv->eweight - srv->prev_eweight;
1029 srv->lb_tree = &p->lbprm.fwlc.bck;
1030 } else {
1031 p->lbprm.tot_wact += srv->eweight - srv->prev_eweight;
1032 srv->lb_tree = &p->lbprm.fwlc.act;
1033 }
1034
1035 fwlc_queue_srv(srv);
1036
1037 update_backend_weight(p);
1038 srv->prev_state = srv->state;
1039 srv->prev_eweight = srv->eweight;
1040}
1041
1042/* This function is responsible for building the trees in case of fast
1043 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
1044 * uweight ratio. Both active and backup groups are initialized.
1045 */
1046void fwlc_init_server_tree(struct proxy *p)
1047{
1048 struct server *srv;
1049 struct eb_root init_head = EB_ROOT;
1050
1051 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
1052 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
1053 p->lbprm.update_server_eweight = fwlc_update_server_weight;
1054 p->lbprm.server_take_conn = fwlc_srv_reposition;
1055 p->lbprm.server_drop_conn = fwlc_srv_reposition;
1056
1057 p->lbprm.wdiv = BE_WEIGHT_SCALE;
1058 for (srv = p->srv; srv; srv = srv->next) {
1059 srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE;
1060 srv->prev_state = srv->state;
1061 }
1062
1063 recount_servers(p);
1064 update_backend_weight(p);
1065
1066 p->lbprm.fwlc.act = init_head;
1067 p->lbprm.fwlc.bck = init_head;
1068
1069 /* queue active and backup servers in two distinct groups */
1070 for (srv = p->srv; srv; srv = srv->next) {
1071 if (!srv_is_usable(srv->state, srv->eweight))
1072 continue;
1073 srv->lb_tree = (srv->state & SRV_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
1074 fwlc_queue_srv(srv);
1075 }
1076}
1077
1078/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
1079 * return NULL. Saturated servers are skipped.
1080 */
1081static struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
1082{
1083 struct server *srv, *avoided;
1084 struct eb32_node *node;
1085
1086 srv = avoided = NULL;
1087
1088 if (p->srv_act)
1089 node = eb32_first(&p->lbprm.fwlc.act);
1090 else if (p->lbprm.fbck)
1091 return p->lbprm.fbck;
1092 else if (p->srv_bck)
1093 node = eb32_first(&p->lbprm.fwlc.bck);
1094 else
1095 return NULL;
1096
1097 while (node) {
1098 /* OK, we have a server. However, it may be saturated, in which
1099 * case we don't want to reconsider it for now, so we'll simply
1100 * skip it. Same if it's the server we try to avoid, in which
1101 * case we simply remember it for later use if needed.
1102 */
1103 struct server *s;
1104
1105 s = eb32_entry(node, struct server, lb_node);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001106 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
Willy Tarreau51406232008-03-10 22:04:20 +01001107 if (s != srvtoavoid) {
1108 srv = s;
1109 break;
1110 }
1111 avoided = s;
1112 }
1113 node = eb32_next(node);
1114 }
1115
1116 if (!srv)
1117 srv = avoided;
1118
1119 return srv;
1120}
1121
Willy Tarreau01732802007-11-01 22:48:15 +01001122/*
1123 * This function tries to find a running server for the proxy <px> following
1124 * the URL parameter hash method. It looks for a specific parameter in the
1125 * URL and hashes it to compute the server ID. This is useful to optimize
1126 * performance by avoiding bounces between servers in contexts where sessions
1127 * are shared but cookies are not usable. If the parameter is not found, NULL
1128 * is returned. If any server is found, it will be returned. If no valid server
1129 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +01001130 */
1131struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
1132{
1133 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001134 const char *p;
1135 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +01001136 int plen;
1137
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001138 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +01001139 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +01001140 return NULL;
1141
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001142 if ((p = memchr(uri, '?', uri_len)) == NULL)
1143 return NULL;
1144
Willy Tarreau20697042007-11-15 23:26:18 +01001145 if (px->lbprm.map.state & PR_MAP_RECALC)
1146 recalc_server_map(px);
1147
Willy Tarreau01732802007-11-01 22:48:15 +01001148 p++;
1149
1150 uri_len -= (p - uri);
1151 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001152 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +01001153
1154 while (uri_len > plen) {
1155 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001156 if (params[plen] == '=') {
1157 if (memcmp(params, px->url_param_name, plen) == 0) {
1158 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +01001159 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001160 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +01001161 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001162 p += plen + 1;
1163 uri_len -= plen + 1;
1164
Willy Tarreau01732802007-11-01 22:48:15 +01001165 while (uri_len && *p != '&') {
1166 hash = *p + (hash << 6) + (hash << 16) - hash;
1167 uri_len--;
1168 p++;
1169 }
Willy Tarreau20697042007-11-15 23:26:18 +01001170 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau01732802007-11-01 22:48:15 +01001171 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001172 }
1173 /* skip to next parameter */
1174 p = memchr(params, '&', uri_len);
1175 if (!p)
1176 return NULL;
1177 p++;
1178 uri_len -= (p - params);
1179 params = p;
1180 }
1181 return NULL;
1182}
1183
1184/*
1185 * this does the same as the previous server_ph, but check the body contents
1186 */
1187struct server *get_server_ph_post(struct session *s)
1188{
1189 unsigned long hash = 0;
1190 struct http_txn *txn = &s->txn;
1191 struct buffer *req = s->req;
1192 struct http_msg *msg = &txn->req;
1193 struct proxy *px = s->be;
1194 unsigned int plen = px->url_param_len;
Willy Tarreau192ee3e2008-04-19 21:24:56 +02001195 unsigned long body;
1196 unsigned long len;
1197 const char *params;
1198 struct hdr_ctx ctx;
1199 const char *p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001200
1201 /* tot_weight appears to mean srv_count */
1202 if (px->lbprm.tot_weight == 0)
1203 return NULL;
1204
Willy Tarreau192ee3e2008-04-19 21:24:56 +02001205 body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
Willy Tarreaufb0528b2008-08-11 00:21:56 +02001206 len = req->l - body;
Willy Tarreau192ee3e2008-04-19 21:24:56 +02001207 params = req->data + body;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001208
1209 if ( len == 0 )
1210 return NULL;
1211
1212 if (px->lbprm.map.state & PR_MAP_RECALC)
1213 recalc_server_map(px);
1214
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001215 ctx.idx = 0;
1216
1217 /* if the message is chunked, we skip the chunk size, but use the value as len */
1218 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02001219 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001220 unsigned int chunk = 0;
Willy Tarreau03d60bb2009-01-09 11:13:00 +01001221 while ( params < (req->data+req->max_len) && !HTTP_IS_CRLF(*params)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001222 char c = *params;
1223 if (ishex(c)) {
1224 unsigned int hex = toupper(c) - '0';
1225 if ( hex > 9 )
1226 hex -= 'A' - '9' - 1;
1227 chunk = (chunk << 4) | hex;
1228 }
1229 else
1230 return NULL;
1231 params++;
1232 len--;
Willy Tarreau01732802007-11-01 22:48:15 +01001233 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001234 /* spec says we get CRLF */
1235 if (HTTP_IS_CRLF(*params) && HTTP_IS_CRLF(params[1]))
1236 params += 2;
1237 else
1238 return NULL;
1239 /* ok we have some encoded length, just inspect the first chunk */
1240 len = chunk;
1241 }
Willy Tarreau01732802007-11-01 22:48:15 +01001242
Willy Tarreau192ee3e2008-04-19 21:24:56 +02001243 p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001244
1245 while (len > plen) {
1246 /* Look for the parameter name followed by an equal symbol */
1247 if (params[plen] == '=') {
1248 if (memcmp(params, px->url_param_name, plen) == 0) {
1249 /* OK, we have the parameter here at <params>, and
1250 * the value after the equal sign, at <p>
1251 * skip the equal symbol
1252 */
1253 p += plen + 1;
1254 len -= plen + 1;
1255
1256 while (len && *p != '&') {
1257 if (unlikely(!HTTP_IS_TOKEN(*p))) {
1258 /* if in a POST, body must be URI encoded or its not a URI.
1259 * Do not interprete any possible binary data as a parameter.
1260 */
1261 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
1262 break;
1263 return NULL; /* oh, no; this is not uri-encoded.
1264 * This body does not contain parameters.
1265 */
1266 }
1267 hash = *p + (hash << 6) + (hash << 16) - hash;
1268 len--;
1269 p++;
1270 /* should we break if vlen exceeds limit? */
1271 }
1272 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
1273 }
1274 }
Willy Tarreau01732802007-11-01 22:48:15 +01001275 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001276 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +01001277 if (!p)
1278 return NULL;
1279 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001280 len -= (p - params);
1281 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +01001282 }
1283 return NULL;
1284}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001285
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001286
Willy Tarreaubaaee002006-06-26 02:48:02 +02001287/*
Benoitaffb4812009-03-25 13:02:10 +01001288 * This function tries to find a running server for the proxy <px> following
1289 * the Header parameter hash method. It looks for a specific parameter in the
1290 * URL and hashes it to compute the server ID. This is useful to optimize
1291 * performance by avoiding bounces between servers in contexts where sessions
1292 * are shared but cookies are not usable. If the parameter is not found, NULL
1293 * is returned. If any server is found, it will be returned. If no valid server
1294 * is found, NULL is returned.
1295 */
1296struct server *get_server_hh(struct session *s)
1297{
1298 unsigned long hash = 0;
1299 struct http_txn *txn = &s->txn;
1300 struct http_msg *msg = &txn->req;
1301 struct proxy *px = s->be;
1302 unsigned int plen = px->hh_len;
1303 unsigned long len;
1304 struct hdr_ctx ctx;
1305 const char *p;
1306
1307 /* tot_weight appears to mean srv_count */
1308 if (px->lbprm.tot_weight == 0)
1309 return NULL;
1310
1311 if (px->lbprm.map.state & PR_MAP_RECALC)
1312 recalc_server_map(px);
1313
1314 ctx.idx = 0;
1315
1316 /* if the message is chunked, we skip the chunk size, but use the value as len */
1317 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
1318
1319 /* if the header is not found or empty, let's fallback to round robin */
1320 if (!ctx.idx || !ctx.vlen)
1321 return NULL;
1322
1323 /* Found a the hh_name in the headers.
1324 * we will compute the hash based on this value ctx.val.
1325 */
1326 len = ctx.vlen;
1327 p = (char *)ctx.line + ctx.val;
1328 if (!px->hh_match_domain) {
1329 while (len) {
1330 hash = *p + (hash << 6) + (hash << 16) - hash;
1331 len--;
1332 p++;
1333 }
1334 } else {
1335 int dohash = 0;
1336 p += len - 1;
1337 /* special computation, use only main domain name, not tld/host
1338 * going back from the end of string, start hashing at first
1339 * dot stop at next.
1340 * This is designed to work with the 'Host' header, and requires
1341 * a special option to activate this.
1342 */
1343 while (len) {
1344 if (*p == '.') {
1345 if (!dohash)
1346 dohash = 1;
1347 else
1348 break;
1349 } else {
1350 if (dohash)
1351 hash = *p + (hash << 6) + (hash << 16) - hash;
1352 }
1353 len--;
1354 p--;
1355 }
1356 }
1357 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
1358}
1359
Emeric Brun736aa232009-06-30 17:56:00 +02001360struct server *get_server_rch(struct session *s)
1361{
1362 unsigned long hash = 0;
1363 struct proxy *px = s->be;
1364 unsigned long len;
1365 const char *p;
1366 int ret;
1367 struct acl_expr expr;
1368 struct acl_test test;
1369
1370 /* tot_weight appears to mean srv_count */
1371 if (px->lbprm.tot_weight == 0)
1372 return NULL;
1373
1374 if (px->lbprm.map.state & PR_MAP_RECALC)
1375 recalc_server_map(px);
1376
1377 memset(&expr, 0, sizeof(expr));
1378 memset(&test, 0, sizeof(test));
1379
1380 expr.arg.str = px->hh_name;
1381 expr.arg_len = px->hh_len;
1382
1383 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1384 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1385 return NULL;
1386
1387 /* Found a the hh_name in the headers.
1388 * we will compute the hash based on this value ctx.val.
1389 */
1390 len = test.len;
1391 p = (char *)test.ptr;
1392 while (len) {
1393 hash = *p + (hash << 6) + (hash << 16) - hash;
1394 len--;
1395 p++;
1396 }
1397
1398 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
1399}
Benoitaffb4812009-03-25 13:02:10 +01001400
1401/*
Willy Tarreau7c669d72008-06-20 15:04:11 +02001402 * This function applies the load-balancing algorithm to the session, as
1403 * defined by the backend it is assigned to. The session is then marked as
1404 * 'assigned'.
1405 *
1406 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
1407 * had a server previously assigned, it is rebalanced, trying to avoid the same
1408 * server.
1409 * The function tries to keep the original connection slot if it reconnects to
1410 * the same server, otherwise it releases it and tries to offer it.
1411 *
1412 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413 *
1414 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +02001415 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
1416 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
1417 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418 * SRV_STATUS_INTERNAL for other unrecoverable errors.
1419 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02001420 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
1421 * it does not need to be called anymore. This means that s->srv can be trusted
1422 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423 *
1424 */
1425
1426int assign_server(struct session *s)
1427{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001428
Willy Tarreau7c669d72008-06-20 15:04:11 +02001429 struct server *conn_slot;
1430 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001431
Willy Tarreaubaaee002006-06-26 02:48:02 +02001432#ifdef DEBUG_FULL
1433 fprintf(stderr,"assign_server : s=%p\n",s);
1434#endif
1435
Willy Tarreau7c669d72008-06-20 15:04:11 +02001436 err = SRV_STATUS_INTERNAL;
1437 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
1438 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001439
Willy Tarreau7c669d72008-06-20 15:04:11 +02001440 s->prev_srv = s->prev_srv;
1441 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442
Willy Tarreau7c669d72008-06-20 15:04:11 +02001443 /* We have to release any connection slot before applying any LB algo,
1444 * otherwise we may erroneously end up with no available slot.
1445 */
1446 if (conn_slot)
1447 sess_change_server(s, NULL);
1448
1449 /* We will now try to find the good server and store it into <s->srv>.
1450 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
1451 * as well as if no server is available (check error code).
1452 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +01001453
Willy Tarreau7c669d72008-06-20 15:04:11 +02001454 s->srv = NULL;
1455 if (s->be->lbprm.algo & BE_LB_ALGO) {
1456 int len;
1457 /* we must check if we have at least one server available */
1458 if (!s->be->lbprm.tot_weight) {
1459 err = SRV_STATUS_NOSRV;
1460 goto out;
1461 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462
Willy Tarreau7c669d72008-06-20 15:04:11 +02001463 switch (s->be->lbprm.algo & BE_LB_ALGO) {
1464 case BE_LB_ALGO_RR:
1465 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
1466 if (!s->srv) {
1467 err = SRV_STATUS_FULL;
1468 goto out;
1469 }
1470 break;
1471 case BE_LB_ALGO_LC:
1472 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
1473 if (!s->srv) {
1474 err = SRV_STATUS_FULL;
1475 goto out;
1476 }
1477 break;
1478 case BE_LB_ALGO_SH:
1479 if (s->cli_addr.ss_family == AF_INET)
1480 len = 4;
1481 else if (s->cli_addr.ss_family == AF_INET6)
1482 len = 16;
1483 else {
1484 /* unknown IP family */
1485 err = SRV_STATUS_INTERNAL;
1486 goto out;
1487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001488
Willy Tarreau7c669d72008-06-20 15:04:11 +02001489 s->srv = get_server_sh(s->be,
1490 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1491 len);
1492 break;
1493 case BE_LB_ALGO_UH:
1494 /* URI hashing */
1495 s->srv = get_server_uh(s->be,
1496 s->txn.req.sol + s->txn.req.sl.rq.u,
1497 s->txn.req.sl.rq.u_l);
1498 break;
1499 case BE_LB_ALGO_PH:
1500 /* URL Parameter hashing */
1501 if (s->txn.meth == HTTP_METH_POST &&
1502 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
1503 s->txn.req.sl.rq.u_l ) == NULL)
1504 s->srv = get_server_ph_post(s);
1505 else
1506 s->srv = get_server_ph(s->be,
Willy Tarreau2fcb5002007-05-08 13:35:26 +02001507 s->txn.req.sol + s->txn.req.sl.rq.u,
1508 s->txn.req.sl.rq.u_l);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001509
Willy Tarreau7c669d72008-06-20 15:04:11 +02001510 if (!s->srv) {
1511 /* parameter not found, fall back to round robin on the map */
1512 s->srv = get_server_rr_with_conns(s->be, s->prev_srv);
Willy Tarreau01732802007-11-01 22:48:15 +01001513 if (!s->srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001514 err = SRV_STATUS_FULL;
1515 goto out;
Willy Tarreau01732802007-11-01 22:48:15 +01001516 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001517 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001518 break;
Benoitaffb4812009-03-25 13:02:10 +01001519 case BE_LB_ALGO_HH:
1520 /* Header Parameter hashing */
1521 s->srv = get_server_hh(s);
1522
1523 if (!s->srv) {
1524 /* parameter not found, fall back to round robin on the map */
1525 s->srv = get_server_rr_with_conns(s->be, s->prev_srv);
1526 if (!s->srv) {
1527 err = SRV_STATUS_FULL;
1528 goto out;
1529 }
1530 }
1531 break;
Emeric Brun736aa232009-06-30 17:56:00 +02001532 case BE_LB_ALGO_RCH:
1533 /* RDP Cookie hashing */
1534 s->srv = get_server_rch(s);
1535
1536 if (!s->srv) {
1537 /* parameter not found, fall back to round robin on the map */
1538 s->srv = get_server_rr_with_conns(s->be, s->prev_srv);
1539 if (!s->srv) {
1540 err = SRV_STATUS_FULL;
1541 goto out;
1542 }
1543 }
1544 break;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001545 default:
1546 /* unknown balancing algorithm */
1547 err = SRV_STATUS_INTERNAL;
1548 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001549 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001550 if (s->srv != s->prev_srv) {
1551 s->be->cum_lbconn++;
1552 s->srv->cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001553 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001554 }
1555 else if (s->be->options & PR_O_HTTP_PROXY) {
1556 if (!s->srv_addr.sin_addr.s_addr) {
1557 err = SRV_STATUS_NOSRV;
1558 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +01001559 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001560 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001561 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +01001562 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001563 err = SRV_STATUS_NOSRV;
1564 goto out;
1565 }
1566
1567 s->flags |= SN_ASSIGNED;
1568 err = SRV_STATUS_OK;
1569 out:
1570
1571 /* Either we take back our connection slot, or we offer it to someone
1572 * else if we don't need it anymore.
1573 */
1574 if (conn_slot) {
1575 if (conn_slot == s->srv) {
1576 sess_change_server(s, s->srv);
1577 } else {
1578 if (may_dequeue_tasks(conn_slot, s->be))
1579 process_srv_queue(conn_slot);
1580 }
1581 }
1582
1583 out_err:
1584 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001585}
1586
1587
1588/*
1589 * This function assigns a server address to a session, and sets SN_ADDR_SET.
1590 * The address is taken from the currently assigned server, or from the
1591 * dispatch or transparent address.
1592 *
1593 * It may return :
1594 * SRV_STATUS_OK if everything is OK.
1595 * SRV_STATUS_INTERNAL for other unrecoverable errors.
1596 *
1597 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
1598 * not cleared, so it's to the caller to clear it if required.
1599 *
1600 */
1601int assign_server_address(struct session *s)
1602{
1603#ifdef DEBUG_FULL
1604 fprintf(stderr,"assign_server_address : s=%p\n",s);
1605#endif
1606
Willy Tarreau31682232007-11-29 15:38:04 +01001607 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001608 /* A server is necessarily known for this session */
1609 if (!(s->flags & SN_ASSIGNED))
1610 return SRV_STATUS_INTERNAL;
1611
1612 s->srv_addr = s->srv->addr;
1613
1614 /* if this server remaps proxied ports, we'll use
1615 * the port the client connected to with an offset. */
1616 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +01001617 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +02001618 get_frt_addr(s);
1619 if (s->frt_addr.ss_family == AF_INET) {
1620 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1621 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
1622 } else {
1623 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1624 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
1625 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001626 }
1627 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001628 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001629 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001630 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001631 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +01001632 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001633 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +01001634 if (!(s->flags & SN_FRT_ADDR_SET))
1635 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636
Willy Tarreaubd414282008-01-19 13:46:35 +01001637 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
1638 /* when we support IPv6 on the backend, we may add other tests */
1639 //qfprintf(stderr, "Cannot get original server address.\n");
1640 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001641 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001642 else if (s->be->options & PR_O_HTTP_PROXY) {
1643 /* If HTTP PROXY option is set, then server is already assigned
1644 * during incoming client request parsing. */
1645 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +01001646 else {
1647 /* no server and no LB algorithm ! */
1648 return SRV_STATUS_INTERNAL;
1649 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001650
1651 s->flags |= SN_ADDR_SET;
1652 return SRV_STATUS_OK;
1653}
1654
1655
1656/* This function assigns a server to session <s> if required, and can add the
1657 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001658 * If ->srv_conn is set, the session is first released from the server.
1659 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
1660 * be called before any connection and after any retry or redispatch occurs.
1661 *
1662 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001663 *
1664 * Returns :
1665 *
1666 * SRV_STATUS_OK if everything is OK.
1667 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
1668 * SRV_STATUS_QUEUED if the connection has been queued.
1669 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +02001670 * connection could not be queued in s->srv,
1671 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001672 * SRV_STATUS_INTERNAL for other unrecoverable errors.
1673 *
1674 */
1675int assign_server_and_queue(struct session *s)
1676{
1677 struct pendconn *p;
1678 int err;
1679
1680 if (s->pend_pos)
1681 return SRV_STATUS_INTERNAL;
1682
Willy Tarreau7c669d72008-06-20 15:04:11 +02001683 err = SRV_STATUS_OK;
1684 if (!(s->flags & SN_ASSIGNED)) {
1685 err = assign_server(s);
1686 if (s->prev_srv) {
1687 /* This session was previously assigned to a server. We have to
1688 * update the session's and the server's stats :
1689 * - if the server changed :
1690 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
1691 * - set SN_REDISP if it was successfully redispatched
1692 * - increment srv->redispatches and be->redispatches
1693 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001694 */
1695
Willy Tarreau7c669d72008-06-20 15:04:11 +02001696 if (s->prev_srv != s->srv) {
1697 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
1698 s->txn.flags &= ~TX_CK_MASK;
1699 s->txn.flags |= TX_CK_DOWN;
1700 }
1701 s->flags |= SN_REDISP;
1702 s->prev_srv->redispatches++;
1703 s->be->redispatches++;
1704 } else {
1705 s->prev_srv->retries++;
1706 s->be->retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001707 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001708 }
1709 }
1710
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711 switch (err) {
1712 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +02001713 /* we have SN_ASSIGNED set */
1714 if (!s->srv)
1715 return SRV_STATUS_OK; /* dispatch or proxy mode */
1716
1717 /* If we already have a connection slot, no need to check any queue */
1718 if (s->srv_conn == s->srv)
1719 return SRV_STATUS_OK;
1720
1721 /* OK, this session already has an assigned server, but no
1722 * connection slot yet. Either it is a redispatch, or it was
1723 * assigned from persistence information (direct mode).
1724 */
1725 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
1726 /* server scheduled for redirection, and already assigned. We
1727 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +01001728 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001729 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +01001730 return SRV_STATUS_OK;
1731 }
1732
Willy Tarreau7c669d72008-06-20 15:04:11 +02001733 /* We might have to queue this session if the assigned server is full.
1734 * We know we have to queue it into the server's queue, so if a maxqueue
1735 * is set on the server, we must also check that the server's queue is
1736 * not full, in which case we have to return FULL.
1737 */
1738 if (s->srv->maxconn &&
1739 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
1740
1741 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
1742 return SRV_STATUS_FULL;
1743
Willy Tarreaubaaee002006-06-26 02:48:02 +02001744 p = pendconn_add(s);
1745 if (p)
1746 return SRV_STATUS_QUEUED;
1747 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001748 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001749 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001750
1751 /* OK, we can use this server. Let's reserve our place */
1752 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001753 return SRV_STATUS_OK;
1754
1755 case SRV_STATUS_FULL:
1756 /* queue this session into the proxy's queue */
1757 p = pendconn_add(s);
1758 if (p)
1759 return SRV_STATUS_QUEUED;
1760 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001761 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001762
1763 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +02001764 return err;
1765
Willy Tarreaubaaee002006-06-26 02:48:02 +02001766 case SRV_STATUS_INTERNAL:
1767 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001768
Willy Tarreaubaaee002006-06-26 02:48:02 +02001769 default:
1770 return SRV_STATUS_INTERNAL;
1771 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +01001772}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001773
1774/*
1775 * This function initiates a connection to the server assigned to this session
1776 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
1777 * It can return one of :
1778 * - SN_ERR_NONE if everything's OK
1779 * - SN_ERR_SRVTO if there are no more servers
1780 * - SN_ERR_SRVCL if the connection was refused by the server
1781 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1782 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1783 * - SN_ERR_INTERNAL for any other purely internal errors
1784 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1785 */
1786int connect_server(struct session *s)
1787{
Willy Tarreau9650f372009-08-16 14:02:45 +02001788 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001789
1790 if (!(s->flags & SN_ADDR_SET)) {
1791 err = assign_server_address(s);
1792 if (err != SRV_STATUS_OK)
1793 return SN_ERR_INTERNAL;
1794 }
1795
Willy Tarreau9650f372009-08-16 14:02:45 +02001796 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001797 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001798
Willy Tarreau9650f372009-08-16 14:02:45 +02001799 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
1800 (struct sockaddr *)&s->srv_addr,
1801 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001802
Willy Tarreau9650f372009-08-16 14:02:45 +02001803 if (err != SN_ERR_NONE)
1804 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001805
Willy Tarreaubaaee002006-06-26 02:48:02 +02001806 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001807 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001808 s->srv->cur_sess++;
1809 if (s->srv->cur_sess > s->srv->cur_sess_max)
1810 s->srv->cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001811 if (s->be->lbprm.server_take_conn)
1812 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001813 }
1814
Willy Tarreaubaaee002006-06-26 02:48:02 +02001815 return SN_ERR_NONE; /* connection is OK */
1816}
1817
1818
Willy Tarreaubaaee002006-06-26 02:48:02 +02001819/* This function performs the "redispatch" part of a connection attempt. It
1820 * will assign a server if required, queue the connection if required, and
1821 * handle errors that might arise at this level. It can change the server
1822 * state. It will return 1 if it encounters an error, switches the server
1823 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1824 * that the connection is ready to use.
1825 */
1826
1827int srv_redispatch_connect(struct session *t)
1828{
1829 int conn_err;
1830
1831 /* We know that we don't have any connection pending, so we will
1832 * try to get a new one, and wait in this state if it's queued
1833 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001834 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001835 conn_err = assign_server_and_queue(t);
1836 switch (conn_err) {
1837 case SRV_STATUS_OK:
1838 break;
1839
Willy Tarreau7c669d72008-06-20 15:04:11 +02001840 case SRV_STATUS_FULL:
1841 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1842 * and we can redispatch to another server, or it is not and we return
1843 * 503. This only makes sense in DIRECT mode however, because normal LB
1844 * algorithms would never select such a server, and hash algorithms
1845 * would bring us on the same server again. Note that t->srv is set in
1846 * this case.
1847 */
1848 if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) {
1849 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1850 t->prev_srv = t->srv;
1851 goto redispatch;
1852 }
1853
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001854 if (!t->req->cons->err_type) {
1855 t->req->cons->err_type = SI_ET_QUEUE_ERR;
1856 t->req->cons->err_loc = t->srv;
1857 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001858
1859 t->srv->failed_conns++;
1860 t->be->failed_conns++;
1861 return 1;
1862
Willy Tarreaubaaee002006-06-26 02:48:02 +02001863 case SRV_STATUS_NOSRV:
1864 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001865 if (!t->req->cons->err_type) {
1866 t->req->cons->err_type = SI_ET_CONN_ERR;
1867 t->req->cons->err_loc = NULL;
1868 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001869
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001870 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001871 return 1;
1872
1873 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001874 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001875 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001876 /* do nothing else and do not wake any other session up */
1877 return 1;
1878
Willy Tarreaubaaee002006-06-26 02:48:02 +02001879 case SRV_STATUS_INTERNAL:
1880 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001881 if (!t->req->cons->err_type) {
1882 t->req->cons->err_type = SI_ET_CONN_OTHER;
1883 t->req->cons->err_loc = t->srv;
1884 }
1885
Willy Tarreaubaaee002006-06-26 02:48:02 +02001886 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001887 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001888 if (t->srv)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001889 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001890 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891
1892 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001893 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001894 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001895 return 1;
1896 }
1897 /* if we get here, it's because we got SRV_STATUS_OK, which also
1898 * means that the connection has not been queued.
1899 */
1900 return 0;
1901}
1902
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001903int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001904 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001905 return px->down_time;
1906
1907 return now.tv_sec - px->last_change + px->down_time;
1908}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001910/* This function parses a "balance" statement in a backend section describing
1911 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1912 * returns -1, it may write an error message into ther <err> buffer, for at
1913 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1914 * written. The function must be called with <args> pointing to the first word
1915 * after "balance".
1916 */
1917int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1918{
1919 if (!*(args[0])) {
1920 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001921 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1922 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001923 return 0;
1924 }
1925
1926 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001927 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1928 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001929 }
Willy Tarreau51406232008-03-10 22:04:20 +01001930 else if (!strcmp(args[0], "leastconn")) {
1931 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1932 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1933 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001934 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001935 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1936 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001937 }
1938 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001939 int arg = 1;
1940
Willy Tarreau31682232007-11-29 15:38:04 +01001941 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1942 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001943
1944 while (*args[arg]) {
1945 if (!strcmp(args[arg], "len")) {
1946 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1947 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1948 return -1;
1949 }
1950 curproxy->uri_len_limit = atoi(args[arg+1]);
1951 arg += 2;
1952 }
1953 else if (!strcmp(args[arg], "depth")) {
1954 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1955 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1956 return -1;
1957 }
1958 /* hint: we store the position of the ending '/' (depth+1) so
1959 * that we avoid a comparison while computing the hash.
1960 */
1961 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1962 arg += 2;
1963 }
1964 else {
1965 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1966 return -1;
1967 }
1968 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001969 }
Willy Tarreau01732802007-11-01 22:48:15 +01001970 else if (!strcmp(args[0], "url_param")) {
1971 if (!*args[1]) {
1972 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1973 return -1;
1974 }
Willy Tarreau31682232007-11-29 15:38:04 +01001975 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1976 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001977
1978 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001979 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001980 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001981 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001982 if (strcmp(args[2], "check_post")) {
1983 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1984 return -1;
1985 }
1986 if (*args[3]) {
1987 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1988 curproxy->url_param_post_limit = str2ui(args[3]);
1989 }
1990 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1991 if (!curproxy->url_param_post_limit)
1992 curproxy->url_param_post_limit = 48;
1993 else if ( curproxy->url_param_post_limit < 3 )
1994 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1995 }
Benoitaffb4812009-03-25 13:02:10 +01001996 }
1997 else if (!strncmp(args[0], "hdr(", 4)) {
1998 const char *beg, *end;
1999
2000 beg = args[0] + 4;
2001 end = strchr(beg, ')');
2002
2003 if (!end || end == beg) {
2004 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
2005 return -1;
2006 }
2007
2008 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2009 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
2010
2011 free(curproxy->hh_name);
2012 curproxy->hh_len = end - beg;
2013 curproxy->hh_name = my_strndup(beg, end - beg);
2014 curproxy->hh_match_domain = 0;
2015
2016 if (*args[1]) {
2017 if (strcmp(args[1], "use_domain_only")) {
2018 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
2019 return -1;
2020 }
2021 curproxy->hh_match_domain = 1;
2022 }
2023
Emeric Brun736aa232009-06-30 17:56:00 +02002024 }
2025 else if (!strncmp(args[0], "rdp-cookie", 10)) {
2026 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2027 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
2028
2029 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
2030 const char *beg, *end;
2031
2032 beg = args[0] + 11;
2033 end = strchr(beg, ')');
2034
2035 if (!end || end == beg) {
2036 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
2037 return -1;
2038 }
2039
2040 free(curproxy->hh_name);
2041 curproxy->hh_name = my_strndup(beg, end - beg);
2042 curproxy->hh_len = end - beg;
2043 }
2044 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
2045 free(curproxy->hh_name);
2046 curproxy->hh_name = strdup("mstshash");
2047 curproxy->hh_len = strlen(curproxy->hh_name);
2048 }
2049 else { /* syntax */
2050 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
2051 return -1;
2052 }
Willy Tarreau01732802007-11-01 22:48:15 +01002053 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002054 else {
Emeric Brun736aa232009-06-30 17:56:00 +02002055 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002056 return -1;
2057 }
2058 return 0;
2059}
2060
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002061
2062/************************************************************************/
2063/* All supported keywords must be declared here. */
2064/************************************************************************/
2065
2066/* set test->i to the number of enabled servers on the proxy */
2067static int
2068acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
2069 struct acl_expr *expr, struct acl_test *test)
2070{
2071 test->flags = ACL_TEST_F_VOL_TEST;
2072 if (expr->arg_len) {
2073 /* another proxy was designated, we must look for it */
2074 for (px = proxy; px; px = px->next)
2075 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
2076 break;
2077 }
2078 if (!px)
2079 return 0;
2080
2081 if (px->srv_act)
2082 test->i = px->srv_act;
2083 else if (px->lbprm.fbck)
2084 test->i = 1;
2085 else
2086 test->i = px->srv_bck;
2087
2088 return 1;
2089}
2090
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002091/* set test->i to the number of enabled servers on the proxy */
2092static int
2093acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
2094 struct acl_expr *expr, struct acl_test *test)
2095{
2096 struct server *iterator;
2097 test->flags = ACL_TEST_F_VOL_TEST;
2098 if (expr->arg_len) {
2099 /* another proxy was designated, we must look for it */
2100 for (px = proxy; px; px = px->next)
2101 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
2102 break;
2103 }
2104 if (!px)
2105 return 0;
2106
2107 test->i = 0;
2108 iterator = px->srv;
2109 while (iterator) {
2110 if ((iterator->state & 1) == 0) {
2111 iterator = iterator->next;
2112 continue;
2113 }
2114 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
2115 test->i = -1;
2116 return 1;
2117 }
2118
2119 test->i += (iterator->maxconn - iterator->cur_sess)
2120 + (iterator->maxqueue - iterator->nbpend);
2121 iterator = iterator->next;
2122 }
2123
2124 return 1;
2125}
2126
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002127/* set test->i to the number of connections per second reaching the frontend */
2128static int
2129acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2130 struct acl_expr *expr, struct acl_test *test)
2131{
2132 test->flags = ACL_TEST_F_VOL_TEST;
2133 if (expr->arg_len) {
2134 /* another proxy was designated, we must look for it */
2135 for (px = proxy; px; px = px->next)
2136 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
2137 break;
2138 }
2139 if (!px)
2140 return 0;
2141
2142 test->i = read_freq_ctr(&px->fe_sess_per_sec);
2143 return 1;
2144}
2145
2146/* set test->i to the number of connections per second reaching the backend */
2147static int
2148acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2149 struct acl_expr *expr, struct acl_test *test)
2150{
2151 test->flags = ACL_TEST_F_VOL_TEST;
2152 if (expr->arg_len) {
2153 /* another proxy was designated, we must look for it */
2154 for (px = proxy; px; px = px->next)
2155 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
2156 break;
2157 }
2158 if (!px)
2159 return 0;
2160
2161 test->i = read_freq_ctr(&px->be_sess_per_sec);
2162 return 1;
2163}
2164
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002165
2166/* Note: must not be declared <const> as its list will be overwritten */
2167static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002168 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01002169 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002170 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
2171 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002172 { NULL, NULL, NULL, NULL },
2173}};
2174
2175
2176__attribute__((constructor))
2177static void __backend_init(void)
2178{
2179 acl_register_keywords(&acl_kws);
2180}
2181
2182
Willy Tarreaubaaee002006-06-26 02:48:02 +02002183/*
2184 * Local variables:
2185 * c-indent-level: 8
2186 * c-basic-offset: 8
2187 * End:
2188 */