Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Backend variables and functions. |
| 3 | * |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | f19cf37 | 2006-11-14 15:40:51 +0100 | [diff] [blame] | 18 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 21 | #include <common/config.h> |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 22 | #include <common/eb32tree.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 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 Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 33 | #include <proto/client.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/fd.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 35 | #include <proto/httperr.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | #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 Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_HAP_CTTPROXY |
| 43 | #include <import/ip_tproxy.h> |
| 44 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 46 | #ifdef CONFIG_HAP_TCPSPLICE |
| 47 | #include <libtcpsplice.h> |
| 48 | #endif |
| 49 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 50 | static inline void fwrr_remove_from_tree(struct server *s); |
| 51 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s); |
| 52 | static inline void fwrr_dequeue_srv(struct server *s); |
| 53 | static void fwrr_get_srv(struct server *s); |
| 54 | static 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 | */ |
| 59 | static inline int srv_is_usable(int state, int weight) |
| 60 | { |
| 61 | if (!weight) |
| 62 | return 0; |
| 63 | if (!(state & SRV_RUNNING)) |
| 64 | return 0; |
| 65 | return 1; |
| 66 | } |
| 67 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | /* |
| 69 | * This function recounts the number of usable active and backup servers for |
| 70 | * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck. |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 71 | * This function also recomputes the total active and backup weights. However, |
| 72 | * it does nout update tot_weight nor tot_used. Use update_backend_weight() for |
| 73 | * this. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 75 | static void recount_servers(struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | { |
| 77 | struct server *srv; |
| 78 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 79 | px->srv_act = px->srv_bck = 0; |
| 80 | px->lbprm.tot_wact = px->lbprm.tot_wbck = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 81 | px->lbprm.fbck = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 83 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 84 | continue; |
| 85 | |
| 86 | if (srv->state & SRV_BACKUP) { |
| 87 | if (!px->srv_bck && |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 88 | !(px->lbprm.algo & PR_O_USE_ALL_BK)) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 89 | px->lbprm.fbck = srv; |
| 90 | px->srv_bck++; |
| 91 | px->lbprm.tot_wbck += srv->eweight; |
| 92 | } else { |
| 93 | px->srv_act++; |
| 94 | px->lbprm.tot_wact += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | } |
| 96 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 97 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 98 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 99 | /* This function simply updates the backend's tot_weight and tot_used values |
| 100 | * after servers weights have been updated. It is designed to be used after |
| 101 | * recount_servers() or equivalent. |
| 102 | */ |
| 103 | static void update_backend_weight(struct proxy *px) |
| 104 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 105 | if (px->srv_act) { |
| 106 | px->lbprm.tot_weight = px->lbprm.tot_wact; |
| 107 | px->lbprm.tot_used = px->srv_act; |
| 108 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 109 | else if (px->lbprm.fbck) { |
| 110 | /* use only the first backup server */ |
| 111 | px->lbprm.tot_weight = px->lbprm.fbck->eweight; |
| 112 | px->lbprm.tot_used = 1; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 113 | } |
| 114 | else { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 115 | px->lbprm.tot_weight = px->lbprm.tot_wbck; |
| 116 | px->lbprm.tot_used = px->srv_bck; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 117 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* this function updates the map according to server <srv>'s new state */ |
| 121 | static void map_set_server_status_down(struct server *srv) |
| 122 | { |
| 123 | struct proxy *p = srv->proxy; |
| 124 | |
| 125 | if (srv->state == srv->prev_state && |
| 126 | srv->eweight == srv->prev_eweight) |
| 127 | return; |
| 128 | |
| 129 | /* FIXME: could be optimized since we know what changed */ |
| 130 | recount_servers(p); |
| 131 | update_backend_weight(p); |
| 132 | srv->prev_state = srv->state; |
| 133 | srv->prev_eweight = srv->eweight; |
| 134 | p->lbprm.map.state |= PR_MAP_RECALC; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 138 | /* this function updates the map according to server <srv>'s new state */ |
| 139 | static void map_set_server_status_up(struct server *srv) |
| 140 | { |
| 141 | struct proxy *p = srv->proxy; |
| 142 | |
| 143 | if (srv->state == srv->prev_state && |
| 144 | srv->eweight == srv->prev_eweight) |
| 145 | return; |
| 146 | |
| 147 | /* FIXME: could be optimized since we know what changed */ |
| 148 | recount_servers(p); |
| 149 | update_backend_weight(p); |
| 150 | srv->prev_state = srv->state; |
| 151 | srv->prev_eweight = srv->eweight; |
| 152 | p->lbprm.map.state |= PR_MAP_RECALC; |
| 153 | } |
| 154 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 155 | /* This function recomputes the server map for proxy px. It relies on |
| 156 | * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be |
| 157 | * called after recount_servers(). It also expects px->lbprm.map.srv |
| 158 | * to be allocated with the largest size needed. It updates tot_weight. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 159 | */ |
| 160 | void recalc_server_map(struct proxy *px) |
| 161 | { |
| 162 | int o, tot, flag; |
| 163 | struct server *cur, *best; |
| 164 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 165 | switch (px->lbprm.tot_used) { |
| 166 | case 0: /* no server */ |
| 167 | px->lbprm.map.state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | return; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 169 | case 1: /* only one server, just fill first entry */ |
| 170 | tot = 1; |
| 171 | break; |
| 172 | default: |
| 173 | tot = px->lbprm.tot_weight; |
| 174 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 177 | /* here we *know* that we have some servers */ |
| 178 | if (px->srv_act) |
| 179 | flag = SRV_RUNNING; |
| 180 | else |
| 181 | flag = SRV_RUNNING | SRV_BACKUP; |
| 182 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | /* this algorithm gives priority to the first server, which means that |
| 184 | * it will respect the declaration order for equivalent weights, and |
| 185 | * that whatever the weights, the first server called will always be |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 186 | * the first declared. This is an important asumption for the backup |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | * case, where we want the first server only. |
| 188 | */ |
| 189 | for (cur = px->srv; cur; cur = cur->next) |
| 190 | cur->wscore = 0; |
| 191 | |
| 192 | for (o = 0; o < tot; o++) { |
| 193 | int max = 0; |
| 194 | best = NULL; |
| 195 | for (cur = px->srv; cur; cur = cur->next) { |
| 196 | if ((cur->state & (SRV_RUNNING | SRV_BACKUP)) == flag) { |
| 197 | int v; |
| 198 | |
| 199 | /* If we are forced to return only one server, we don't want to |
| 200 | * go further, because we would return the wrong one due to |
| 201 | * divide overflow. |
| 202 | */ |
| 203 | if (tot == 1) { |
| 204 | best = cur; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 205 | /* note that best->wscore will be wrong but we don't care */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 209 | cur->wscore += cur->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 210 | v = (cur->wscore + tot) / tot; /* result between 0 and 3 */ |
| 211 | if (best == NULL || v > max) { |
| 212 | max = v; |
| 213 | best = cur; |
| 214 | } |
| 215 | } |
| 216 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 217 | px->lbprm.map.srv[o] = best; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 218 | best->wscore -= tot; |
| 219 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 220 | px->lbprm.map.state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 223 | /* This function is responsible of building the server MAP for map-based LB |
| 224 | * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the |
| 225 | * weights if applicable. It should be called only once per proxy, at config |
| 226 | * time. |
| 227 | */ |
| 228 | void init_server_map(struct proxy *p) |
| 229 | { |
| 230 | struct server *srv; |
| 231 | int pgcd; |
| 232 | int act, bck; |
| 233 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 234 | p->lbprm.set_server_status_up = map_set_server_status_up; |
| 235 | p->lbprm.set_server_status_down = map_set_server_status_down; |
| 236 | p->lbprm.update_server_eweight = NULL; |
| 237 | |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 238 | if (!p->srv) |
| 239 | return; |
| 240 | |
| 241 | /* We will factor the weights to reduce the table, |
| 242 | * using Euclide's largest common divisor algorithm |
| 243 | */ |
| 244 | pgcd = p->srv->uweight; |
| 245 | for (srv = p->srv->next; srv && pgcd > 1; srv = srv->next) { |
| 246 | int w = srv->uweight; |
| 247 | while (w) { |
| 248 | int t = pgcd % w; |
| 249 | pgcd = w; |
| 250 | w = t; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* It is sometimes useful to know what factor to apply |
| 255 | * to the backend's effective weight to know its real |
| 256 | * weight. |
| 257 | */ |
| 258 | p->lbprm.wmult = pgcd; |
| 259 | |
| 260 | act = bck = 0; |
| 261 | for (srv = p->srv; srv; srv = srv->next) { |
| 262 | srv->eweight = srv->uweight / pgcd; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 263 | srv->prev_eweight = srv->eweight; |
| 264 | srv->prev_state = srv->state; |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 265 | if (srv->state & SRV_BACKUP) |
| 266 | bck += srv->eweight; |
| 267 | else |
| 268 | act += srv->eweight; |
| 269 | } |
| 270 | |
| 271 | /* this is the largest map we will ever need for this servers list */ |
| 272 | if (act < bck) |
| 273 | act = bck; |
| 274 | |
| 275 | p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *)); |
| 276 | /* recounts servers and their weights */ |
| 277 | p->lbprm.map.state = PR_MAP_RECALC; |
| 278 | recount_servers(p); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 279 | update_backend_weight(p); |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 280 | recalc_server_map(p); |
| 281 | } |
| 282 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 283 | /* This function updates the server trees according to server <srv>'s new |
| 284 | * state. It should be called when server <srv>'s status changes to down. |
| 285 | * It is not important whether the server was already down or not. However, |
| 286 | * it is mandatory that the new state be down. |
| 287 | */ |
| 288 | static void fwrr_set_server_status_down(struct server *srv) |
| 289 | { |
| 290 | struct proxy *p = srv->proxy; |
| 291 | struct fwrr_group *grp; |
| 292 | |
| 293 | if (srv->state == srv->prev_state && |
| 294 | srv->eweight == srv->prev_eweight) |
| 295 | return; |
| 296 | |
| 297 | if (!srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 298 | /* server was already down */ |
| 299 | goto out_update_backend; |
| 300 | |
| 301 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 302 | grp->next_weight -= srv->prev_eweight; |
| 303 | |
| 304 | if (srv->state & SRV_BACKUP) { |
| 305 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 306 | p->srv_bck--; |
| 307 | |
| 308 | if (srv == p->lbprm.fbck) { |
| 309 | /* we lost the first backup server in a single-backup |
| 310 | * configuration, we must search another one. |
| 311 | */ |
| 312 | struct server *srv2 = p->lbprm.fbck; |
| 313 | do { |
| 314 | srv2 = srv2->next; |
| 315 | } while (srv2 && |
| 316 | !((srv2->state & SRV_BACKUP) && |
| 317 | srv_is_usable(srv2->state, srv2->eweight))); |
| 318 | p->lbprm.fbck = srv2; |
| 319 | } |
| 320 | } else { |
| 321 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 322 | p->srv_act--; |
| 323 | } |
| 324 | |
| 325 | fwrr_dequeue_srv(srv); |
| 326 | fwrr_remove_from_tree(srv); |
| 327 | |
| 328 | out_update_backend: |
| 329 | /* check/update tot_used, tot_weight */ |
| 330 | update_backend_weight(p); |
| 331 | srv->prev_state = srv->state; |
| 332 | srv->prev_eweight = srv->eweight; |
| 333 | |
| 334 | } |
| 335 | |
| 336 | /* This function updates the server trees according to server <srv>'s new |
| 337 | * state. It should be called when server <srv>'s status changes to up. |
| 338 | * It is not important whether the server was already down or not. However, |
| 339 | * it is mandatory that the new state be up. This function will not change |
| 340 | * the weight of a server which was already up. |
| 341 | */ |
| 342 | static void fwrr_set_server_status_up(struct server *srv) |
| 343 | { |
| 344 | struct proxy *p = srv->proxy; |
| 345 | struct fwrr_group *grp; |
| 346 | |
| 347 | if (srv->state == srv->prev_state && |
| 348 | srv->eweight == srv->prev_eweight) |
| 349 | return; |
| 350 | |
| 351 | if (srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 352 | /* server was already up */ |
| 353 | goto out_update_backend; |
| 354 | |
| 355 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 356 | grp->next_weight += srv->eweight; |
| 357 | |
| 358 | if (srv->state & SRV_BACKUP) { |
| 359 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 360 | p->srv_bck++; |
| 361 | |
| 362 | if (p->lbprm.fbck) { |
| 363 | /* we may have restored a backup server prior to fbck, |
| 364 | * in which case it should replace it. |
| 365 | */ |
| 366 | struct server *srv2 = srv; |
| 367 | do { |
| 368 | srv2 = srv2->next; |
| 369 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 370 | if (srv2) |
| 371 | p->lbprm.fbck = srv; |
| 372 | } |
| 373 | } else { |
| 374 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 375 | p->srv_act++; |
| 376 | } |
| 377 | |
| 378 | /* note that eweight cannot be 0 here */ |
| 379 | fwrr_get_srv(srv); |
| 380 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 381 | fwrr_queue_srv(srv); |
| 382 | |
| 383 | out_update_backend: |
| 384 | /* check/update tot_used, tot_weight */ |
| 385 | update_backend_weight(p); |
| 386 | srv->prev_state = srv->state; |
| 387 | srv->prev_eweight = srv->eweight; |
| 388 | } |
| 389 | |
| 390 | /* This function must be called after an update to server <srv>'s effective |
| 391 | * weight. It may be called after a state change too. |
| 392 | */ |
| 393 | static void fwrr_update_server_weight(struct server *srv) |
| 394 | { |
| 395 | int old_state, new_state; |
| 396 | struct proxy *p = srv->proxy; |
| 397 | struct fwrr_group *grp; |
| 398 | |
| 399 | if (srv->state == srv->prev_state && |
| 400 | srv->eweight == srv->prev_eweight) |
| 401 | return; |
| 402 | |
| 403 | /* If changing the server's weight changes its state, we simply apply |
| 404 | * the procedures we already have for status change. If the state |
| 405 | * remains down, the server is not in any tree, so it's as easy as |
| 406 | * updating its values. If the state remains up with different weights, |
| 407 | * there are some computations to perform to find a new place and |
| 408 | * possibly a new tree for this server. |
| 409 | */ |
| 410 | |
| 411 | old_state = srv_is_usable(srv->prev_state, srv->prev_eweight); |
| 412 | new_state = srv_is_usable(srv->state, srv->eweight); |
| 413 | |
| 414 | if (!old_state && !new_state) { |
| 415 | srv->prev_state = srv->state; |
| 416 | srv->prev_eweight = srv->eweight; |
| 417 | return; |
| 418 | } |
| 419 | else if (!old_state && new_state) { |
| 420 | fwrr_set_server_status_up(srv); |
| 421 | return; |
| 422 | } |
| 423 | else if (old_state && !new_state) { |
| 424 | fwrr_set_server_status_down(srv); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 429 | grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight; |
| 430 | |
| 431 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 432 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 433 | |
| 434 | if (srv->lb_tree == grp->init) { |
| 435 | fwrr_dequeue_srv(srv); |
| 436 | fwrr_queue_by_weight(grp->init, srv); |
| 437 | } |
| 438 | else if (!srv->lb_tree) { |
| 439 | /* FIXME: server was down. This is not possible right now but |
| 440 | * may be needed soon for slowstart or graceful shutdown. |
| 441 | */ |
| 442 | fwrr_dequeue_srv(srv); |
| 443 | fwrr_get_srv(srv); |
| 444 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 445 | fwrr_queue_srv(srv); |
| 446 | } else { |
| 447 | /* The server is either active or in the next queue. If it's |
| 448 | * still in the active queue and it has not consumed all of its |
| 449 | * places, let's adjust its next position. |
| 450 | */ |
| 451 | fwrr_get_srv(srv); |
| 452 | |
| 453 | if (srv->eweight > 0) { |
| 454 | int prev_next = srv->npos; |
| 455 | int step = grp->next_weight / srv->eweight; |
| 456 | |
| 457 | srv->npos = srv->lpos + step; |
| 458 | srv->rweight = 0; |
| 459 | |
| 460 | if (srv->npos > prev_next) |
| 461 | srv->npos = prev_next; |
| 462 | if (srv->npos < grp->curr_pos + 2) |
| 463 | srv->npos = grp->curr_pos + step; |
| 464 | } else { |
| 465 | /* push it into the next tree */ |
| 466 | srv->npos = grp->curr_pos + grp->curr_weight; |
| 467 | } |
| 468 | |
| 469 | fwrr_dequeue_srv(srv); |
| 470 | fwrr_queue_srv(srv); |
| 471 | } |
| 472 | |
| 473 | update_backend_weight(p); |
| 474 | srv->prev_state = srv->state; |
| 475 | srv->prev_eweight = srv->eweight; |
| 476 | } |
| 477 | |
| 478 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 479 | * function is meant to be called when a server is going down or has its |
| 480 | * weight disabled. |
| 481 | */ |
| 482 | static inline void fwrr_remove_from_tree(struct server *s) |
| 483 | { |
| 484 | s->lb_tree = NULL; |
| 485 | } |
| 486 | |
| 487 | /* Queue a server in the weight tree <root>, assuming the weight is >0. |
| 488 | * We want to sort them by inverted weights, because we need to place |
| 489 | * heavy servers first in order to get a smooth distribution. |
| 490 | */ |
| 491 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s) |
| 492 | { |
| 493 | /* eweight can be as high as 256*255 */ |
| 494 | s->lb_node.key = BE_WEIGHT_SCALE*255 - s->eweight; |
| 495 | eb32_insert(root, &s->lb_node); |
| 496 | s->lb_tree = root; |
| 497 | } |
| 498 | |
| 499 | /* This function is responsible for building the weight trees in case of fast |
| 500 | * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight |
| 501 | * ratio. Both active and backup groups are initialized. |
| 502 | */ |
| 503 | void fwrr_init_server_groups(struct proxy *p) |
| 504 | { |
| 505 | struct server *srv; |
| 506 | struct eb_root init_head = EB_ROOT; |
| 507 | |
| 508 | p->lbprm.set_server_status_up = fwrr_set_server_status_up; |
| 509 | p->lbprm.set_server_status_down = fwrr_set_server_status_down; |
| 510 | p->lbprm.update_server_eweight = fwrr_update_server_weight; |
| 511 | |
| 512 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 513 | for (srv = p->srv; srv; srv = srv->next) { |
| 514 | srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE; |
| 515 | srv->prev_state = srv->state; |
| 516 | } |
| 517 | |
| 518 | recount_servers(p); |
| 519 | update_backend_weight(p); |
| 520 | |
| 521 | /* prepare the active servers group */ |
| 522 | p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight = |
| 523 | p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact; |
| 524 | p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 = |
| 525 | p->lbprm.fwrr.act.t1 = init_head; |
| 526 | p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0; |
| 527 | p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1; |
| 528 | |
| 529 | /* prepare the backup servers group */ |
| 530 | p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight = |
| 531 | p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck; |
| 532 | p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 = |
| 533 | p->lbprm.fwrr.bck.t1 = init_head; |
| 534 | p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0; |
| 535 | p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1; |
| 536 | |
| 537 | /* queue active and backup servers in two distinct groups */ |
| 538 | for (srv = p->srv; srv; srv = srv->next) { |
| 539 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 540 | continue; |
| 541 | fwrr_queue_by_weight((srv->state & SRV_BACKUP) ? |
| 542 | p->lbprm.fwrr.bck.init : |
| 543 | p->lbprm.fwrr.act.init, |
| 544 | srv); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* simply removes a server from a weight tree */ |
| 549 | static inline void fwrr_dequeue_srv(struct server *s) |
| 550 | { |
| 551 | eb32_delete(&s->lb_node); |
| 552 | } |
| 553 | |
| 554 | /* queues a server into the appropriate group and tree depending on its |
| 555 | * backup status, and ->npos. If the server is disabled, simply assign |
| 556 | * it to the NULL tree. |
| 557 | */ |
| 558 | static void fwrr_queue_srv(struct server *s) |
| 559 | { |
| 560 | struct proxy *p = s->proxy; |
| 561 | struct fwrr_group *grp; |
| 562 | |
| 563 | grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 564 | |
| 565 | /* Delay everything which does not fit into the window and everything |
| 566 | * which does not fit into the theorical new window. |
| 567 | */ |
| 568 | if (!srv_is_usable(s->state, s->eweight)) { |
| 569 | fwrr_remove_from_tree(s); |
| 570 | } |
| 571 | else if (s->eweight <= 0 || |
| 572 | s->npos >= 2 * grp->curr_weight || |
| 573 | s->npos >= grp->curr_weight + grp->next_weight) { |
| 574 | /* put into next tree, and readjust npos in case we could |
| 575 | * finally take this back to current. */ |
| 576 | s->npos -= grp->curr_weight; |
| 577 | fwrr_queue_by_weight(grp->next, s); |
| 578 | } |
| 579 | else { |
| 580 | /* FIXME: we want to multiply by a constant to avoid overrides |
| 581 | * after weight changes, but this can easily overflow on 32-bit |
| 582 | * values. We need to change this for a 64-bit tree, and keep |
| 583 | * the 65536 factor for optimal smoothness (both rweight and |
| 584 | * eweight are 16 bit entities). s->npos is bound by the number |
| 585 | * of servers times the maximum eweight (~= nsrv << 16). |
| 586 | */ |
| 587 | //s->lb_node.key = grp->curr_weight * s->npos + s->rweight - s->eweight; |
| 588 | //s->lb_node.key = 65536 * s->npos + s->rweight - s->eweight; |
| 589 | s->lb_node.key = 16 * s->npos + (s->rweight - s->eweight) / 4096; |
| 590 | eb32i_insert(&grp->curr, &s->lb_node); |
| 591 | s->lb_tree = &grp->curr; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* prepares a server when extracting it from the "init" tree */ |
| 596 | static inline void fwrr_get_srv_init(struct server *s) |
| 597 | { |
| 598 | s->npos = s->rweight = 0; |
| 599 | } |
| 600 | |
| 601 | /* prepares a server when extracting it from the "next" tree */ |
| 602 | static inline void fwrr_get_srv_next(struct server *s) |
| 603 | { |
| 604 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 605 | &s->proxy->lbprm.fwrr.bck : |
| 606 | &s->proxy->lbprm.fwrr.act; |
| 607 | |
| 608 | s->npos += grp->curr_weight; |
| 609 | } |
| 610 | |
| 611 | /* prepares a server when it was marked down */ |
| 612 | static inline void fwrr_get_srv_down(struct server *s) |
| 613 | { |
| 614 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 615 | &s->proxy->lbprm.fwrr.bck : |
| 616 | &s->proxy->lbprm.fwrr.act; |
| 617 | |
| 618 | s->npos = grp->curr_pos; |
| 619 | } |
| 620 | |
| 621 | /* prepares a server when extracting it from its tree */ |
| 622 | static void fwrr_get_srv(struct server *s) |
| 623 | { |
| 624 | struct proxy *p = s->proxy; |
| 625 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 626 | &p->lbprm.fwrr.bck : |
| 627 | &p->lbprm.fwrr.act; |
| 628 | |
| 629 | if (s->lb_tree == grp->init) { |
| 630 | fwrr_get_srv_init(s); |
| 631 | } |
| 632 | else if (s->lb_tree == grp->next) { |
| 633 | fwrr_get_srv_next(s); |
| 634 | } |
| 635 | else if (s->lb_tree == NULL) { |
| 636 | fwrr_get_srv_down(s); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty |
| 641 | * when this happens, and "next" filled with servers sorted by weights. |
| 642 | */ |
| 643 | static inline void fwrr_switch_trees(struct fwrr_group *grp) |
| 644 | { |
| 645 | struct eb_root *swap; |
| 646 | swap = grp->init; |
| 647 | grp->init = grp->next; |
| 648 | grp->next = swap; |
| 649 | grp->curr_weight = grp->next_weight; |
| 650 | grp->curr_pos = grp->curr_weight; |
| 651 | } |
| 652 | |
| 653 | /* return next server from the current tree in FWRR group <grp>, or a server |
| 654 | * from the "init" tree if appropriate. If both trees are empty, return NULL. |
| 655 | */ |
| 656 | static struct server *fwrr_get_server_from_group(struct fwrr_group *grp) |
| 657 | { |
| 658 | struct eb32_node *node; |
| 659 | struct server *s; |
| 660 | |
| 661 | node = eb32_first(&grp->curr); |
| 662 | s = eb32_entry(node, struct server, lb_node); |
| 663 | |
| 664 | if (!node || s->npos > grp->curr_pos) { |
| 665 | /* either we have no server left, or we have a hole */ |
| 666 | struct eb32_node *node2; |
| 667 | node2 = eb32_first(grp->init); |
| 668 | if (node2) { |
| 669 | node = node2; |
| 670 | s = eb32_entry(node, struct server, lb_node); |
| 671 | fwrr_get_srv_init(s); |
| 672 | if (s->eweight == 0) /* FIXME: is it possible at all ? */ |
| 673 | node = NULL; |
| 674 | } |
| 675 | } |
| 676 | if (node) |
| 677 | return s; |
| 678 | else |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* Computes next position of server <s> in the group. It is mandatory for <s> |
| 683 | * to have a non-zero, positive eweight. |
| 684 | */ |
| 685 | static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s) |
| 686 | { |
| 687 | if (!s->npos) { |
| 688 | /* first time ever for this server */ |
| 689 | s->lpos = grp->curr_pos; |
| 690 | s->npos = grp->curr_pos + grp->next_weight / s->eweight; |
| 691 | s->rweight += grp->next_weight % s->eweight; |
| 692 | |
| 693 | if (s->rweight >= s->eweight) { |
| 694 | s->rweight -= s->eweight; |
| 695 | s->npos++; |
| 696 | } |
| 697 | } else { |
| 698 | s->lpos = s->npos; |
| 699 | s->npos += grp->next_weight / s->eweight; |
| 700 | s->rweight += grp->next_weight % s->eweight; |
| 701 | |
| 702 | if (s->rweight >= s->eweight) { |
| 703 | s->rweight -= s->eweight; |
| 704 | s->npos++; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* Return next server from the current tree in backend <p>, or a server from |
| 710 | * the init tree if appropriate. If both trees are empty, return NULL. |
| 711 | * Saturated servers are skipped and requeued. |
| 712 | */ |
| 713 | static struct server *fwrr_get_next_server(struct proxy *p) |
| 714 | { |
| 715 | struct server *srv; |
| 716 | struct fwrr_group *grp; |
| 717 | struct server *full; |
| 718 | int switched; |
| 719 | |
| 720 | if (p->srv_act) |
| 721 | grp = &p->lbprm.fwrr.act; |
| 722 | else if (p->lbprm.fbck) |
| 723 | return p->lbprm.fbck; |
| 724 | else if (p->srv_bck) |
| 725 | grp = &p->lbprm.fwrr.bck; |
| 726 | else |
| 727 | return NULL; |
| 728 | |
| 729 | switched = 0; |
| 730 | full = NULL; /* NULL-terminated list of saturated servers */ |
| 731 | while (1) { |
| 732 | /* if we see an empty group, let's first try to collect weights |
| 733 | * which might have recently changed. |
| 734 | */ |
| 735 | if (!grp->curr_weight) |
| 736 | grp->curr_pos = grp->curr_weight = grp->next_weight; |
| 737 | |
| 738 | /* get first server from the "current" tree. When the end of |
| 739 | * the tree is reached, we may have to switch, but only once. |
| 740 | */ |
| 741 | while (1) { |
| 742 | srv = fwrr_get_server_from_group(grp); |
| 743 | if (srv) |
| 744 | break; |
| 745 | if (switched) |
| 746 | goto requeue_servers; |
| 747 | switched = 1; |
| 748 | fwrr_switch_trees(grp); |
| 749 | |
| 750 | } |
| 751 | |
| 752 | /* OK, we have a server. However, it may be saturated, in which |
| 753 | * case we don't want to reconsider it for now. We'll update |
| 754 | * its position and dequeue it anyway, so that we can move it |
| 755 | * to a better place afterwards. |
| 756 | */ |
| 757 | fwrr_update_position(grp, srv); |
| 758 | fwrr_dequeue_srv(srv); |
| 759 | grp->curr_pos++; |
| 760 | if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) |
| 761 | break; |
| 762 | |
| 763 | /* the server is saturated, let's chain it for later reinsertion */ |
| 764 | srv->next_full = full; |
| 765 | full = srv; |
| 766 | } |
| 767 | |
| 768 | /* OK, we got the best server, let's update it */ |
| 769 | fwrr_queue_srv(srv); |
| 770 | |
| 771 | requeue_servers: |
| 772 | if (unlikely(full)) { |
| 773 | if (switched) { |
| 774 | /* the tree has switched, requeue all extracted servers |
| 775 | * into "init", because their place was lost, and only |
| 776 | * their weight matters. |
| 777 | */ |
| 778 | do { |
| 779 | fwrr_queue_by_weight(grp->init, full); |
| 780 | full = full->next_full; |
| 781 | } while (full); |
| 782 | } else { |
| 783 | /* requeue all extracted servers just as if they were consumed |
| 784 | * so that they regain their expected place. |
| 785 | */ |
| 786 | do { |
| 787 | fwrr_queue_srv(full); |
| 788 | full = full->next_full; |
| 789 | } while (full); |
| 790 | } |
| 791 | } |
| 792 | return srv; |
| 793 | } |
| 794 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 795 | /* |
| 796 | * This function tries to find a running server for the proxy <px> following |
| 797 | * the URL parameter hash method. It looks for a specific parameter in the |
| 798 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 799 | * performance by avoiding bounces between servers in contexts where sessions |
| 800 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 801 | * is returned. If any server is found, it will be returned. If no valid server |
| 802 | * is found, NULL is returned. |
| 803 | * |
| 804 | */ |
| 805 | struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len) |
| 806 | { |
| 807 | unsigned long hash = 0; |
| 808 | char *p; |
| 809 | int plen; |
| 810 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 811 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 812 | return NULL; |
| 813 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 814 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 815 | recalc_server_map(px); |
| 816 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 817 | p = memchr(uri, '?', uri_len); |
| 818 | if (!p) |
| 819 | return NULL; |
| 820 | p++; |
| 821 | |
| 822 | uri_len -= (p - uri); |
| 823 | plen = px->url_param_len; |
| 824 | |
| 825 | if (uri_len <= plen) |
| 826 | return NULL; |
| 827 | |
| 828 | while (uri_len > plen) { |
| 829 | /* Look for the parameter name followed by an equal symbol */ |
| 830 | if (p[plen] == '=') { |
| 831 | /* skip the equal symbol */ |
| 832 | uri = p; |
| 833 | p += plen + 1; |
| 834 | uri_len -= plen + 1; |
| 835 | if (memcmp(uri, px->url_param_name, plen) == 0) { |
| 836 | /* OK, we have the parameter here at <uri>, and |
| 837 | * the value after the equal sign, at <p> |
| 838 | */ |
| 839 | while (uri_len && *p != '&') { |
| 840 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 841 | uri_len--; |
| 842 | p++; |
| 843 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 844 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | /* skip to next parameter */ |
| 849 | uri = p; |
| 850 | p = memchr(uri, '&', uri_len); |
| 851 | if (!p) |
| 852 | return NULL; |
| 853 | p++; |
| 854 | uri_len -= (p - uri); |
| 855 | } |
| 856 | return NULL; |
| 857 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 858 | |
| 859 | /* |
| 860 | * This function marks the session as 'assigned' in direct or dispatch modes, |
| 861 | * or tries to assign one in balance mode, according to the algorithm. It does |
| 862 | * nothing if the session had already been assigned a server. |
| 863 | * |
| 864 | * It may return : |
| 865 | * SRV_STATUS_OK if everything is OK. s->srv will be valid. |
| 866 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 867 | * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL. |
| 868 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 869 | * |
| 870 | * Upon successful return, the session flag SN_ASSIGNED to indicate that it does |
| 871 | * not need to be called anymore. This usually means that s->srv can be trusted |
| 872 | * in balance and direct modes. This flag is not cleared, so it's to the caller |
| 873 | * to clear it if required (eg: redispatch). |
| 874 | * |
| 875 | */ |
| 876 | |
| 877 | int assign_server(struct session *s) |
| 878 | { |
| 879 | #ifdef DEBUG_FULL |
| 880 | fprintf(stderr,"assign_server : s=%p\n",s); |
| 881 | #endif |
| 882 | |
| 883 | if (s->pend_pos) |
| 884 | return SRV_STATUS_INTERNAL; |
| 885 | |
| 886 | if (!(s->flags & SN_ASSIGNED)) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 887 | if (s->be->lbprm.algo & BE_LB_ALGO) { |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 888 | int len; |
| 889 | |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 890 | if (s->flags & SN_DIRECT) { |
| 891 | s->flags |= SN_ASSIGNED; |
| 892 | return SRV_STATUS_OK; |
| 893 | } |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 894 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 895 | if (!s->be->lbprm.tot_weight) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 896 | return SRV_STATUS_NOSRV; |
| 897 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 898 | switch (s->be->lbprm.algo & BE_LB_ALGO) { |
| 899 | case BE_LB_ALGO_RR: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 900 | s->srv = fwrr_get_next_server(s->be); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 901 | if (!s->srv) |
| 902 | return SRV_STATUS_FULL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 903 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 904 | case BE_LB_ALGO_SH: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 905 | if (s->cli_addr.ss_family == AF_INET) |
| 906 | len = 4; |
| 907 | else if (s->cli_addr.ss_family == AF_INET6) |
| 908 | len = 16; |
| 909 | else /* unknown IP family */ |
| 910 | return SRV_STATUS_INTERNAL; |
| 911 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 912 | s->srv = get_server_sh(s->be, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 914 | len); |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 915 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 916 | case BE_LB_ALGO_UH: |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 917 | /* URI hashing */ |
| 918 | s->srv = get_server_uh(s->be, |
| 919 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 920 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 921 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 922 | case BE_LB_ALGO_PH: |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 923 | /* URL Parameter hashing */ |
| 924 | s->srv = get_server_ph(s->be, |
| 925 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 926 | s->txn.req.sl.rq.u_l); |
| 927 | if (!s->srv) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 928 | /* parameter not found, fall back to round robin on the map */ |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 929 | s->srv = get_server_rr_with_conns(s->be); |
| 930 | if (!s->srv) |
| 931 | return SRV_STATUS_FULL; |
| 932 | } |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 933 | break; |
| 934 | default: |
| 935 | /* unknown balancing algorithm */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 936 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 937 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 938 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 939 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 940 | if (!s->srv_addr.sin_addr.s_addr) |
| 941 | return SRV_STATUS_NOSRV; |
| 942 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 943 | else if (!*(int *)&s->be->dispatch_addr.sin_addr && |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 944 | !(s->fe->options & PR_O_TRANSP)) { |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 945 | return SRV_STATUS_NOSRV; |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 946 | } |
| 947 | s->flags |= SN_ASSIGNED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | } |
| 949 | return SRV_STATUS_OK; |
| 950 | } |
| 951 | |
| 952 | |
| 953 | /* |
| 954 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 955 | * The address is taken from the currently assigned server, or from the |
| 956 | * dispatch or transparent address. |
| 957 | * |
| 958 | * It may return : |
| 959 | * SRV_STATUS_OK if everything is OK. |
| 960 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 961 | * |
| 962 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 963 | * not cleared, so it's to the caller to clear it if required. |
| 964 | * |
| 965 | */ |
| 966 | int assign_server_address(struct session *s) |
| 967 | { |
| 968 | #ifdef DEBUG_FULL |
| 969 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 970 | #endif |
| 971 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 972 | if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 973 | /* A server is necessarily known for this session */ |
| 974 | if (!(s->flags & SN_ASSIGNED)) |
| 975 | return SRV_STATUS_INTERNAL; |
| 976 | |
| 977 | s->srv_addr = s->srv->addr; |
| 978 | |
| 979 | /* if this server remaps proxied ports, we'll use |
| 980 | * the port the client connected to with an offset. */ |
| 981 | if (s->srv->state & SRV_MAPPORTS) { |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 982 | if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET)) |
| 983 | get_frt_addr(s); |
| 984 | if (s->frt_addr.ss_family == AF_INET) { |
| 985 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 986 | ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port)); |
| 987 | } else { |
| 988 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 989 | ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port)); |
| 990 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 991 | } |
| 992 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 993 | else if (*(int *)&s->be->dispatch_addr.sin_addr) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 994 | /* connect to the defined dispatch addr */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 995 | s->srv_addr = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 996 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 997 | else if (s->fe->options & PR_O_TRANSP) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 998 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
| 999 | socklen_t salen = sizeof(s->srv_addr); |
| 1000 | |
| 1001 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1002 | qfprintf(stderr, "Cannot get original server address.\n"); |
| 1003 | return SRV_STATUS_INTERNAL; |
| 1004 | } |
| 1005 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1006 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 1007 | /* If HTTP PROXY option is set, then server is already assigned |
| 1008 | * during incoming client request parsing. */ |
| 1009 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 1010 | else { |
| 1011 | /* no server and no LB algorithm ! */ |
| 1012 | return SRV_STATUS_INTERNAL; |
| 1013 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1014 | |
| 1015 | s->flags |= SN_ADDR_SET; |
| 1016 | return SRV_STATUS_OK; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /* This function assigns a server to session <s> if required, and can add the |
| 1021 | * connection to either the assigned server's queue or to the proxy's queue. |
| 1022 | * |
| 1023 | * Returns : |
| 1024 | * |
| 1025 | * SRV_STATUS_OK if everything is OK. |
| 1026 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 1027 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 1028 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
| 1029 | * connection could not be queued. |
| 1030 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 1031 | * |
| 1032 | */ |
| 1033 | int assign_server_and_queue(struct session *s) |
| 1034 | { |
| 1035 | struct pendconn *p; |
| 1036 | int err; |
| 1037 | |
| 1038 | if (s->pend_pos) |
| 1039 | return SRV_STATUS_INTERNAL; |
| 1040 | |
| 1041 | if (s->flags & SN_ASSIGNED) { |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 1042 | if (s->srv && s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) { |
| 1043 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 1044 | s->srv = NULL; |
| 1045 | http_flush_cookie_flags(&s->txn); |
| 1046 | } else { |
| 1047 | /* a server does not need to be assigned, perhaps because we're in |
| 1048 | * direct mode, or in dispatch or transparent modes where the server |
| 1049 | * is not needed. |
| 1050 | */ |
| 1051 | if (s->srv && |
| 1052 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 1053 | p = pendconn_add(s); |
| 1054 | if (p) |
| 1055 | return SRV_STATUS_QUEUED; |
| 1056 | else |
| 1057 | return SRV_STATUS_FULL; |
| 1058 | } |
| 1059 | return SRV_STATUS_OK; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1060 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* a server needs to be assigned */ |
| 1064 | err = assign_server(s); |
| 1065 | switch (err) { |
| 1066 | case SRV_STATUS_OK: |
| 1067 | /* in balance mode, we might have servers with connection limits */ |
| 1068 | if (s->srv && |
| 1069 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 1070 | p = pendconn_add(s); |
| 1071 | if (p) |
| 1072 | return SRV_STATUS_QUEUED; |
| 1073 | else |
| 1074 | return SRV_STATUS_FULL; |
| 1075 | } |
| 1076 | return SRV_STATUS_OK; |
| 1077 | |
| 1078 | case SRV_STATUS_FULL: |
| 1079 | /* queue this session into the proxy's queue */ |
| 1080 | p = pendconn_add(s); |
| 1081 | if (p) |
| 1082 | return SRV_STATUS_QUEUED; |
| 1083 | else |
| 1084 | return SRV_STATUS_FULL; |
| 1085 | |
| 1086 | case SRV_STATUS_NOSRV: |
| 1087 | case SRV_STATUS_INTERNAL: |
| 1088 | return err; |
| 1089 | default: |
| 1090 | return SRV_STATUS_INTERNAL; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | /* |
| 1096 | * This function initiates a connection to the server assigned to this session |
| 1097 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 1098 | * It can return one of : |
| 1099 | * - SN_ERR_NONE if everything's OK |
| 1100 | * - SN_ERR_SRVTO if there are no more servers |
| 1101 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1102 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1103 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1104 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1105 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1106 | */ |
| 1107 | int connect_server(struct session *s) |
| 1108 | { |
| 1109 | int fd, err; |
| 1110 | |
| 1111 | if (!(s->flags & SN_ADDR_SET)) { |
| 1112 | err = assign_server_address(s); |
| 1113 | if (err != SRV_STATUS_OK) |
| 1114 | return SN_ERR_INTERNAL; |
| 1115 | } |
| 1116 | |
| 1117 | if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 1118 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 1119 | |
| 1120 | if (errno == ENFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1121 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1122 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1123 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1124 | else if (errno == EMFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1125 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1126 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1127 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1128 | else if (errno == ENOBUFS || errno == ENOMEM) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1129 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1130 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1131 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1132 | /* this is a resource error */ |
| 1133 | return SN_ERR_RESOURCE; |
| 1134 | } |
| 1135 | |
| 1136 | if (fd >= global.maxsock) { |
| 1137 | /* do not log anything there, it's a normal condition when this option |
| 1138 | * is used to serialize connections to a server ! |
| 1139 | */ |
| 1140 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1141 | close(fd); |
| 1142 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 1143 | } |
| 1144 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1145 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1146 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1147 | /* TCP splicing supported by both FE and BE */ |
| 1148 | tcp_splice_initfd(s->cli_fd, fd); |
| 1149 | } |
| 1150 | #endif |
| 1151 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1152 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1153 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 1154 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 1155 | close(fd); |
| 1156 | return SN_ERR_INTERNAL; |
| 1157 | } |
| 1158 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1159 | if (s->be->options & PR_O_TCP_SRV_KA) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1160 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1161 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 1162 | if (s->be->options & PR_O_TCP_NOLING) |
| 1163 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 1164 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1165 | /* allow specific binding : |
| 1166 | * - server-specific at first |
| 1167 | * - proxy-specific next |
| 1168 | */ |
| 1169 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 1170 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1171 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 1172 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1173 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1174 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1175 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1176 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1177 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1178 | return SN_ERR_RESOURCE; |
| 1179 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1180 | #ifdef CONFIG_HAP_CTTPROXY |
| 1181 | if (s->srv->state & SRV_TPROXY_MASK) { |
| 1182 | struct in_tproxy itp1, itp2; |
| 1183 | memset(&itp1, 0, sizeof(itp1)); |
| 1184 | |
| 1185 | itp1.op = TPROXY_ASSIGN; |
| 1186 | switch (s->srv->state & SRV_TPROXY_MASK) { |
| 1187 | case SRV_TPROXY_ADDR: |
| 1188 | itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr; |
| 1189 | itp1.v.addr.fport = s->srv->tproxy_addr.sin_port; |
| 1190 | break; |
| 1191 | case SRV_TPROXY_CLI: |
| 1192 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 1193 | /* fall through */ |
| 1194 | case SRV_TPROXY_CIP: |
| 1195 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1196 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | /* set connect flag on socket */ |
| 1201 | itp2.op = TPROXY_FLAGS; |
| 1202 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 1203 | |
| 1204 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 1205 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 1206 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1207 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1208 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1209 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1210 | "Cannot bind to tproxy source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1211 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1212 | return SN_ERR_RESOURCE; |
| 1213 | } |
| 1214 | } |
| 1215 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1216 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1217 | else if (s->be->options & PR_O_BIND_SRC) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1218 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1219 | if (bind(fd, (struct sockaddr *)&s->be->source_addr, sizeof(s->be->source_addr)) == -1) { |
| 1220 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->be->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1221 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1222 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1223 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1224 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1225 | return SN_ERR_RESOURCE; |
| 1226 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1227 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1228 | if (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1229 | struct in_tproxy itp1, itp2; |
| 1230 | memset(&itp1, 0, sizeof(itp1)); |
| 1231 | |
| 1232 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1233 | switch (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1234 | case PR_O_TPXY_ADDR: |
| 1235 | itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr; |
| 1236 | itp1.v.addr.fport = s->srv->tproxy_addr.sin_port; |
| 1237 | break; |
| 1238 | case PR_O_TPXY_CLI: |
| 1239 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 1240 | /* fall through */ |
| 1241 | case PR_O_TPXY_CIP: |
| 1242 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1243 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| 1247 | /* set connect flag on socket */ |
| 1248 | itp2.op = TPROXY_FLAGS; |
| 1249 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 1250 | |
| 1251 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 1252 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 1253 | Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1254 | s->be->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1255 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1256 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1257 | "Cannot bind to tproxy source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1258 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1259 | return SN_ERR_RESOURCE; |
| 1260 | } |
| 1261 | } |
| 1262 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1266 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1267 | |
| 1268 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1269 | char *msg; |
| 1270 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1271 | msg = "no free ports"; |
| 1272 | else |
| 1273 | msg = "local address already in use"; |
| 1274 | |
| 1275 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
| 1276 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1277 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1278 | "Connect() failed for server %s/%s: %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1279 | s->be->id, s->srv->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1280 | return SN_ERR_RESOURCE; |
| 1281 | } else if (errno == ETIMEDOUT) { |
| 1282 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 1283 | close(fd); |
| 1284 | return SN_ERR_SRVTO; |
| 1285 | } else { |
| 1286 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 1287 | //qfprintf(stderr,"Connect(): %d", errno); |
| 1288 | close(fd); |
| 1289 | return SN_ERR_SRVCL; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | fdtab[fd].owner = s->task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1294 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1295 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1296 | fdtab[fd].cb[DIR_RD].b = s->rep; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 1297 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1298 | fdtab[fd].cb[DIR_WR].b = s->req; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 1299 | |
| 1300 | fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr; |
| 1301 | fdtab[fd].peerlen = sizeof(s->srv_addr); |
| 1302 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1303 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1304 | |
| 1305 | fd_insert(fd); |
| 1306 | if (s->srv) { |
| 1307 | s->srv->cur_sess++; |
| 1308 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 1309 | s->srv->cur_sess_max = s->srv->cur_sess; |
| 1310 | } |
| 1311 | |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1312 | if (!tv_add_ifset(&s->req->cex, &now, &s->be->contimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1313 | tv_eternity(&s->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1314 | return SN_ERR_NONE; /* connection is OK */ |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | /* |
| 1319 | * This function checks the retry count during the connect() job. |
| 1320 | * It updates the session's srv_state and retries, so that the caller knows |
| 1321 | * what it has to do. It uses the last connection error to set the log when |
| 1322 | * it expires. It returns 1 when it has expired, and 0 otherwise. |
| 1323 | */ |
| 1324 | int srv_count_retry_down(struct session *t, int conn_err) |
| 1325 | { |
| 1326 | /* we are in front of a retryable error */ |
| 1327 | t->conn_retries--; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1328 | if (t->srv) |
| 1329 | t->srv->retries++; |
| 1330 | t->be->retries++; |
| 1331 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1332 | if (t->conn_retries < 0) { |
| 1333 | /* if not retryable anymore, let's abort */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1334 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1335 | srv_close_with_err(t, conn_err, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1336 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1337 | if (t->srv) |
| 1338 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1339 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1340 | |
| 1341 | /* We used to have a free connection slot. Since we'll never use it, |
| 1342 | * we have to inform the server that it may be used by another session. |
| 1343 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1344 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1345 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1346 | return 1; |
| 1347 | } |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | /* |
| 1353 | * This function performs the retryable part of the connect() job. |
| 1354 | * It updates the session's srv_state and retries, so that the caller knows |
| 1355 | * what it has to do. It returns 1 when it breaks out of the loop, or 0 if |
| 1356 | * it needs to redispatch. |
| 1357 | */ |
| 1358 | int srv_retryable_connect(struct session *t) |
| 1359 | { |
| 1360 | int conn_err; |
| 1361 | |
| 1362 | /* This loop ensures that we stop before the last retry in case of a |
| 1363 | * redispatchable server. |
| 1364 | */ |
| 1365 | do { |
| 1366 | /* initiate a connection to the server */ |
| 1367 | conn_err = connect_server(t); |
| 1368 | switch (conn_err) { |
| 1369 | |
| 1370 | case SN_ERR_NONE: |
| 1371 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 1372 | t->srv_state = SV_STCONN; |
| 1373 | return 1; |
| 1374 | |
| 1375 | case SN_ERR_INTERNAL: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1376 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1377 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1378 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1379 | if (t->srv) |
| 1380 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1381 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1382 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1383 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1384 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1385 | return 1; |
| 1386 | } |
| 1387 | /* ensure that we have enough retries left */ |
| 1388 | if (srv_count_retry_down(t, conn_err)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1389 | return 1; |
| 1390 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1391 | } while (t->srv == NULL || t->conn_retries > 0 || !(t->be->options & PR_O_REDISP)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1392 | |
| 1393 | /* We're on our last chance, and the REDISP option was specified. |
| 1394 | * We will ignore cookie and force to balance or use the dispatcher. |
| 1395 | */ |
| 1396 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1397 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1398 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | |
| 1400 | if (t->srv) |
| 1401 | t->srv->failed_conns++; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1402 | t->be->redispatches++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1403 | |
| 1404 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 1405 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1406 | http_flush_cookie_flags(&t->txn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | /* This function performs the "redispatch" part of a connection attempt. It |
| 1412 | * will assign a server if required, queue the connection if required, and |
| 1413 | * handle errors that might arise at this level. It can change the server |
| 1414 | * state. It will return 1 if it encounters an error, switches the server |
| 1415 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 1416 | * that the connection is ready to use. |
| 1417 | */ |
| 1418 | |
| 1419 | int srv_redispatch_connect(struct session *t) |
| 1420 | { |
| 1421 | int conn_err; |
| 1422 | |
| 1423 | /* We know that we don't have any connection pending, so we will |
| 1424 | * try to get a new one, and wait in this state if it's queued |
| 1425 | */ |
| 1426 | conn_err = assign_server_and_queue(t); |
| 1427 | switch (conn_err) { |
| 1428 | case SRV_STATUS_OK: |
| 1429 | break; |
| 1430 | |
| 1431 | case SRV_STATUS_NOSRV: |
| 1432 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1433 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1434 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1435 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1436 | if (t->srv) |
| 1437 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1438 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1439 | |
| 1440 | return 1; |
| 1441 | |
| 1442 | case SRV_STATUS_QUEUED: |
| 1443 | /* FIXME-20060503 : we should use the queue timeout instead */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1444 | if (!tv_add_ifset(&t->req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1445 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1446 | t->srv_state = SV_STIDLE; |
| 1447 | /* do nothing else and do not wake any other session up */ |
| 1448 | return 1; |
| 1449 | |
| 1450 | case SRV_STATUS_FULL: |
| 1451 | case SRV_STATUS_INTERNAL: |
| 1452 | default: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1453 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1454 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1455 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1456 | if (t->srv) |
| 1457 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1458 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1459 | |
| 1460 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1461 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1462 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1463 | return 1; |
| 1464 | } |
| 1465 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 1466 | * means that the connection has not been queued. |
| 1467 | */ |
| 1468 | return 0; |
| 1469 | } |
| 1470 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1471 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 1472 | if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1473 | return px->down_time; |
| 1474 | |
| 1475 | return now.tv_sec - px->last_change + px->down_time; |
| 1476 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1477 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1478 | /* This function parses a "balance" statement in a backend section describing |
| 1479 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 1480 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 1481 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 1482 | * written. The function must be called with <args> pointing to the first word |
| 1483 | * after "balance". |
| 1484 | */ |
| 1485 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 1486 | { |
| 1487 | if (!*(args[0])) { |
| 1488 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1489 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1490 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1495 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1496 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1497 | } |
| 1498 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1499 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1500 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1501 | } |
| 1502 | else if (!strcmp(args[0], "uri")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1503 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1504 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1505 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1506 | else if (!strcmp(args[0], "url_param")) { |
| 1507 | if (!*args[1]) { |
| 1508 | snprintf(err, errlen, "'balance url_param' requires an URL parameter name."); |
| 1509 | return -1; |
| 1510 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1511 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1512 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1513 | if (curproxy->url_param_name) |
| 1514 | free(curproxy->url_param_name); |
| 1515 | curproxy->url_param_name = strdup(args[1]); |
| 1516 | curproxy->url_param_len = strlen(args[1]); |
| 1517 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1518 | else { |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1519 | snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source', 'uri' and 'url_param' options."); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1520 | return -1; |
| 1521 | } |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1525 | /* |
| 1526 | * Local variables: |
| 1527 | * c-indent-level: 8 |
| 1528 | * c-basic-offset: 8 |
| 1529 | * End: |
| 1530 | */ |