blob: 1c544749f058e6d8d5960d23b279e2f6fe295cae [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
25#include <types/buffers.h>
26#include <types/global.h>
27#include <types/polling.h>
28#include <types/proxy.h>
29#include <types/server.h>
30#include <types/session.h>
31
32#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/fd.h>
Willy Tarreau80587432006-12-24 17:47:20 +010035#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/log.h>
37#include <proto/proto_http.h>
38#include <proto/queue.h>
39#include <proto/stream_sock.h>
40#include <proto/task.h>
41
Willy Tarreau77074d52006-11-12 23:57:19 +010042#ifdef CONFIG_HAP_CTTPROXY
43#include <import/ip_tproxy.h>
44#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau6d1a9882007-01-07 02:03:04 +010046#ifdef CONFIG_HAP_TCPSPLICE
47#include <libtcpsplice.h>
48#endif
49
Willy Tarreaub625a082007-11-26 01:15:43 +010050static inline void fwrr_remove_from_tree(struct server *s);
51static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
52static inline void fwrr_dequeue_srv(struct server *s);
53static void fwrr_get_srv(struct server *s);
54static void fwrr_queue_srv(struct server *s);
55
56/* This function returns non-zero if a server with the given weight and state
57 * is usable for LB, otherwise zero.
58 */
59static inline int srv_is_usable(int state, int weight)
60{
61 if (!weight)
62 return 0;
Willy Tarreau48494c02007-11-30 10:41:39 +010063 if (state & SRV_GOINGDOWN)
64 return 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010065 if (!(state & SRV_RUNNING))
66 return 0;
67 return 1;
68}
69
Willy Tarreaubaaee002006-06-26 02:48:02 +020070/*
71 * This function recounts the number of usable active and backup servers for
72 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010073 * This function also recomputes the total active and backup weights. However,
74 * it does nout update tot_weight nor tot_used. Use update_backend_weight() for
75 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 */
Willy Tarreaub625a082007-11-26 01:15:43 +010077static void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020078{
79 struct server *srv;
80
Willy Tarreau20697042007-11-15 23:26:18 +010081 px->srv_act = px->srv_bck = 0;
82 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010083 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010085 if (!srv_is_usable(srv->state, srv->eweight))
86 continue;
87
88 if (srv->state & SRV_BACKUP) {
89 if (!px->srv_bck &&
Willy Tarreau31682232007-11-29 15:38:04 +010090 !(px->lbprm.algo & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010091 px->lbprm.fbck = srv;
92 px->srv_bck++;
93 px->lbprm.tot_wbck += srv->eweight;
94 } else {
95 px->srv_act++;
96 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 }
98 }
Willy Tarreaub625a082007-11-26 01:15:43 +010099}
Willy Tarreau20697042007-11-15 23:26:18 +0100100
Willy Tarreaub625a082007-11-26 01:15:43 +0100101/* This function simply updates the backend's tot_weight and tot_used values
102 * after servers weights have been updated. It is designed to be used after
103 * recount_servers() or equivalent.
104 */
105static void update_backend_weight(struct proxy *px)
106{
Willy Tarreau20697042007-11-15 23:26:18 +0100107 if (px->srv_act) {
108 px->lbprm.tot_weight = px->lbprm.tot_wact;
109 px->lbprm.tot_used = px->srv_act;
110 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100111 else if (px->lbprm.fbck) {
112 /* use only the first backup server */
113 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
114 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100115 }
116 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100117 px->lbprm.tot_weight = px->lbprm.tot_wbck;
118 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100119 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100120}
121
122/* this function updates the map according to server <srv>'s new state */
123static void map_set_server_status_down(struct server *srv)
124{
125 struct proxy *p = srv->proxy;
126
127 if (srv->state == srv->prev_state &&
128 srv->eweight == srv->prev_eweight)
129 return;
130
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100131 if (srv_is_usable(srv->state, srv->eweight))
132 goto out_update_state;
133
Willy Tarreaub625a082007-11-26 01:15:43 +0100134 /* FIXME: could be optimized since we know what changed */
135 recount_servers(p);
136 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100137 p->lbprm.map.state |= PR_MAP_RECALC;
138 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100139 srv->prev_state = srv->state;
140 srv->prev_eweight = srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141}
142
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100143/* This function updates the map according to server <srv>'s new state */
Willy Tarreaub625a082007-11-26 01:15:43 +0100144static void map_set_server_status_up(struct server *srv)
145{
146 struct proxy *p = srv->proxy;
147
148 if (srv->state == srv->prev_state &&
149 srv->eweight == srv->prev_eweight)
150 return;
151
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100152 if (!srv_is_usable(srv->state, srv->eweight))
153 goto out_update_state;
154
Willy Tarreaub625a082007-11-26 01:15:43 +0100155 /* FIXME: could be optimized since we know what changed */
156 recount_servers(p);
157 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100158 p->lbprm.map.state |= PR_MAP_RECALC;
159 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100160 srv->prev_state = srv->state;
161 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100162}
163
Willy Tarreau20697042007-11-15 23:26:18 +0100164/* This function recomputes the server map for proxy px. It relies on
165 * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be
166 * called after recount_servers(). It also expects px->lbprm.map.srv
167 * to be allocated with the largest size needed. It updates tot_weight.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 */
169void recalc_server_map(struct proxy *px)
170{
171 int o, tot, flag;
172 struct server *cur, *best;
173
Willy Tarreau20697042007-11-15 23:26:18 +0100174 switch (px->lbprm.tot_used) {
175 case 0: /* no server */
176 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177 return;
Willy Tarreau20697042007-11-15 23:26:18 +0100178 case 1: /* only one server, just fill first entry */
179 tot = 1;
180 break;
181 default:
182 tot = px->lbprm.tot_weight;
183 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 }
185
Willy Tarreau20697042007-11-15 23:26:18 +0100186 /* here we *know* that we have some servers */
187 if (px->srv_act)
188 flag = SRV_RUNNING;
189 else
190 flag = SRV_RUNNING | SRV_BACKUP;
191
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192 /* this algorithm gives priority to the first server, which means that
193 * it will respect the declaration order for equivalent weights, and
194 * that whatever the weights, the first server called will always be
Willy Tarreau20697042007-11-15 23:26:18 +0100195 * the first declared. This is an important asumption for the backup
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 * case, where we want the first server only.
197 */
198 for (cur = px->srv; cur; cur = cur->next)
199 cur->wscore = 0;
200
201 for (o = 0; o < tot; o++) {
202 int max = 0;
203 best = NULL;
204 for (cur = px->srv; cur; cur = cur->next) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100205 if (flag == (cur->state &
206 (SRV_RUNNING | SRV_GOINGDOWN | SRV_BACKUP))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207 int v;
208
209 /* If we are forced to return only one server, we don't want to
210 * go further, because we would return the wrong one due to
211 * divide overflow.
212 */
213 if (tot == 1) {
214 best = cur;
Willy Tarreau20697042007-11-15 23:26:18 +0100215 /* note that best->wscore will be wrong but we don't care */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216 break;
217 }
218
Willy Tarreau417fae02007-03-25 21:16:40 +0200219 cur->wscore += cur->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220 v = (cur->wscore + tot) / tot; /* result between 0 and 3 */
221 if (best == NULL || v > max) {
222 max = v;
223 best = cur;
224 }
225 }
226 }
Willy Tarreau20697042007-11-15 23:26:18 +0100227 px->lbprm.map.srv[o] = best;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228 best->wscore -= tot;
229 }
Willy Tarreau20697042007-11-15 23:26:18 +0100230 px->lbprm.map.state &= ~PR_MAP_RECALC;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231}
232
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100233/* This function is responsible of building the server MAP for map-based LB
234 * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the
235 * weights if applicable. It should be called only once per proxy, at config
236 * time.
237 */
238void init_server_map(struct proxy *p)
239{
240 struct server *srv;
241 int pgcd;
242 int act, bck;
243
Willy Tarreaub625a082007-11-26 01:15:43 +0100244 p->lbprm.set_server_status_up = map_set_server_status_up;
245 p->lbprm.set_server_status_down = map_set_server_status_down;
246 p->lbprm.update_server_eweight = NULL;
247
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100248 if (!p->srv)
249 return;
250
251 /* We will factor the weights to reduce the table,
252 * using Euclide's largest common divisor algorithm
253 */
254 pgcd = p->srv->uweight;
255 for (srv = p->srv->next; srv && 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 }
262 }
263
264 /* It is sometimes useful to know what factor to apply
265 * to the backend's effective weight to know its real
266 * weight.
267 */
268 p->lbprm.wmult = pgcd;
269
270 act = bck = 0;
271 for (srv = p->srv; srv; srv = srv->next) {
272 srv->eweight = srv->uweight / pgcd;
Willy Tarreaub625a082007-11-26 01:15:43 +0100273 srv->prev_eweight = srv->eweight;
274 srv->prev_state = srv->state;
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100275 if (srv->state & SRV_BACKUP)
276 bck += srv->eweight;
277 else
278 act += srv->eweight;
279 }
280
281 /* this is the largest map we will ever need for this servers list */
282 if (act < bck)
283 act = bck;
284
285 p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *));
286 /* recounts servers and their weights */
287 p->lbprm.map.state = PR_MAP_RECALC;
288 recount_servers(p);
Willy Tarreaub625a082007-11-26 01:15:43 +0100289 update_backend_weight(p);
Willy Tarreau5dc2fa62007-11-19 19:10:18 +0100290 recalc_server_map(p);
291}
292
Willy Tarreaub625a082007-11-26 01:15:43 +0100293/* This function updates the server trees according to server <srv>'s new
294 * state. It should be called when server <srv>'s status changes to down.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100295 * It is not important whether the server was already down or not. It is not
296 * important either that the new state is completely down (the caller may not
297 * know all the variables of a server's state).
Willy Tarreaub625a082007-11-26 01:15:43 +0100298 */
299static void fwrr_set_server_status_down(struct server *srv)
300{
301 struct proxy *p = srv->proxy;
302 struct fwrr_group *grp;
303
304 if (srv->state == srv->prev_state &&
305 srv->eweight == srv->prev_eweight)
306 return;
307
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100308 if (srv_is_usable(srv->state, srv->eweight))
309 goto out_update_state;
310
Willy Tarreaub625a082007-11-26 01:15:43 +0100311 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
312 /* server was already down */
313 goto out_update_backend;
314
315 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
316 grp->next_weight -= srv->prev_eweight;
317
318 if (srv->state & SRV_BACKUP) {
319 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
320 p->srv_bck--;
321
322 if (srv == p->lbprm.fbck) {
323 /* we lost the first backup server in a single-backup
324 * configuration, we must search another one.
325 */
326 struct server *srv2 = p->lbprm.fbck;
327 do {
328 srv2 = srv2->next;
329 } while (srv2 &&
330 !((srv2->state & SRV_BACKUP) &&
331 srv_is_usable(srv2->state, srv2->eweight)));
332 p->lbprm.fbck = srv2;
333 }
334 } else {
335 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
336 p->srv_act--;
337 }
338
339 fwrr_dequeue_srv(srv);
340 fwrr_remove_from_tree(srv);
341
342out_update_backend:
343 /* check/update tot_used, tot_weight */
344 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100345 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100346 srv->prev_state = srv->state;
347 srv->prev_eweight = srv->eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100348}
349
350/* This function updates the server trees according to server <srv>'s new
351 * state. It should be called when server <srv>'s status changes to up.
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100352 * It is not important whether the server was already down or not. It is not
353 * important either that the new state is completely UP (the caller may not
354 * know all the variables of a server's state). This function will not change
Willy Tarreaub625a082007-11-26 01:15:43 +0100355 * the weight of a server which was already up.
356 */
357static void fwrr_set_server_status_up(struct server *srv)
358{
359 struct proxy *p = srv->proxy;
360 struct fwrr_group *grp;
361
362 if (srv->state == srv->prev_state &&
363 srv->eweight == srv->prev_eweight)
364 return;
365
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100366 if (!srv_is_usable(srv->state, srv->eweight))
367 goto out_update_state;
368
Willy Tarreaub625a082007-11-26 01:15:43 +0100369 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
370 /* server was already up */
371 goto out_update_backend;
372
373 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
374 grp->next_weight += srv->eweight;
375
376 if (srv->state & SRV_BACKUP) {
377 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
378 p->srv_bck++;
379
380 if (p->lbprm.fbck) {
381 /* we may have restored a backup server prior to fbck,
382 * in which case it should replace it.
383 */
384 struct server *srv2 = srv;
385 do {
386 srv2 = srv2->next;
387 } while (srv2 && (srv2 != p->lbprm.fbck));
388 if (srv2)
389 p->lbprm.fbck = srv;
390 }
391 } else {
392 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
393 p->srv_act++;
394 }
395
396 /* note that eweight cannot be 0 here */
397 fwrr_get_srv(srv);
398 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
399 fwrr_queue_srv(srv);
400
401out_update_backend:
402 /* check/update tot_used, tot_weight */
403 update_backend_weight(p);
Willy Tarreau0ebe1062007-11-30 11:11:02 +0100404 out_update_state:
Willy Tarreaub625a082007-11-26 01:15:43 +0100405 srv->prev_state = srv->state;
406 srv->prev_eweight = srv->eweight;
407}
408
409/* This function must be called after an update to server <srv>'s effective
410 * weight. It may be called after a state change too.
411 */
412static void fwrr_update_server_weight(struct server *srv)
413{
414 int old_state, new_state;
415 struct proxy *p = srv->proxy;
416 struct fwrr_group *grp;
417
418 if (srv->state == srv->prev_state &&
419 srv->eweight == srv->prev_eweight)
420 return;
421
422 /* If changing the server's weight changes its state, we simply apply
423 * the procedures we already have for status change. If the state
424 * remains down, the server is not in any tree, so it's as easy as
425 * updating its values. If the state remains up with different weights,
426 * there are some computations to perform to find a new place and
427 * possibly a new tree for this server.
428 */
429
430 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
431 new_state = srv_is_usable(srv->state, srv->eweight);
432
433 if (!old_state && !new_state) {
434 srv->prev_state = srv->state;
435 srv->prev_eweight = srv->eweight;
436 return;
437 }
438 else if (!old_state && new_state) {
439 fwrr_set_server_status_up(srv);
440 return;
441 }
442 else if (old_state && !new_state) {
443 fwrr_set_server_status_down(srv);
444 return;
445 }
446
447 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
448 grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight;
449
450 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
451 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
452
453 if (srv->lb_tree == grp->init) {
454 fwrr_dequeue_srv(srv);
455 fwrr_queue_by_weight(grp->init, srv);
456 }
457 else if (!srv->lb_tree) {
458 /* FIXME: server was down. This is not possible right now but
459 * may be needed soon for slowstart or graceful shutdown.
460 */
461 fwrr_dequeue_srv(srv);
462 fwrr_get_srv(srv);
463 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
464 fwrr_queue_srv(srv);
465 } else {
466 /* The server is either active or in the next queue. If it's
467 * still in the active queue and it has not consumed all of its
468 * places, let's adjust its next position.
469 */
470 fwrr_get_srv(srv);
471
472 if (srv->eweight > 0) {
473 int prev_next = srv->npos;
474 int step = grp->next_weight / srv->eweight;
475
476 srv->npos = srv->lpos + step;
477 srv->rweight = 0;
478
479 if (srv->npos > prev_next)
480 srv->npos = prev_next;
481 if (srv->npos < grp->curr_pos + 2)
482 srv->npos = grp->curr_pos + step;
483 } else {
484 /* push it into the next tree */
485 srv->npos = grp->curr_pos + grp->curr_weight;
486 }
487
488 fwrr_dequeue_srv(srv);
489 fwrr_queue_srv(srv);
490 }
491
492 update_backend_weight(p);
493 srv->prev_state = srv->state;
494 srv->prev_eweight = srv->eweight;
495}
496
497/* Remove a server from a tree. It must have previously been dequeued. This
498 * function is meant to be called when a server is going down or has its
499 * weight disabled.
500 */
501static inline void fwrr_remove_from_tree(struct server *s)
502{
503 s->lb_tree = NULL;
504}
505
506/* Queue a server in the weight tree <root>, assuming the weight is >0.
507 * We want to sort them by inverted weights, because we need to place
508 * heavy servers first in order to get a smooth distribution.
509 */
510static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
511{
512 /* eweight can be as high as 256*255 */
513 s->lb_node.key = BE_WEIGHT_SCALE*255 - s->eweight;
514 eb32_insert(root, &s->lb_node);
515 s->lb_tree = root;
516}
517
518/* This function is responsible for building the weight trees in case of fast
519 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
520 * ratio. Both active and backup groups are initialized.
521 */
522void fwrr_init_server_groups(struct proxy *p)
523{
524 struct server *srv;
525 struct eb_root init_head = EB_ROOT;
526
527 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
528 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
529 p->lbprm.update_server_eweight = fwrr_update_server_weight;
530
531 p->lbprm.wdiv = BE_WEIGHT_SCALE;
532 for (srv = p->srv; srv; srv = srv->next) {
533 srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE;
534 srv->prev_state = srv->state;
535 }
536
537 recount_servers(p);
538 update_backend_weight(p);
539
540 /* prepare the active servers group */
541 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
542 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
543 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
544 p->lbprm.fwrr.act.t1 = init_head;
545 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
546 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
547
548 /* prepare the backup servers group */
549 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
550 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
551 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
552 p->lbprm.fwrr.bck.t1 = init_head;
553 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
554 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
555
556 /* queue active and backup servers in two distinct groups */
557 for (srv = p->srv; srv; srv = srv->next) {
558 if (!srv_is_usable(srv->state, srv->eweight))
559 continue;
560 fwrr_queue_by_weight((srv->state & SRV_BACKUP) ?
561 p->lbprm.fwrr.bck.init :
562 p->lbprm.fwrr.act.init,
563 srv);
564 }
565}
566
567/* simply removes a server from a weight tree */
568static inline void fwrr_dequeue_srv(struct server *s)
569{
570 eb32_delete(&s->lb_node);
571}
572
573/* queues a server into the appropriate group and tree depending on its
574 * backup status, and ->npos. If the server is disabled, simply assign
575 * it to the NULL tree.
576 */
577static void fwrr_queue_srv(struct server *s)
578{
579 struct proxy *p = s->proxy;
580 struct fwrr_group *grp;
581
582 grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
583
584 /* Delay everything which does not fit into the window and everything
585 * which does not fit into the theorical new window.
586 */
587 if (!srv_is_usable(s->state, s->eweight)) {
588 fwrr_remove_from_tree(s);
589 }
590 else if (s->eweight <= 0 ||
591 s->npos >= 2 * grp->curr_weight ||
592 s->npos >= grp->curr_weight + grp->next_weight) {
593 /* put into next tree, and readjust npos in case we could
594 * finally take this back to current. */
595 s->npos -= grp->curr_weight;
596 fwrr_queue_by_weight(grp->next, s);
597 }
598 else {
599 /* FIXME: we want to multiply by a constant to avoid overrides
600 * after weight changes, but this can easily overflow on 32-bit
601 * values. We need to change this for a 64-bit tree, and keep
602 * the 65536 factor for optimal smoothness (both rweight and
603 * eweight are 16 bit entities). s->npos is bound by the number
604 * of servers times the maximum eweight (~= nsrv << 16).
605 */
606 //s->lb_node.key = grp->curr_weight * s->npos + s->rweight - s->eweight;
607 //s->lb_node.key = 65536 * s->npos + s->rweight - s->eweight;
608 s->lb_node.key = 16 * s->npos + (s->rweight - s->eweight) / 4096;
609 eb32i_insert(&grp->curr, &s->lb_node);
610 s->lb_tree = &grp->curr;
611 }
612}
613
614/* prepares a server when extracting it from the "init" tree */
615static inline void fwrr_get_srv_init(struct server *s)
616{
617 s->npos = s->rweight = 0;
618}
619
620/* prepares a server when extracting it from the "next" tree */
621static inline void fwrr_get_srv_next(struct server *s)
622{
623 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
624 &s->proxy->lbprm.fwrr.bck :
625 &s->proxy->lbprm.fwrr.act;
626
627 s->npos += grp->curr_weight;
628}
629
630/* prepares a server when it was marked down */
631static inline void fwrr_get_srv_down(struct server *s)
632{
633 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
634 &s->proxy->lbprm.fwrr.bck :
635 &s->proxy->lbprm.fwrr.act;
636
637 s->npos = grp->curr_pos;
638}
639
640/* prepares a server when extracting it from its tree */
641static void fwrr_get_srv(struct server *s)
642{
643 struct proxy *p = s->proxy;
644 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
645 &p->lbprm.fwrr.bck :
646 &p->lbprm.fwrr.act;
647
648 if (s->lb_tree == grp->init) {
649 fwrr_get_srv_init(s);
650 }
651 else if (s->lb_tree == grp->next) {
652 fwrr_get_srv_next(s);
653 }
654 else if (s->lb_tree == NULL) {
655 fwrr_get_srv_down(s);
656 }
657}
658
659/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
660 * when this happens, and "next" filled with servers sorted by weights.
661 */
662static inline void fwrr_switch_trees(struct fwrr_group *grp)
663{
664 struct eb_root *swap;
665 swap = grp->init;
666 grp->init = grp->next;
667 grp->next = swap;
668 grp->curr_weight = grp->next_weight;
669 grp->curr_pos = grp->curr_weight;
670}
671
672/* return next server from the current tree in FWRR group <grp>, or a server
673 * from the "init" tree if appropriate. If both trees are empty, return NULL.
674 */
675static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
676{
677 struct eb32_node *node;
678 struct server *s;
679
680 node = eb32_first(&grp->curr);
681 s = eb32_entry(node, struct server, lb_node);
682
683 if (!node || s->npos > grp->curr_pos) {
684 /* either we have no server left, or we have a hole */
685 struct eb32_node *node2;
686 node2 = eb32_first(grp->init);
687 if (node2) {
688 node = node2;
689 s = eb32_entry(node, struct server, lb_node);
690 fwrr_get_srv_init(s);
691 if (s->eweight == 0) /* FIXME: is it possible at all ? */
692 node = NULL;
693 }
694 }
695 if (node)
696 return s;
697 else
698 return NULL;
699}
700
701/* Computes next position of server <s> in the group. It is mandatory for <s>
702 * to have a non-zero, positive eweight.
703*/
704static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
705{
706 if (!s->npos) {
707 /* first time ever for this server */
708 s->lpos = grp->curr_pos;
709 s->npos = grp->curr_pos + grp->next_weight / s->eweight;
710 s->rweight += grp->next_weight % s->eweight;
711
712 if (s->rweight >= s->eweight) {
713 s->rweight -= s->eweight;
714 s->npos++;
715 }
716 } else {
717 s->lpos = s->npos;
718 s->npos += grp->next_weight / s->eweight;
719 s->rweight += grp->next_weight % s->eweight;
720
721 if (s->rweight >= s->eweight) {
722 s->rweight -= s->eweight;
723 s->npos++;
724 }
725 }
726}
727
728/* Return next server from the current tree in backend <p>, or a server from
729 * the init tree if appropriate. If both trees are empty, return NULL.
730 * Saturated servers are skipped and requeued.
731 */
732static struct server *fwrr_get_next_server(struct proxy *p)
733{
734 struct server *srv;
735 struct fwrr_group *grp;
736 struct server *full;
737 int switched;
738
739 if (p->srv_act)
740 grp = &p->lbprm.fwrr.act;
741 else if (p->lbprm.fbck)
742 return p->lbprm.fbck;
743 else if (p->srv_bck)
744 grp = &p->lbprm.fwrr.bck;
745 else
746 return NULL;
747
748 switched = 0;
749 full = NULL; /* NULL-terminated list of saturated servers */
750 while (1) {
751 /* if we see an empty group, let's first try to collect weights
752 * which might have recently changed.
753 */
754 if (!grp->curr_weight)
755 grp->curr_pos = grp->curr_weight = grp->next_weight;
756
757 /* get first server from the "current" tree. When the end of
758 * the tree is reached, we may have to switch, but only once.
759 */
760 while (1) {
761 srv = fwrr_get_server_from_group(grp);
762 if (srv)
763 break;
764 if (switched)
765 goto requeue_servers;
766 switched = 1;
767 fwrr_switch_trees(grp);
768
769 }
770
771 /* OK, we have a server. However, it may be saturated, in which
772 * case we don't want to reconsider it for now. We'll update
773 * its position and dequeue it anyway, so that we can move it
774 * to a better place afterwards.
775 */
776 fwrr_update_position(grp, srv);
777 fwrr_dequeue_srv(srv);
778 grp->curr_pos++;
779 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv))
780 break;
781
782 /* the server is saturated, let's chain it for later reinsertion */
783 srv->next_full = full;
784 full = srv;
785 }
786
787 /* OK, we got the best server, let's update it */
788 fwrr_queue_srv(srv);
789
790 requeue_servers:
791 if (unlikely(full)) {
792 if (switched) {
793 /* the tree has switched, requeue all extracted servers
794 * into "init", because their place was lost, and only
795 * their weight matters.
796 */
797 do {
798 fwrr_queue_by_weight(grp->init, full);
799 full = full->next_full;
800 } while (full);
801 } else {
802 /* requeue all extracted servers just as if they were consumed
803 * so that they regain their expected place.
804 */
805 do {
806 fwrr_queue_srv(full);
807 full = full->next_full;
808 } while (full);
809 }
810 }
811 return srv;
812}
813
Willy Tarreau01732802007-11-01 22:48:15 +0100814/*
815 * This function tries to find a running server for the proxy <px> following
816 * the URL parameter hash method. It looks for a specific parameter in the
817 * URL and hashes it to compute the server ID. This is useful to optimize
818 * performance by avoiding bounces between servers in contexts where sessions
819 * are shared but cookies are not usable. If the parameter is not found, NULL
820 * is returned. If any server is found, it will be returned. If no valid server
821 * is found, NULL is returned.
822 *
823 */
824struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
825{
826 unsigned long hash = 0;
827 char *p;
828 int plen;
829
Willy Tarreau20697042007-11-15 23:26:18 +0100830 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100831 return NULL;
832
Willy Tarreau20697042007-11-15 23:26:18 +0100833 if (px->lbprm.map.state & PR_MAP_RECALC)
834 recalc_server_map(px);
835
Willy Tarreau01732802007-11-01 22:48:15 +0100836 p = memchr(uri, '?', uri_len);
837 if (!p)
838 return NULL;
839 p++;
840
841 uri_len -= (p - uri);
842 plen = px->url_param_len;
843
844 if (uri_len <= plen)
845 return NULL;
846
847 while (uri_len > plen) {
848 /* Look for the parameter name followed by an equal symbol */
849 if (p[plen] == '=') {
850 /* skip the equal symbol */
851 uri = p;
852 p += plen + 1;
853 uri_len -= plen + 1;
854 if (memcmp(uri, px->url_param_name, plen) == 0) {
855 /* OK, we have the parameter here at <uri>, and
856 * the value after the equal sign, at <p>
857 */
858 while (uri_len && *p != '&') {
859 hash = *p + (hash << 6) + (hash << 16) - hash;
860 uri_len--;
861 p++;
862 }
Willy Tarreau20697042007-11-15 23:26:18 +0100863 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau01732802007-11-01 22:48:15 +0100864 }
865 }
866
867 /* skip to next parameter */
868 uri = p;
869 p = memchr(uri, '&', uri_len);
870 if (!p)
871 return NULL;
872 p++;
873 uri_len -= (p - uri);
874 }
875 return NULL;
876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
878/*
879 * This function marks the session as 'assigned' in direct or dispatch modes,
880 * or tries to assign one in balance mode, according to the algorithm. It does
881 * nothing if the session had already been assigned a server.
882 *
883 * It may return :
884 * SRV_STATUS_OK if everything is OK. s->srv will be valid.
885 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
886 * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL.
887 * SRV_STATUS_INTERNAL for other unrecoverable errors.
888 *
889 * Upon successful return, the session flag SN_ASSIGNED to indicate that it does
890 * not need to be called anymore. This usually means that s->srv can be trusted
891 * in balance and direct modes. This flag is not cleared, so it's to the caller
892 * to clear it if required (eg: redispatch).
893 *
894 */
895
896int assign_server(struct session *s)
897{
898#ifdef DEBUG_FULL
899 fprintf(stderr,"assign_server : s=%p\n",s);
900#endif
901
902 if (s->pend_pos)
903 return SRV_STATUS_INTERNAL;
904
905 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau31682232007-11-29 15:38:04 +0100906 if (s->be->lbprm.algo & BE_LB_ALGO) {
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100907 int len;
908
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100909 if (s->flags & SN_DIRECT) {
910 s->flags |= SN_ASSIGNED;
911 return SRV_STATUS_OK;
912 }
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100913
Willy Tarreaub625a082007-11-26 01:15:43 +0100914 if (!s->be->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 return SRV_STATUS_NOSRV;
916
Willy Tarreau31682232007-11-29 15:38:04 +0100917 switch (s->be->lbprm.algo & BE_LB_ALGO) {
918 case BE_LB_ALGO_RR:
Willy Tarreaub625a082007-11-26 01:15:43 +0100919 s->srv = fwrr_get_next_server(s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920 if (!s->srv)
921 return SRV_STATUS_FULL;
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100922 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100923 case BE_LB_ALGO_SH:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 if (s->cli_addr.ss_family == AF_INET)
925 len = 4;
926 else if (s->cli_addr.ss_family == AF_INET6)
927 len = 16;
928 else /* unknown IP family */
929 return SRV_STATUS_INTERNAL;
930
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200931 s->srv = get_server_sh(s->be,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
933 len);
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100934 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100935 case BE_LB_ALGO_UH:
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200936 /* URI hashing */
937 s->srv = get_server_uh(s->be,
938 s->txn.req.sol + s->txn.req.sl.rq.u,
939 s->txn.req.sl.rq.u_l);
Willy Tarreau01732802007-11-01 22:48:15 +0100940 break;
Willy Tarreau31682232007-11-29 15:38:04 +0100941 case BE_LB_ALGO_PH:
Willy Tarreau01732802007-11-01 22:48:15 +0100942 /* URL Parameter hashing */
943 s->srv = get_server_ph(s->be,
944 s->txn.req.sol + s->txn.req.sl.rq.u,
945 s->txn.req.sl.rq.u_l);
946 if (!s->srv) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100947 /* parameter not found, fall back to round robin on the map */
Willy Tarreau01732802007-11-01 22:48:15 +0100948 s->srv = get_server_rr_with_conns(s->be);
949 if (!s->srv)
950 return SRV_STATUS_FULL;
951 }
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100952 break;
953 default:
954 /* unknown balancing algorithm */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 return SRV_STATUS_INTERNAL;
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100956 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100958 else if (s->be->options & PR_O_HTTP_PROXY) {
959 if (!s->srv_addr.sin_addr.s_addr)
960 return SRV_STATUS_NOSRV;
961 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200962 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100963 !(s->fe->options & PR_O_TRANSP)) {
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100964 return SRV_STATUS_NOSRV;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100965 }
966 s->flags |= SN_ASSIGNED;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 }
968 return SRV_STATUS_OK;
969}
970
971
972/*
973 * This function assigns a server address to a session, and sets SN_ADDR_SET.
974 * The address is taken from the currently assigned server, or from the
975 * dispatch or transparent address.
976 *
977 * It may return :
978 * SRV_STATUS_OK if everything is OK.
979 * SRV_STATUS_INTERNAL for other unrecoverable errors.
980 *
981 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
982 * not cleared, so it's to the caller to clear it if required.
983 *
984 */
985int assign_server_address(struct session *s)
986{
987#ifdef DEBUG_FULL
988 fprintf(stderr,"assign_server_address : s=%p\n",s);
989#endif
990
Willy Tarreau31682232007-11-29 15:38:04 +0100991 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 /* A server is necessarily known for this session */
993 if (!(s->flags & SN_ASSIGNED))
994 return SRV_STATUS_INTERNAL;
995
996 s->srv_addr = s->srv->addr;
997
998 /* if this server remaps proxied ports, we'll use
999 * the port the client connected to with an offset. */
1000 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau14c8aac2007-05-08 19:46:30 +02001001 if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
1002 get_frt_addr(s);
1003 if (s->frt_addr.ss_family == AF_INET) {
1004 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1005 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
1006 } else {
1007 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
1008 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
1009 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010 }
1011 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001012 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001014 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001016 else if (s->fe->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 /* in transparent mode, use the original dest addr if no dispatch specified */
1018 socklen_t salen = sizeof(s->srv_addr);
1019
1020 if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) {
1021 qfprintf(stderr, "Cannot get original server address.\n");
1022 return SRV_STATUS_INTERNAL;
1023 }
1024 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001025 else if (s->be->options & PR_O_HTTP_PROXY) {
1026 /* If HTTP PROXY option is set, then server is already assigned
1027 * during incoming client request parsing. */
1028 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +01001029 else {
1030 /* no server and no LB algorithm ! */
1031 return SRV_STATUS_INTERNAL;
1032 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033
1034 s->flags |= SN_ADDR_SET;
1035 return SRV_STATUS_OK;
1036}
1037
1038
1039/* This function assigns a server to session <s> if required, and can add the
1040 * connection to either the assigned server's queue or to the proxy's queue.
1041 *
1042 * Returns :
1043 *
1044 * SRV_STATUS_OK if everything is OK.
1045 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
1046 * SRV_STATUS_QUEUED if the connection has been queued.
1047 * SRV_STATUS_FULL if the server(s) is/are saturated and the
1048 * connection could not be queued.
1049 * SRV_STATUS_INTERNAL for other unrecoverable errors.
1050 *
1051 */
1052int assign_server_and_queue(struct session *s)
1053{
1054 struct pendconn *p;
1055 int err;
1056
1057 if (s->pend_pos)
1058 return SRV_STATUS_INTERNAL;
1059
1060 if (s->flags & SN_ASSIGNED) {
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001061 if (s->srv && s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) {
1062 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1063 s->srv = NULL;
1064 http_flush_cookie_flags(&s->txn);
1065 } else {
1066 /* a server does not need to be assigned, perhaps because we're in
1067 * direct mode, or in dispatch or transparent modes where the server
1068 * is not needed.
1069 */
1070 if (s->srv &&
1071 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
1072 p = pendconn_add(s);
1073 if (p)
1074 return SRV_STATUS_QUEUED;
1075 else
1076 return SRV_STATUS_FULL;
1077 }
1078 return SRV_STATUS_OK;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 }
1081
1082 /* a server needs to be assigned */
1083 err = assign_server(s);
1084 switch (err) {
1085 case SRV_STATUS_OK:
1086 /* in balance mode, we might have servers with connection limits */
1087 if (s->srv &&
1088 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
1089 p = pendconn_add(s);
1090 if (p)
1091 return SRV_STATUS_QUEUED;
1092 else
1093 return SRV_STATUS_FULL;
1094 }
1095 return SRV_STATUS_OK;
1096
1097 case SRV_STATUS_FULL:
1098 /* queue this session into the proxy's queue */
1099 p = pendconn_add(s);
1100 if (p)
1101 return SRV_STATUS_QUEUED;
1102 else
1103 return SRV_STATUS_FULL;
1104
1105 case SRV_STATUS_NOSRV:
1106 case SRV_STATUS_INTERNAL:
1107 return err;
1108 default:
1109 return SRV_STATUS_INTERNAL;
1110 }
1111}
1112
1113
1114/*
1115 * This function initiates a connection to the server assigned to this session
1116 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
1117 * It can return one of :
1118 * - SN_ERR_NONE if everything's OK
1119 * - SN_ERR_SRVTO if there are no more servers
1120 * - SN_ERR_SRVCL if the connection was refused by the server
1121 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1122 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1123 * - SN_ERR_INTERNAL for any other purely internal errors
1124 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
1125 */
1126int connect_server(struct session *s)
1127{
1128 int fd, err;
1129
1130 if (!(s->flags & SN_ADDR_SET)) {
1131 err = assign_server_address(s);
1132 if (err != SRV_STATUS_OK)
1133 return SN_ERR_INTERNAL;
1134 }
1135
1136 if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
1137 qfprintf(stderr, "Cannot get a server socket.\n");
1138
1139 if (errno == ENFILE)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001140 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001142 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 else if (errno == EMFILE)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001144 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001145 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001146 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147 else if (errno == ENOBUFS || errno == ENOMEM)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001148 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001150 s->be->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 /* this is a resource error */
1152 return SN_ERR_RESOURCE;
1153 }
1154
1155 if (fd >= global.maxsock) {
1156 /* do not log anything there, it's a normal condition when this option
1157 * is used to serialize connections to a server !
1158 */
1159 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
1160 close(fd);
1161 return SN_ERR_PRXCOND; /* it is a configuration limit */
1162 }
1163
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001164#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001165 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001166 /* TCP splicing supported by both FE and BE */
1167 tcp_splice_initfd(s->cli_fd, fd);
1168 }
1169#endif
1170
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
1172 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
1173 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
1174 close(fd);
1175 return SN_ERR_INTERNAL;
1176 }
1177
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001178 if (s->be->options & PR_O_TCP_SRV_KA)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
1180
Alexandre Cassen87ea5482007-10-11 20:48:58 +02001181 if (s->be->options & PR_O_TCP_NOLING)
1182 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184 /* allow specific binding :
1185 * - server-specific at first
1186 * - proxy-specific next
1187 */
1188 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
1189 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
1190 if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) {
1191 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001192 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001193 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001194 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001196 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 return SN_ERR_RESOURCE;
1198 }
Willy Tarreau77074d52006-11-12 23:57:19 +01001199#ifdef CONFIG_HAP_CTTPROXY
1200 if (s->srv->state & SRV_TPROXY_MASK) {
1201 struct in_tproxy itp1, itp2;
1202 memset(&itp1, 0, sizeof(itp1));
1203
1204 itp1.op = TPROXY_ASSIGN;
1205 switch (s->srv->state & SRV_TPROXY_MASK) {
1206 case SRV_TPROXY_ADDR:
1207 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
1208 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
1209 break;
1210 case SRV_TPROXY_CLI:
1211 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
1212 /* fall through */
1213 case SRV_TPROXY_CIP:
1214 /* FIXME: what can we do if the client connects in IPv6 ? */
1215 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
1216 break;
1217 }
1218
1219 /* set connect flag on socket */
1220 itp2.op = TPROXY_FLAGS;
1221 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
1222
1223 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
1224 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
1225 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001226 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001227 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001228 send_log(s->be, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +01001229 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001230 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001231 return SN_ERR_RESOURCE;
1232 }
1233 }
1234#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001235 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001236 else if (s->be->options & PR_O_BIND_SRC) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001238 if (bind(fd, (struct sockaddr *)&s->be->source_addr, sizeof(s->be->source_addr)) == -1) {
1239 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->be->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001241 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001243 s->be->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 return SN_ERR_RESOURCE;
1245 }
Willy Tarreau77074d52006-11-12 23:57:19 +01001246#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001247 if (s->be->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +01001248 struct in_tproxy itp1, itp2;
1249 memset(&itp1, 0, sizeof(itp1));
1250
1251 itp1.op = TPROXY_ASSIGN;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001252 switch (s->be->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +01001253 case PR_O_TPXY_ADDR:
1254 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
1255 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
1256 break;
1257 case PR_O_TPXY_CLI:
1258 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
1259 /* fall through */
1260 case PR_O_TPXY_CIP:
1261 /* FIXME: what can we do if the client connects in IPv6 ? */
1262 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
1263 break;
1264 }
1265
1266 /* set connect flag on socket */
1267 itp2.op = TPROXY_FLAGS;
1268 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
1269
1270 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
1271 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
1272 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001273 s->be->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001274 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001275 send_log(s->be, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +01001276 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001277 s->be->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +01001278 return SN_ERR_RESOURCE;
1279 }
1280 }
1281#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282 }
1283
1284 if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) &&
1285 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
1286
1287 if (errno == EAGAIN || errno == EADDRINUSE) {
1288 char *msg;
1289 if (errno == EAGAIN) /* no free ports left, try again later */
1290 msg = "no free ports";
1291 else
1292 msg = "local address already in use";
1293
1294 qfprintf(stderr,"Cannot connect: %s.\n",msg);
1295 close(fd);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001296 send_log(s->be, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297 "Connect() failed for server %s/%s: %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001298 s->be->id, s->srv->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 return SN_ERR_RESOURCE;
1300 } else if (errno == ETIMEDOUT) {
1301 //qfprintf(stderr,"Connect(): ETIMEDOUT");
1302 close(fd);
1303 return SN_ERR_SRVTO;
1304 } else {
1305 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
1306 //qfprintf(stderr,"Connect(): %d", errno);
1307 close(fd);
1308 return SN_ERR_SRVCL;
1309 }
1310 }
1311
1312 fdtab[fd].owner = s->task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaud7971282006-07-29 18:36:34 +02001314 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
Willy Tarreau54469402006-07-29 16:59:06 +02001315 fdtab[fd].cb[DIR_RD].b = s->rep;
Willy Tarreauf8306d52006-07-29 19:01:31 +02001316 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
Willy Tarreau54469402006-07-29 16:59:06 +02001317 fdtab[fd].cb[DIR_WR].b = s->req;
Willy Tarreaue94ebd02007-10-09 17:14:37 +02001318
1319 fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr;
1320 fdtab[fd].peerlen = sizeof(s->srv_addr);
1321
Willy Tarreauf161a342007-04-08 16:59:42 +02001322 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001323
1324 fd_insert(fd);
1325 if (s->srv) {
1326 s->srv->cur_sess++;
1327 if (s->srv->cur_sess > s->srv->cur_sess_max)
1328 s->srv->cur_sess_max = s->srv->cur_sess;
1329 }
1330
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001331 if (!tv_add_ifset(&s->req->cex, &now, &s->be->contimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02001332 tv_eternity(&s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333 return SN_ERR_NONE; /* connection is OK */
1334}
1335
1336
1337/*
1338 * This function checks the retry count during the connect() job.
1339 * It updates the session's srv_state and retries, so that the caller knows
1340 * what it has to do. It uses the last connection error to set the log when
1341 * it expires. It returns 1 when it has expired, and 0 otherwise.
1342 */
1343int srv_count_retry_down(struct session *t, int conn_err)
1344{
1345 /* we are in front of a retryable error */
1346 t->conn_retries--;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001347 if (t->srv)
1348 t->srv->retries++;
1349 t->be->retries++;
1350
Willy Tarreaubaaee002006-06-26 02:48:02 +02001351 if (t->conn_retries < 0) {
1352 /* if not retryable anymore, let's abort */
Willy Tarreaud7971282006-07-29 18:36:34 +02001353 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001354 srv_close_with_err(t, conn_err, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001355 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356 if (t->srv)
1357 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001358 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359
1360 /* We used to have a free connection slot. Since we'll never use it,
1361 * we have to inform the server that it may be used by another session.
1362 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001363 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001364 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001365 return 1;
1366 }
1367 return 0;
1368}
1369
1370
1371/*
1372 * This function performs the retryable part of the connect() job.
1373 * It updates the session's srv_state and retries, so that the caller knows
1374 * what it has to do. It returns 1 when it breaks out of the loop, or 0 if
1375 * it needs to redispatch.
1376 */
1377int srv_retryable_connect(struct session *t)
1378{
1379 int conn_err;
1380
1381 /* This loop ensures that we stop before the last retry in case of a
1382 * redispatchable server.
1383 */
1384 do {
1385 /* initiate a connection to the server */
1386 conn_err = connect_server(t);
1387 switch (conn_err) {
1388
1389 case SN_ERR_NONE:
1390 //fprintf(stderr,"0: c=%d, s=%d\n", c, s);
1391 t->srv_state = SV_STCONN;
1392 return 1;
1393
1394 case SN_ERR_INTERNAL:
Willy Tarreaud7971282006-07-29 18:36:34 +02001395 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001397 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 if (t->srv)
1399 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001400 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001402 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001403 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404 return 1;
1405 }
1406 /* ensure that we have enough retries left */
1407 if (srv_count_retry_down(t, conn_err)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001408 return 1;
1409 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001410 } while (t->srv == NULL || t->conn_retries > 0 || !(t->be->options & PR_O_REDISP));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001411
1412 /* We're on our last chance, and the REDISP option was specified.
1413 * We will ignore cookie and force to balance or use the dispatcher.
1414 */
1415 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001416 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001417 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418
1419 if (t->srv)
1420 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001421 t->be->redispatches++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001422
1423 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1424 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreau3d300592007-03-18 18:34:41 +01001425 http_flush_cookie_flags(&t->txn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426 return 0;
1427}
1428
1429
1430/* This function performs the "redispatch" part of a connection attempt. It
1431 * will assign a server if required, queue the connection if required, and
1432 * handle errors that might arise at this level. It can change the server
1433 * state. It will return 1 if it encounters an error, switches the server
1434 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1435 * that the connection is ready to use.
1436 */
1437
1438int srv_redispatch_connect(struct session *t)
1439{
1440 int conn_err;
1441
1442 /* We know that we don't have any connection pending, so we will
1443 * try to get a new one, and wait in this state if it's queued
1444 */
1445 conn_err = assign_server_and_queue(t);
1446 switch (conn_err) {
1447 case SRV_STATUS_OK:
1448 break;
1449
1450 case SRV_STATUS_NOSRV:
1451 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaud7971282006-07-29 18:36:34 +02001452 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001453 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001454 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455 if (t->srv)
1456 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001457 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001458
1459 return 1;
1460
1461 case SRV_STATUS_QUEUED:
1462 /* FIXME-20060503 : we should use the queue timeout instead */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001463 if (!tv_add_ifset(&t->req->cex, &now, &t->be->contimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02001464 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001465 t->srv_state = SV_STIDLE;
1466 /* do nothing else and do not wake any other session up */
1467 return 1;
1468
1469 case SRV_STATUS_FULL:
1470 case SRV_STATUS_INTERNAL:
1471 default:
Willy Tarreaud7971282006-07-29 18:36:34 +02001472 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001473 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +01001474 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001475 if (t->srv)
1476 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001477 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001478
1479 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001480 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02001481 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001482 return 1;
1483 }
1484 /* if we get here, it's because we got SRV_STATUS_OK, which also
1485 * means that the connection has not been queued.
1486 */
1487 return 0;
1488}
1489
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001490int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001491 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001492 return px->down_time;
1493
1494 return now.tv_sec - px->last_change + px->down_time;
1495}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001496
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001497/* This function parses a "balance" statement in a backend section describing
1498 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1499 * returns -1, it may write an error message into ther <err> buffer, for at
1500 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1501 * written. The function must be called with <args> pointing to the first word
1502 * after "balance".
1503 */
1504int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1505{
1506 if (!*(args[0])) {
1507 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001508 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1509 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001510 return 0;
1511 }
1512
1513 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001514 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1515 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001516 }
1517 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001518 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1519 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001520 }
1521 else if (!strcmp(args[0], "uri")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001522 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1523 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001524 }
Willy Tarreau01732802007-11-01 22:48:15 +01001525 else if (!strcmp(args[0], "url_param")) {
1526 if (!*args[1]) {
1527 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1528 return -1;
1529 }
Willy Tarreau31682232007-11-29 15:38:04 +01001530 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1531 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreau01732802007-11-01 22:48:15 +01001532 if (curproxy->url_param_name)
1533 free(curproxy->url_param_name);
1534 curproxy->url_param_name = strdup(args[1]);
1535 curproxy->url_param_len = strlen(args[1]);
1536 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001537 else {
Willy Tarreau01732802007-11-01 22:48:15 +01001538 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source', 'uri' and 'url_param' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001539 return -1;
1540 }
1541 return 0;
1542}
1543
Willy Tarreaubaaee002006-06-26 02:48:02 +02001544/*
1545 * Local variables:
1546 * c-indent-level: 8
1547 * c-basic-offset: 8
1548 * End:
1549 */