blob: 4dc2b9caf0d081d7c891471ce4d83711e9fa27cf [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreaud825eef2007-05-12 22:35:00 +02004 * Copyright 2000-2007 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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020021#include <common/config.h>
Willy Tarreaub625a082007-11-26 01:15:43 +010022#include <common/eb32tree.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010025#include <types/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <types/buffers.h>
27#include <types/global.h>
28#include <types/polling.h>
29#include <types/proxy.h>
30#include <types/server.h>
31#include <types/session.h>
32
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010033#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreau14c8aac2007-05-08 19:46:30 +020035#include <proto/client.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/fd.h>
Willy Tarreau80587432006-12-24 17:47:20 +010037#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/log.h>
39#include <proto/proto_http.h>
40#include <proto/queue.h>
41#include <proto/stream_sock.h>
42#include <proto/task.h>
43
Willy Tarreau77074d52006-11-12 23:57:19 +010044#ifdef CONFIG_HAP_CTTPROXY
45#include <import/ip_tproxy.h>
46#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau6d1a9882007-01-07 02:03:04 +010048#ifdef CONFIG_HAP_TCPSPLICE
49#include <libtcpsplice.h>
50#endif
51
Willy Tarreaub625a082007-11-26 01:15:43 +010052static inline void fwrr_remove_from_tree(struct server *s);
53static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
54static inline void fwrr_dequeue_srv(struct server *s);
55static void fwrr_get_srv(struct server *s);
56static void fwrr_queue_srv(struct server *s);
57
58/* This function returns non-zero if a server with the given weight and state
59 * is usable for LB, otherwise zero.
60 */
61static inline int srv_is_usable(int state, int weight)
62{
63 if (!weight)
64 return 0;
Willy Tarreau48494c02007-11-30 10:41:39 +010065 if (state & SRV_GOINGDOWN)
66 return 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010067 if (!(state & SRV_RUNNING))
68 return 0;
69 return 1;
70}
71
Willy Tarreaubaaee002006-06-26 02:48:02 +020072/*
73 * This function recounts the number of usable active and backup servers for
74 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010075 * This function also recomputes the total active and backup weights. However,
76 * it does nout update tot_weight nor tot_used. Use update_backend_weight() for
77 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 */
Willy Tarreaub625a082007-11-26 01:15:43 +010079static void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020080{
81 struct server *srv;
82
Willy Tarreau20697042007-11-15 23:26:18 +010083 px->srv_act = px->srv_bck = 0;
84 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010085 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020086 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010087 if (!srv_is_usable(srv->state, srv->eweight))
88 continue;
89
90 if (srv->state & SRV_BACKUP) {
91 if (!px->srv_bck &&
Willy Tarreau31682232007-11-29 15:38:04 +010092 !(px->lbprm.algo & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010093 px->lbprm.fbck = srv;
94 px->srv_bck++;
95 px->lbprm.tot_wbck += srv->eweight;
96 } else {
97 px->srv_act++;
98 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 }
100 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100101}
Willy Tarreau20697042007-11-15 23:26:18 +0100102
Willy Tarreaub625a082007-11-26 01:15:43 +0100103/* This function simply updates the backend's tot_weight and tot_used values
104 * after servers weights have been updated. It is designed to be used after
105 * recount_servers() or equivalent.
106 */
107static void update_backend_weight(struct proxy *px)
108{
Willy Tarreau20697042007-11-15 23:26:18 +0100109 if (px->srv_act) {
110 px->lbprm.tot_weight = px->lbprm.tot_wact;
111 px->lbprm.tot_used = px->srv_act;
112 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100113 else if (px->lbprm.fbck) {
114 /* use only the first backup server */
115 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
116 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100117 }
118 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100119 px->lbprm.tot_weight = px->lbprm.tot_wbck;
120 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100121 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100122}
123
124/* this function updates the map according to server <srv>'s new state */
125static void map_set_server_status_down(struct server *srv)
126{
127 struct proxy *p = srv->proxy;
128
129 if (srv->state == srv->prev_state &&
130 srv->eweight == srv->prev_eweight)
131 return;
132
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100133 if (srv_is_usable(srv->state, srv->eweight))
134 goto out_update_state;
135
Willy Tarreaub625a082007-11-26 01:15:43 +0100136 /* FIXME: could be optimized since we know what changed */
137 recount_servers(p);
138 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100139 p->lbprm.map.state |= PR_MAP_RECALC;
140 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100141 srv->prev_state = srv->state;
142 srv->prev_eweight = srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143}
144
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100145/* This function updates the map according to server <srv>'s new state */
Willy Tarreaub625a082007-11-26 01:15:43 +0100146static void map_set_server_status_up(struct server *srv)
147{
148 struct proxy *p = srv->proxy;
149
150 if (srv->state == srv->prev_state &&
151 srv->eweight == srv->prev_eweight)
152 return;
153
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100154 if (!srv_is_usable(srv->state, srv->eweight))
155 goto out_update_state;
156
Willy Tarreaub625a082007-11-26 01:15:43 +0100157 /* FIXME: could be optimized since we know what changed */
158 recount_servers(p);
159 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100160 p->lbprm.map.state |= PR_MAP_RECALC;
161 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100162 srv->prev_state = srv->state;
163 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100164}
165
Willy Tarreau20697042007-11-15 23:26:18 +0100166/* This function recomputes the server map for proxy px. It relies on
167 * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be
168 * called after recount_servers(). It also expects px->lbprm.map.srv
169 * to be allocated with the largest size needed. It updates tot_weight.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 */
171void recalc_server_map(struct proxy *px)
172{
173 int o, tot, flag;
174 struct server *cur, *best;
175
Willy Tarreau20697042007-11-15 23:26:18 +0100176 switch (px->lbprm.tot_used) {
177 case 0: /* no server */
178 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 return;
Willy Tarreau20697042007-11-15 23:26:18 +0100180 case 1: /* only one server, just fill first entry */
181 tot = 1;
182 break;
183 default:
184 tot = px->lbprm.tot_weight;
185 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 }
187
Willy Tarreau20697042007-11-15 23:26:18 +0100188 /* here we *know* that we have some servers */
189 if (px->srv_act)
190 flag = SRV_RUNNING;
191 else
192 flag = SRV_RUNNING | SRV_BACKUP;
193
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194 /* this algorithm gives priority to the first server, which means that
195 * it will respect the declaration order for equivalent weights, and
196 * that whatever the weights, the first server called will always be
Willy Tarreau20697042007-11-15 23:26:18 +0100197 * the first declared. This is an important asumption for the backup
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198 * case, where we want the first server only.
199 */
200 for (cur = px->srv; cur; cur = cur->next)
201 cur->wscore = 0;
202
203 for (o = 0; o < tot; o++) {
204 int max = 0;
205 best = NULL;
206 for (cur = px->srv; cur; cur = cur->next) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100207 if (flag == (cur->state &
208 (SRV_RUNNING | SRV_GOINGDOWN | SRV_BACKUP))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 int v;
210
211 /* If we are forced to return only one server, we don't want to
212 * go further, because we would return the wrong one due to
213 * divide overflow.
214 */
215 if (tot == 1) {
216 best = cur;
Willy Tarreau20697042007-11-15 23:26:18 +0100217 /* note that best->wscore will be wrong but we don't care */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 break;
219 }
220
Willy Tarreau417fae02007-03-25 21:16:40 +0200221 cur->wscore += cur->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222 v = (cur->wscore + tot) / tot; /* result between 0 and 3 */
223 if (best == NULL || v > max) {
224 max = v;
225 best = cur;
226 }
227 }
228 }
Willy Tarreau20697042007-11-15 23:26:18 +0100229 px->lbprm.map.srv[o] = best;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 best->wscore -= tot;
231 }
Willy Tarreau20697042007-11-15 23:26:18 +0100232 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233}
234
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100235/* This function is responsible of building the server MAP for map-based LB
236 * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the
237 * weights if applicable. It should be called only once per proxy, at config
238 * time.
239 */
240void init_server_map(struct proxy *p)
241{
242 struct server *srv;
243 int pgcd;
244 int act, bck;
245
Willy Tarreaub625a082007-11-26 01:15:43 +0100246 p->lbprm.set_server_status_up = map_set_server_status_up;
247 p->lbprm.set_server_status_down = map_set_server_status_down;
248 p->lbprm.update_server_eweight = NULL;
249
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100250 if (!p->srv)
251 return;
252
253 /* We will factor the weights to reduce the table,
254 * using Euclide's largest common divisor algorithm
255 */
256 pgcd = p->srv->uweight;
257 for (srv = p->srv->next; srv && pgcd > 1; srv = srv->next) {
258 int w = srv->uweight;
259 while (w) {
260 int t = pgcd % w;
261 pgcd = w;
262 w = t;
263 }
264 }
265
266 /* It is sometimes useful to know what factor to apply
267 * to the backend's effective weight to know its real
268 * weight.
269 */
270 p->lbprm.wmult = pgcd;
271
272 act = bck = 0;
273 for (srv = p->srv; srv; srv = srv->next) {
274 srv->eweight = srv->uweight / pgcd;
Willy Tarreaub625a082007-11-26 01:15:43 +0100275 srv->prev_eweight = srv->eweight;
276 srv->prev_state = srv->state;
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100277 if (srv->state & SRV_BACKUP)
278 bck += srv->eweight;
279 else
280 act += srv->eweight;
281 }
282
283 /* this is the largest map we will ever need for this servers list */
284 if (act < bck)
285 act = bck;
286
287 p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *));
288 /* recounts servers and their weights */
289 p->lbprm.map.state = PR_MAP_RECALC;
290 recount_servers(p);
Willy Tarreaub625a082007-11-26 01:15:43 +0100291 update_backend_weight(p);
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100292 recalc_server_map(p);
293}
294
Willy Tarreaub625a082007-11-26 01:15:43 +0100295/* This function updates the server trees according to server <srv>'s new
296 * state. It should be called when server <srv>'s status changes to down.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100297 * It is not important whether the server was already down or not. It is not
298 * important either that the new state is completely down (the caller may not
299 * know all the variables of a server's state).
Willy Tarreaub625a082007-11-26 01:15:43 +0100300 */
301static void fwrr_set_server_status_down(struct server *srv)
302{
303 struct proxy *p = srv->proxy;
304 struct fwrr_group *grp;
305
306 if (srv->state == srv->prev_state &&
307 srv->eweight == srv->prev_eweight)
308 return;
309
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100310 if (srv_is_usable(srv->state, srv->eweight))
311 goto out_update_state;
312
Willy Tarreaub625a082007-11-26 01:15:43 +0100313 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
314 /* server was already down */
315 goto out_update_backend;
316
317 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
318 grp->next_weight -= srv->prev_eweight;
319
320 if (srv->state & SRV_BACKUP) {
321 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
322 p->srv_bck--;
323
324 if (srv == p->lbprm.fbck) {
325 /* we lost the first backup server in a single-backup
326 * configuration, we must search another one.
327 */
328 struct server *srv2 = p->lbprm.fbck;
329 do {
330 srv2 = srv2->next;
331 } while (srv2 &&
332 !((srv2->state & SRV_BACKUP) &&
333 srv_is_usable(srv2->state, srv2->eweight)));
334 p->lbprm.fbck = srv2;
335 }
336 } else {
337 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
338 p->srv_act--;
339 }
340
341 fwrr_dequeue_srv(srv);
342 fwrr_remove_from_tree(srv);
343
344out_update_backend:
345 /* check/update tot_used, tot_weight */
346 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100347 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100348 srv->prev_state = srv->state;
349 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100350}
351
352/* This function updates the server trees according to server <srv>'s new
353 * state. It should be called when server <srv>'s status changes to up.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100354 * It is not important whether the server was already down or not. It is not
355 * important either that the new state is completely UP (the caller may not
356 * know all the variables of a server's state). This function will not change
Willy Tarreaub625a082007-11-26 01:15:43 +0100357 * the weight of a server which was already up.
358 */
359static void fwrr_set_server_status_up(struct server *srv)
360{
361 struct proxy *p = srv->proxy;
362 struct fwrr_group *grp;
363
364 if (srv->state == srv->prev_state &&
365 srv->eweight == srv->prev_eweight)
366 return;
367
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100368 if (!srv_is_usable(srv->state, srv->eweight))
369 goto out_update_state;
370
Willy Tarreaub625a082007-11-26 01:15:43 +0100371 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
372 /* server was already up */
373 goto out_update_backend;
374
375 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
376 grp->next_weight += srv->eweight;
377
378 if (srv->state & SRV_BACKUP) {
379 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
380 p->srv_bck++;
381
382 if (p->lbprm.fbck) {
383 /* we may have restored a backup server prior to fbck,
384 * in which case it should replace it.
385 */
386 struct server *srv2 = srv;
387 do {
388 srv2 = srv2->next;
389 } while (srv2 && (srv2 != p->lbprm.fbck));
390 if (srv2)
391 p->lbprm.fbck = srv;
392 }
393 } else {
394 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
395 p->srv_act++;
396 }
397
398 /* note that eweight cannot be 0 here */
399 fwrr_get_srv(srv);
400 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
401 fwrr_queue_srv(srv);
402
403out_update_backend:
404 /* check/update tot_used, tot_weight */
405 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100406 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100407 srv->prev_state = srv->state;
408 srv->prev_eweight = srv->eweight;
409}
410
411/* This function must be called after an update to server <srv>'s effective
412 * weight. It may be called after a state change too.
413 */
414static void fwrr_update_server_weight(struct server *srv)
415{
416 int old_state, new_state;
417 struct proxy *p = srv->proxy;
418 struct fwrr_group *grp;
419
420 if (srv->state == srv->prev_state &&
421 srv->eweight == srv->prev_eweight)
422 return;
423
424 /* If changing the server's weight changes its state, we simply apply
425 * the procedures we already have for status change. If the state
426 * remains down, the server is not in any tree, so it's as easy as
427 * updating its values. If the state remains up with different weights,
428 * there are some computations to perform to find a new place and
429 * possibly a new tree for this server.
430 */
431
432 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
433 new_state = srv_is_usable(srv->state, srv->eweight);
434
435 if (!old_state && !new_state) {
436 srv->prev_state = srv->state;
437 srv->prev_eweight = srv->eweight;
438 return;
439 }
440 else if (!old_state && new_state) {
441 fwrr_set_server_status_up(srv);
442 return;
443 }
444 else if (old_state && !new_state) {
445 fwrr_set_server_status_down(srv);
446 return;
447 }
448
449 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
450 grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight;
451
452 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
453 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
454
455 if (srv->lb_tree == grp->init) {
456 fwrr_dequeue_srv(srv);
457 fwrr_queue_by_weight(grp->init, srv);
458 }
459 else if (!srv->lb_tree) {
460 /* FIXME: server was down. This is not possible right now but
461 * may be needed soon for slowstart or graceful shutdown.
462 */
463 fwrr_dequeue_srv(srv);
464 fwrr_get_srv(srv);
465 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
466 fwrr_queue_srv(srv);
467 } else {
468 /* The server is either active or in the next queue. If it's
469 * still in the active queue and it has not consumed all of its
470 * places, let's adjust its next position.
471 */
472 fwrr_get_srv(srv);
473
474 if (srv->eweight > 0) {
475 int prev_next = srv->npos;
476 int step = grp->next_weight / srv->eweight;
477
478 srv->npos = srv->lpos + step;
479 srv->rweight = 0;
480
481 if (srv->npos > prev_next)
482 srv->npos = prev_next;
483 if (srv->npos < grp->curr_pos + 2)
484 srv->npos = grp->curr_pos + step;
485 } else {
486 /* push it into the next tree */
487 srv->npos = grp->curr_pos + grp->curr_weight;
488 }
489
490 fwrr_dequeue_srv(srv);
491 fwrr_queue_srv(srv);
492 }
493
494 update_backend_weight(p);
495 srv->prev_state = srv->state;
496 srv->prev_eweight = srv->eweight;
497}
498
499/* Remove a server from a tree. It must have previously been dequeued. This
500 * function is meant to be called when a server is going down or has its
501 * weight disabled.
502 */
503static inline void fwrr_remove_from_tree(struct server *s)
504{
505 s->lb_tree = NULL;
506}
507
508/* Queue a server in the weight tree <root>, assuming the weight is >0.
509 * We want to sort them by inverted weights, because we need to place
510 * heavy servers first in order to get a smooth distribution.
511 */
512static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
513{
514 /* eweight can be as high as 256*255 */
515 s->lb_node.key = BE_WEIGHT_SCALE*255 - s->eweight;
516 eb32_insert(root, &s->lb_node);
517 s->lb_tree = root;
518}
519
520/* This function is responsible for building the weight trees in case of fast
521 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
522 * ratio. Both active and backup groups are initialized.
523 */
524void fwrr_init_server_groups(struct proxy *p)
525{
526 struct server *srv;
527 struct eb_root init_head = EB_ROOT;
528
529 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
530 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
531 p->lbprm.update_server_eweight = fwrr_update_server_weight;
532
533 p->lbprm.wdiv = BE_WEIGHT_SCALE;
534 for (srv = p->srv; srv; srv = srv->next) {
535 srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE;
536 srv->prev_state = srv->state;
537 }
538
539 recount_servers(p);
540 update_backend_weight(p);
541
542 /* prepare the active servers group */
543 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
544 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
545 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
546 p->lbprm.fwrr.act.t1 = init_head;
547 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
548 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
549
550 /* prepare the backup servers group */
551 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
552 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
553 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
554 p->lbprm.fwrr.bck.t1 = init_head;
555 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
556 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
557
558 /* queue active and backup servers in two distinct groups */
559 for (srv = p->srv; srv; srv = srv->next) {
560 if (!srv_is_usable(srv->state, srv->eweight))
561 continue;
562 fwrr_queue_by_weight((srv->state & SRV_BACKUP) ?
563 p->lbprm.fwrr.bck.init :
564 p->lbprm.fwrr.act.init,
565 srv);
566 }
567}
568
569/* simply removes a server from a weight tree */
570static inline void fwrr_dequeue_srv(struct server *s)
571{
572 eb32_delete(&s->lb_node);
573}
574
575/* queues a server into the appropriate group and tree depending on its
576 * backup status, and ->npos. If the server is disabled, simply assign
577 * it to the NULL tree.
578 */
579static void fwrr_queue_srv(struct server *s)
580{
581 struct proxy *p = s->proxy;
582 struct fwrr_group *grp;
583
584 grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
585
586 /* Delay everything which does not fit into the window and everything
587 * which does not fit into the theorical new window.
588 */
589 if (!srv_is_usable(s->state, s->eweight)) {
590 fwrr_remove_from_tree(s);
591 }
592 else if (s->eweight <= 0 ||
593 s->npos >= 2 * grp->curr_weight ||
594 s->npos >= grp->curr_weight + grp->next_weight) {
595 /* put into next tree, and readjust npos in case we could
596 * finally take this back to current. */
597 s->npos -= grp->curr_weight;
598 fwrr_queue_by_weight(grp->next, s);
599 }
600 else {
601 /* FIXME: we want to multiply by a constant to avoid overrides
602 * after weight changes, but this can easily overflow on 32-bit
603 * values. We need to change this for a 64-bit tree, and keep
604 * the 65536 factor for optimal smoothness (both rweight and
605 * eweight are 16 bit entities). s->npos is bound by the number
606 * of servers times the maximum eweight (~= nsrv << 16).
607 */
608 //s->lb_node.key = grp->curr_weight * s->npos + s->rweight - s->eweight;
609 //s->lb_node.key = 65536 * s->npos + s->rweight - s->eweight;
610 s->lb_node.key = 16 * s->npos + (s->rweight - s->eweight) / 4096;
611 eb32i_insert(&grp->curr, &s->lb_node);
612 s->lb_tree = &grp->curr;
613 }
614}
615
616/* prepares a server when extracting it from the "init" tree */
617static inline void fwrr_get_srv_init(struct server *s)
618{
619 s->npos = s->rweight = 0;
620}
621
622/* prepares a server when extracting it from the "next" tree */
623static inline void fwrr_get_srv_next(struct server *s)
624{
625 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
626 &s->proxy->lbprm.fwrr.bck :
627 &s->proxy->lbprm.fwrr.act;
628
629 s->npos += grp->curr_weight;
630}
631
632/* prepares a server when it was marked down */
633static inline void fwrr_get_srv_down(struct server *s)
634{
635 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
636 &s->proxy->lbprm.fwrr.bck :
637 &s->proxy->lbprm.fwrr.act;
638
639 s->npos = grp->curr_pos;
640}
641
642/* prepares a server when extracting it from its tree */
643static void fwrr_get_srv(struct server *s)
644{
645 struct proxy *p = s->proxy;
646 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
647 &p->lbprm.fwrr.bck :
648 &p->lbprm.fwrr.act;
649
650 if (s->lb_tree == grp->init) {
651 fwrr_get_srv_init(s);
652 }
653 else if (s->lb_tree == grp->next) {
654 fwrr_get_srv_next(s);
655 }
656 else if (s->lb_tree == NULL) {
657 fwrr_get_srv_down(s);
658 }
659}
660
661/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
662 * when this happens, and "next" filled with servers sorted by weights.
663 */
664static inline void fwrr_switch_trees(struct fwrr_group *grp)
665{
666 struct eb_root *swap;
667 swap = grp->init;
668 grp->init = grp->next;
669 grp->next = swap;
670 grp->curr_weight = grp->next_weight;
671 grp->curr_pos = grp->curr_weight;
672}
673
674/* return next server from the current tree in FWRR group <grp>, or a server
675 * from the "init" tree if appropriate. If both trees are empty, return NULL.
676 */
677static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
678{
679 struct eb32_node *node;
680 struct server *s;
681
682 node = eb32_first(&grp->curr);
683 s = eb32_entry(node, struct server, lb_node);
684
685 if (!node || s->npos > grp->curr_pos) {
686 /* either we have no server left, or we have a hole */
687 struct eb32_node *node2;
688 node2 = eb32_first(grp->init);
689 if (node2) {
690 node = node2;
691 s = eb32_entry(node, struct server, lb_node);
692 fwrr_get_srv_init(s);
693 if (s->eweight == 0) /* FIXME: is it possible at all ? */
694 node = NULL;
695 }
696 }
697 if (node)
698 return s;
699 else
700 return NULL;
701}
702
703/* Computes next position of server <s> in the group. It is mandatory for <s>
704 * to have a non-zero, positive eweight.
705*/
706static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
707{
708 if (!s->npos) {
709 /* first time ever for this server */
710 s->lpos = grp->curr_pos;
711 s->npos = grp->curr_pos + grp->next_weight / s->eweight;
712 s->rweight += grp->next_weight % s->eweight;
713
714 if (s->rweight >= s->eweight) {
715 s->rweight -= s->eweight;
716 s->npos++;
717 }
718 } else {
719 s->lpos = s->npos;
720 s->npos += grp->next_weight / s->eweight;
721 s->rweight += grp->next_weight % s->eweight;
722
723 if (s->rweight >= s->eweight) {
724 s->rweight -= s->eweight;
725 s->npos++;
726 }
727 }
728}
729
730/* Return next server from the current tree in backend <p>, or a server from
731 * the init tree if appropriate. If both trees are empty, return NULL.
732 * Saturated servers are skipped and requeued.
733 */
734static struct server *fwrr_get_next_server(struct proxy *p)
735{
736 struct server *srv;
737 struct fwrr_group *grp;
738 struct server *full;
739 int switched;
740
741 if (p->srv_act)
742 grp = &p->lbprm.fwrr.act;
743 else if (p->lbprm.fbck)
744 return p->lbprm.fbck;
745 else if (p->srv_bck)
746 grp = &p->lbprm.fwrr.bck;
747 else
748 return NULL;
749
750 switched = 0;
751 full = NULL; /* NULL-terminated list of saturated servers */
752 while (1) {
753 /* if we see an empty group, let's first try to collect weights
754 * which might have recently changed.
755 */
756 if (!grp->curr_weight)
757 grp->curr_pos = grp->curr_weight = grp->next_weight;
758
759 /* get first server from the "current" tree. When the end of
760 * the tree is reached, we may have to switch, but only once.
761 */
762 while (1) {
763 srv = fwrr_get_server_from_group(grp);
764 if (srv)
765 break;
766 if (switched)
767 goto requeue_servers;
768 switched = 1;
769 fwrr_switch_trees(grp);
770
771 }
772
773 /* OK, we have a server. However, it may be saturated, in which
774 * case we don't want to reconsider it for now. We'll update
775 * its position and dequeue it anyway, so that we can move it
776 * to a better place afterwards.
777 */
778 fwrr_update_position(grp, srv);
779 fwrr_dequeue_srv(srv);
780 grp->curr_pos++;
781 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv))
782 break;
783
784 /* the server is saturated, let's chain it for later reinsertion */
785 srv->next_full = full;
786 full = srv;
787 }
788
789 /* OK, we got the best server, let's update it */
790 fwrr_queue_srv(srv);
791
792 requeue_servers:
793 if (unlikely(full)) {
794 if (switched) {
795 /* the tree has switched, requeue all extracted servers
796 * into "init", because their place was lost, and only
797 * their weight matters.
798 */
799 do {
800 fwrr_queue_by_weight(grp->init, full);
801 full = full->next_full;
802 } while (full);
803 } else {
804 /* requeue all extracted servers just as if they were consumed
805 * so that they regain their expected place.
806 */
807 do {
808 fwrr_queue_srv(full);
809 full = full->next_full;
810 } while (full);
811 }
812 }
813 return srv;
814}
815
Willy Tarreau01732802007-11-01 22:48:15 +0100816/*
817 * This function tries to find a running server for the proxy <px> following
818 * the URL parameter hash method. It looks for a specific parameter in the
819 * URL and hashes it to compute the server ID. This is useful to optimize
820 * performance by avoiding bounces between servers in contexts where sessions
821 * are shared but cookies are not usable. If the parameter is not found, NULL
822 * is returned. If any server is found, it will be returned. If no valid server
823 * is found, NULL is returned.
824 *
825 */
826struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
827{
828 unsigned long hash = 0;
829 char *p;
830 int plen;
831
Willy Tarreau20697042007-11-15 23:26:18 +0100832 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100833 return NULL;
834
Willy Tarreau20697042007-11-15 23:26:18 +0100835 if (px->lbprm.map.state & PR_MAP_RECALC)
836 recalc_server_map(px);
837
Willy Tarreau01732802007-11-01 22:48:15 +0100838 p = memchr(uri, '?', uri_len);
839 if (!p)
840 return NULL;
841 p++;
842
843 uri_len -= (p - uri);
844 plen = px->url_param_len;
845
846 if (uri_len <= plen)
847 return NULL;
848
849 while (uri_len > plen) {
850 /* Look for the parameter name followed by an equal symbol */
851 if (p[plen] == '=') {
852 /* skip the equal symbol */
853 uri = p;
854 p += plen + 1;
855 uri_len -= plen + 1;
856 if (memcmp(uri, px->url_param_name, plen) == 0) {
857 /* OK, we have the parameter here at <uri>, and
858 * the value after the equal sign, at <p>
859 */
860 while (uri_len && *p != '&') {
861 hash = *p + (hash << 6) + (hash << 16) - hash;
862 uri_len--;
863 p++;
864 }
Willy Tarreau20697042007-11-15 23:26:18 +0100865 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau01732802007-11-01 22:48:15 +0100866 }
867 }
868
869 /* skip to next parameter */
870 uri = p;
871 p = memchr(uri, '&', uri_len);
872 if (!p)
873 return NULL;
874 p++;
875 uri_len -= (p - uri);
876 }
877 return NULL;
878}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879
880/*
881 * This function marks the session as 'assigned' in direct or dispatch modes,
882 * or tries to assign one in balance mode, according to the algorithm. It does
883 * nothing if the session had already been assigned a server.
884 *
885 * It may return :
886 * SRV_STATUS_OK if everything is OK. s->srv will be valid.
887 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
888 * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL.
889 * SRV_STATUS_INTERNAL for other unrecoverable errors.
890 *
891 * Upon successful return, the session flag SN_ASSIGNED to indicate that it does
892 * not need to be called anymore. This usually means that s->srv can be trusted
893 * in balance and direct modes. This flag is not cleared, so it's to the caller
894 * to clear it if required (eg: redispatch).
895 *
896 */
897
898int assign_server(struct session *s)
899{
900#ifdef DEBUG_FULL
901 fprintf(stderr,"assign_server : s=%p\n",s);
902#endif
903
904 if (s->pend_pos)
905 return SRV_STATUS_INTERNAL;
906
907 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau31682232007-11-29 15:38:04 +0100908 if (s->be->lbprm.algo & BE_LB_ALGO) {
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100909 int len;
910
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100911 if (s->flags & SN_DIRECT) {
912 s->flags |= SN_ASSIGNED;
913 return SRV_STATUS_OK;
914 }
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100915
Willy Tarreaub625a082007-11-26 01:15:43 +0100916 if (!s->be->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 return SRV_STATUS_NOSRV;
918
Willy Tarreau31682232007-11-29 15:38:04 +0100919 switch (s->be->lbprm.algo & BE_LB_ALGO) {
920 case BE_LB_ALGO_RR:
Willy Tarreaub625a082007-11-26 01:15:43 +0100921 s->srv = fwrr_get_next_server(s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 if (!s->srv)
923 return SRV_STATUS_FULL;
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100924 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100925 case BE_LB_ALGO_SH:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 if (s->cli_addr.ss_family == AF_INET)
927 len = 4;
928 else if (s->cli_addr.ss_family == AF_INET6)
929 len = 16;
930 else /* unknown IP family */
931 return SRV_STATUS_INTERNAL;
932
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200933 s->srv = get_server_sh(s->be,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
935 len);
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100936 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100937 case BE_LB_ALGO_UH:
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200938 /* URI hashing */
939 s->srv = get_server_uh(s->be,
940 s->txn.req.sol + s->txn.req.sl.rq.u,
941 s->txn.req.sl.rq.u_l);
Willy Tarreau01732802007-11-01 22:48:15 +0100942 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100943 case BE_LB_ALGO_PH:
Willy Tarreau01732802007-11-01 22:48:15 +0100944 /* URL Parameter hashing */
945 s->srv = get_server_ph(s->be,
946 s->txn.req.sol + s->txn.req.sl.rq.u,
947 s->txn.req.sl.rq.u_l);
948 if (!s->srv) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100949 /* parameter not found, fall back to round robin on the map */
Willy Tarreau01732802007-11-01 22:48:15 +0100950 s->srv = get_server_rr_with_conns(s->be);
951 if (!s->srv)
952 return SRV_STATUS_FULL;
953 }
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100954 break;
955 default:
956 /* unknown balancing algorithm */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 return SRV_STATUS_INTERNAL;
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100958 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100960 else if (s->be->options & PR_O_HTTP_PROXY) {
961 if (!s->srv_addr.sin_addr.s_addr)
962 return SRV_STATUS_NOSRV;
963 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200964 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100965 !(s->fe->options & PR_O_TRANSP)) {
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100966 return SRV_STATUS_NOSRV;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100967 }
968 s->flags |= SN_ASSIGNED;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969 }
970 return SRV_STATUS_OK;
971}
972
973
974/*
975 * This function assigns a server address to a session, and sets SN_ADDR_SET.
976 * The address is taken from the currently assigned server, or from the
977 * dispatch or transparent address.
978 *
979 * It may return :
980 * SRV_STATUS_OK if everything is OK.
981 * SRV_STATUS_INTERNAL for other unrecoverable errors.
982 *
983 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
984 * not cleared, so it's to the caller to clear it if required.
985 *
986 */
987int assign_server_address(struct session *s)
988{
989#ifdef DEBUG_FULL
990 fprintf(stderr,"assign_server_address : s=%p\n",s);
991#endif
992
Willy Tarreau31682232007-11-29 15:38:04 +0100993 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994 /* A server is necessarily known for this session */
995 if (!(s->flags & SN_ASSIGNED))
996 return SRV_STATUS_INTERNAL;
997
998 s->srv_addr = s->srv->addr;
999
1000 /* if this server remaps proxied ports, we'll use
1001 * the port the client connected to with an offset. */
1002 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau14c8aac2007-05-08 19:46:30 +02001003 if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
1004 get_frt_addr(s);
1005 if (s->frt_addr.ss_family == AF_INET) {
1006 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1007 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
1008 } else {
1009 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1010 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
1011 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 }
1013 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001014 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001016 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001018 else if (s->fe->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 /* in transparent mode, use the original dest addr if no dispatch specified */
1020 socklen_t salen = sizeof(s->srv_addr);
1021
1022 if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) {
1023 qfprintf(stderr, "Cannot get original server address.\n");
1024 return SRV_STATUS_INTERNAL;
1025 }
1026 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001027 else if (s->be->options & PR_O_HTTP_PROXY) {
1028 /* If HTTP PROXY option is set, then server is already assigned
1029 * during incoming client request parsing. */
1030 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +01001031 else {
1032 /* no server and no LB algorithm ! */
1033 return SRV_STATUS_INTERNAL;
1034 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001035
1036 s->flags |= SN_ADDR_SET;
1037 return SRV_STATUS_OK;
1038}
1039
1040
1041/* This function assigns a server to session <s> if required, and can add the
1042 * connection to either the assigned server's queue or to the proxy's queue.
1043 *
1044 * Returns :
1045 *
1046 * SRV_STATUS_OK if everything is OK.
1047 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
1048 * SRV_STATUS_QUEUED if the connection has been queued.
1049 * SRV_STATUS_FULL if the server(s) is/are saturated and the
1050 * connection could not be queued.
1051 * SRV_STATUS_INTERNAL for other unrecoverable errors.
1052 *
1053 */
1054int assign_server_and_queue(struct session *s)
1055{
1056 struct pendconn *p;
1057 int err;
1058
1059 if (s->pend_pos)
1060 return SRV_STATUS_INTERNAL;
1061
1062 if (s->flags & SN_ASSIGNED) {
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001063 if (s->srv && s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) {
1064 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1065 s->srv = NULL;
1066 http_flush_cookie_flags(&s->txn);
1067 } else {
1068 /* a server does not need to be assigned, perhaps because we're in
1069 * direct mode, or in dispatch or transparent modes where the server
1070 * is not needed.
1071 */
1072 if (s->srv &&
1073 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
1074 p = pendconn_add(s);
1075 if (p)
1076 return SRV_STATUS_QUEUED;
1077 else
1078 return SRV_STATUS_FULL;
1079 }
1080 return SRV_STATUS_OK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001082 }
1083
1084 /* a server needs to be assigned */
1085 err = assign_server(s);
1086 switch (err) {
1087 case SRV_STATUS_OK:
1088 /* in balance mode, we might have servers with connection limits */
1089 if (s->srv &&
1090 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
1091 p = pendconn_add(s);
1092 if (p)
1093 return SRV_STATUS_QUEUED;
1094 else
1095 return SRV_STATUS_FULL;
1096 }
1097 return SRV_STATUS_OK;
1098
1099 case SRV_STATUS_FULL:
1100 /* queue this session into the proxy's queue */
1101 p = pendconn_add(s);
1102 if (p)
1103 return SRV_STATUS_QUEUED;
1104 else
1105 return SRV_STATUS_FULL;
1106
1107 case SRV_STATUS_NOSRV:
1108 case SRV_STATUS_INTERNAL:
1109 return err;
1110 default:
1111 return SRV_STATUS_INTERNAL;
1112 }
1113}
1114
1115
1116/*
1117 * This function initiates a connection to the server assigned to this session
1118 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
1119 * It can return one of :
1120 * - SN_ERR_NONE if everything's OK
1121 * - SN_ERR_SRVTO if there are no more servers
1122 * - SN_ERR_SRVCL if the connection was refused by the server
1123 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1124 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1125 * - SN_ERR_INTERNAL for any other purely internal errors
1126 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1127 */
1128int connect_server(struct session *s)
1129{
1130 int fd, err;
1131
1132 if (!(s->flags & SN_ADDR_SET)) {
1133 err = assign_server_address(s);
1134 if (err != SRV_STATUS_OK)
1135 return SN_ERR_INTERNAL;
1136 }
1137
1138 if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
1139 qfprintf(stderr, "Cannot get a server socket.\n");
1140
1141 if (errno == ENFILE)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001142 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001144 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001145 else if (errno == EMFILE)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001146 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001148 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 else if (errno == ENOBUFS || errno == ENOMEM)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001150 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001152 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 /* this is a resource error */
1154 return SN_ERR_RESOURCE;
1155 }
1156
1157 if (fd >= global.maxsock) {
1158 /* do not log anything there, it's a normal condition when this option
1159 * is used to serialize connections to a server !
1160 */
1161 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
1162 close(fd);
1163 return SN_ERR_PRXCOND; /* it is a configuration limit */
1164 }
1165
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001166#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001167 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001168 /* TCP splicing supported by both FE and BE */
1169 tcp_splice_initfd(s->cli_fd, fd);
1170 }
1171#endif
1172
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
1174 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
1175 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
1176 close(fd);
1177 return SN_ERR_INTERNAL;
1178 }
1179
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001180 if (s->be->options & PR_O_TCP_SRV_KA)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
1182
Alexandre Cassen87ea5482007-10-11 20:48:58 +02001183 if (s->be->options & PR_O_TCP_NOLING)
1184 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1185
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186 /* allow specific binding :
1187 * - server-specific at first
1188 * - proxy-specific next
1189 */
1190 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
1191 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
1192 if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) {
1193 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001194 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001196 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001198 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199 return SN_ERR_RESOURCE;
1200 }
Willy Tarreau77074d52006-11-12 23:57:19 +01001201#ifdef CONFIG_HAP_CTTPROXY
1202 if (s->srv->state & SRV_TPROXY_MASK) {
1203 struct in_tproxy itp1, itp2;
1204 memset(&itp1, 0, sizeof(itp1));
1205
1206 itp1.op = TPROXY_ASSIGN;
1207 switch (s->srv->state & SRV_TPROXY_MASK) {
1208 case SRV_TPROXY_ADDR:
1209 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
1210 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
1211 break;
1212 case SRV_TPROXY_CLI:
1213 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
1214 /* fall through */
1215 case SRV_TPROXY_CIP:
1216 /* FIXME: what can we do if the client connects in IPv6 ? */
1217 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
1218 break;
1219 }
1220
1221 /* set connect flag on socket */
1222 itp2.op = TPROXY_FLAGS;
1223 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
1224
1225 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
1226 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
1227 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001228 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001229 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001230 send_log(s->be, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +01001231 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001232 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001233 return SN_ERR_RESOURCE;
1234 }
1235 }
1236#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001238 else if (s->be->options & PR_O_BIND_SRC) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001239 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001240 if (bind(fd, (struct sockaddr *)&s->be->source_addr, sizeof(s->be->source_addr)) == -1) {
1241 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->be->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001243 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001245 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246 return SN_ERR_RESOURCE;
1247 }
Willy Tarreau77074d52006-11-12 23:57:19 +01001248#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001249 if (s->be->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +01001250 struct in_tproxy itp1, itp2;
1251 memset(&itp1, 0, sizeof(itp1));
1252
1253 itp1.op = TPROXY_ASSIGN;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001254 switch (s->be->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +01001255 case PR_O_TPXY_ADDR:
1256 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
1257 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
1258 break;
1259 case PR_O_TPXY_CLI:
1260 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
1261 /* fall through */
1262 case PR_O_TPXY_CIP:
1263 /* FIXME: what can we do if the client connects in IPv6 ? */
1264 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
1265 break;
1266 }
1267
1268 /* set connect flag on socket */
1269 itp2.op = TPROXY_FLAGS;
1270 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
1271
1272 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
1273 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
1274 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001275 s->be->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001276 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001277 send_log(s->be, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +01001278 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001279 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001280 return SN_ERR_RESOURCE;
1281 }
1282 }
1283#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284 }
1285
1286 if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) &&
1287 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
1288
1289 if (errno == EAGAIN || errno == EADDRINUSE) {
1290 char *msg;
1291 if (errno == EAGAIN) /* no free ports left, try again later */
1292 msg = "no free ports";
1293 else
1294 msg = "local address already in use";
1295
1296 qfprintf(stderr,"Cannot connect: %s.\n",msg);
1297 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001298 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 "Connect() failed for server %s/%s: %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001300 s->be->id, s->srv->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 return SN_ERR_RESOURCE;
1302 } else if (errno == ETIMEDOUT) {
1303 //qfprintf(stderr,"Connect(): ETIMEDOUT");
1304 close(fd);
1305 return SN_ERR_SRVTO;
1306 } else {
1307 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
1308 //qfprintf(stderr,"Connect(): %d", errno);
1309 close(fd);
1310 return SN_ERR_SRVCL;
1311 }
1312 }
1313
1314 fdtab[fd].owner = s->task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001315 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaud7971282006-07-29 18:36:34 +02001316 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
Willy Tarreau54469402006-07-29 16:59:06 +02001317 fdtab[fd].cb[DIR_RD].b = s->rep;
Willy Tarreauf8306d52006-07-29 19:01:31 +02001318 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
Willy Tarreau54469402006-07-29 16:59:06 +02001319 fdtab[fd].cb[DIR_WR].b = s->req;
Willy Tarreaue94ebd02007-10-09 17:14:37 +02001320
1321 fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr;
1322 fdtab[fd].peerlen = sizeof(s->srv_addr);
1323
Willy Tarreauf161a342007-04-08 16:59:42 +02001324 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001325
1326 fd_insert(fd);
1327 if (s->srv) {
1328 s->srv->cur_sess++;
1329 if (s->srv->cur_sess > s->srv->cur_sess_max)
1330 s->srv->cur_sess_max = s->srv->cur_sess;
1331 }
1332
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001333 if (!tv_add_ifset(&s->req->cex, &now, &s->be->contimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02001334 tv_eternity(&s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335 return SN_ERR_NONE; /* connection is OK */
1336}
1337
1338
1339/*
1340 * This function checks the retry count during the connect() job.
1341 * It updates the session's srv_state and retries, so that the caller knows
1342 * what it has to do. It uses the last connection error to set the log when
1343 * it expires. It returns 1 when it has expired, and 0 otherwise.
1344 */
1345int srv_count_retry_down(struct session *t, int conn_err)
1346{
1347 /* we are in front of a retryable error */
1348 t->conn_retries--;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001349 if (t->srv)
1350 t->srv->retries++;
1351 t->be->retries++;
1352
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353 if (t->conn_retries < 0) {
1354 /* if not retryable anymore, let's abort */
Willy Tarreaud7971282006-07-29 18:36:34 +02001355 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356 srv_close_with_err(t, conn_err, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001357 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001358 if (t->srv)
1359 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001360 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001361
1362 /* We used to have a free connection slot. Since we'll never use it,
1363 * we have to inform the server that it may be used by another session.
1364 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001365 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001366 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367 return 1;
1368 }
1369 return 0;
1370}
1371
1372
1373/*
1374 * This function performs the retryable part of the connect() job.
1375 * It updates the session's srv_state and retries, so that the caller knows
1376 * what it has to do. It returns 1 when it breaks out of the loop, or 0 if
1377 * it needs to redispatch.
1378 */
1379int srv_retryable_connect(struct session *t)
1380{
1381 int conn_err;
1382
1383 /* This loop ensures that we stop before the last retry in case of a
1384 * redispatchable server.
1385 */
1386 do {
1387 /* initiate a connection to the server */
1388 conn_err = connect_server(t);
1389 switch (conn_err) {
1390
1391 case SN_ERR_NONE:
1392 //fprintf(stderr,"0: c=%d, s=%d\n", c, s);
1393 t->srv_state = SV_STCONN;
1394 return 1;
1395
1396 case SN_ERR_INTERNAL:
Willy Tarreaud7971282006-07-29 18:36:34 +02001397 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001399 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001400 if (t->srv)
1401 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001402 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001404 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001405 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406 return 1;
1407 }
1408 /* ensure that we have enough retries left */
1409 if (srv_count_retry_down(t, conn_err)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410 return 1;
1411 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001412 } while (t->srv == NULL || t->conn_retries > 0 || !(t->be->options & PR_O_REDISP));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413
1414 /* We're on our last chance, and the REDISP option was specified.
1415 * We will ignore cookie and force to balance or use the dispatcher.
1416 */
1417 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001418 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001419 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420
1421 if (t->srv)
1422 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001423 t->be->redispatches++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424
1425 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1426 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreau3d300592007-03-18 18:34:41 +01001427 http_flush_cookie_flags(&t->txn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428 return 0;
1429}
1430
1431
1432/* This function performs the "redispatch" part of a connection attempt. It
1433 * will assign a server if required, queue the connection if required, and
1434 * handle errors that might arise at this level. It can change the server
1435 * state. It will return 1 if it encounters an error, switches the server
1436 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1437 * that the connection is ready to use.
1438 */
1439
1440int srv_redispatch_connect(struct session *t)
1441{
1442 int conn_err;
1443
1444 /* We know that we don't have any connection pending, so we will
1445 * try to get a new one, and wait in this state if it's queued
1446 */
1447 conn_err = assign_server_and_queue(t);
1448 switch (conn_err) {
1449 case SRV_STATUS_OK:
1450 break;
1451
1452 case SRV_STATUS_NOSRV:
1453 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaud7971282006-07-29 18:36:34 +02001454 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001456 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001457 if (t->srv)
1458 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001459 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460
1461 return 1;
1462
1463 case SRV_STATUS_QUEUED:
1464 /* FIXME-20060503 : we should use the queue timeout instead */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001465 if (!tv_add_ifset(&t->req->cex, &now, &t->be->contimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02001466 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001467 t->srv_state = SV_STIDLE;
1468 /* do nothing else and do not wake any other session up */
1469 return 1;
1470
1471 case SRV_STATUS_FULL:
1472 case SRV_STATUS_INTERNAL:
1473 default:
Willy Tarreaud7971282006-07-29 18:36:34 +02001474 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001475 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001476 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001477 if (t->srv)
1478 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001479 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001480
1481 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001482 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001483 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484 return 1;
1485 }
1486 /* if we get here, it's because we got SRV_STATUS_OK, which also
1487 * means that the connection has not been queued.
1488 */
1489 return 0;
1490}
1491
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001492int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001493 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001494 return px->down_time;
1495
1496 return now.tv_sec - px->last_change + px->down_time;
1497}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001499/* This function parses a "balance" statement in a backend section describing
1500 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1501 * returns -1, it may write an error message into ther <err> buffer, for at
1502 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1503 * written. The function must be called with <args> pointing to the first word
1504 * after "balance".
1505 */
1506int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1507{
1508 if (!*(args[0])) {
1509 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001510 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1511 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001512 return 0;
1513 }
1514
1515 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001516 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1517 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001518 }
1519 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001520 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1521 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001522 }
1523 else if (!strcmp(args[0], "uri")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001524 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1525 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001526 }
Willy Tarreau01732802007-11-01 22:48:15 +01001527 else if (!strcmp(args[0], "url_param")) {
1528 if (!*args[1]) {
1529 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1530 return -1;
1531 }
Willy Tarreau31682232007-11-29 15:38:04 +01001532 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1533 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreau01732802007-11-01 22:48:15 +01001534 if (curproxy->url_param_name)
1535 free(curproxy->url_param_name);
1536 curproxy->url_param_name = strdup(args[1]);
1537 curproxy->url_param_len = strlen(args[1]);
1538 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001539 else {
Willy Tarreau01732802007-11-01 22:48:15 +01001540 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source', 'uri' and 'url_param' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001541 return -1;
1542 }
1543 return 0;
1544}
1545
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001546
1547/************************************************************************/
1548/* All supported keywords must be declared here. */
1549/************************************************************************/
1550
1551/* set test->i to the number of enabled servers on the proxy */
1552static int
1553acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1554 struct acl_expr *expr, struct acl_test *test)
1555{
1556 test->flags = ACL_TEST_F_VOL_TEST;
1557 if (expr->arg_len) {
1558 /* another proxy was designated, we must look for it */
1559 for (px = proxy; px; px = px->next)
1560 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1561 break;
1562 }
1563 if (!px)
1564 return 0;
1565
1566 if (px->srv_act)
1567 test->i = px->srv_act;
1568 else if (px->lbprm.fbck)
1569 test->i = 1;
1570 else
1571 test->i = px->srv_bck;
1572
1573 return 1;
1574}
1575
1576
1577/* Note: must not be declared <const> as its list will be overwritten */
1578static struct acl_kw_list acl_kws = {{ },{
1579 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int },
1580 { NULL, NULL, NULL, NULL },
1581}};
1582
1583
1584__attribute__((constructor))
1585static void __backend_init(void)
1586{
1587 acl_register_keywords(&acl_kws);
1588}
1589
1590
Willy Tarreaubaaee002006-06-26 02:48:02 +02001591/*
1592 * Local variables:
1593 * c-indent-level: 8
1594 * c-basic-offset: 8
1595 * End:
1596 */