Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Backend variables and functions. |
| 3 | * |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 4 | * Copyright 2000-2008 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> |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 21 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 23 | #include <common/debug.h> |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 24 | #include <common/eb32tree.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 25 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 30 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <proto/backend.h> |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 32 | #include <proto/client.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/fd.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 34 | #include <proto/httperr.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | #include <proto/log.h> |
| 36 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 37 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <proto/queue.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 39 | #include <proto/session.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <proto/stream_sock.h> |
| 41 | #include <proto/task.h> |
| 42 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 43 | #ifdef CONFIG_HAP_TCPSPLICE |
| 44 | #include <libtcpsplice.h> |
| 45 | #endif |
| 46 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 47 | static inline void fwrr_remove_from_tree(struct server *s); |
| 48 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s); |
| 49 | static inline void fwrr_dequeue_srv(struct server *s); |
| 50 | static void fwrr_get_srv(struct server *s); |
| 51 | static void fwrr_queue_srv(struct server *s); |
| 52 | |
| 53 | /* This function returns non-zero if a server with the given weight and state |
| 54 | * is usable for LB, otherwise zero. |
| 55 | */ |
| 56 | static inline int srv_is_usable(int state, int weight) |
| 57 | { |
| 58 | if (!weight) |
| 59 | return 0; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 60 | if (state & SRV_GOINGDOWN) |
| 61 | return 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 62 | if (!(state & SRV_RUNNING)) |
| 63 | return 0; |
| 64 | return 1; |
| 65 | } |
| 66 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | /* |
| 68 | * This function recounts the number of usable active and backup servers for |
| 69 | * 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] | 70 | * This function also recomputes the total active and backup weights. However, |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 71 | * it does not update tot_weight nor tot_used. Use update_backend_weight() for |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 72 | * this. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 74 | static void recount_servers(struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | { |
| 76 | struct server *srv; |
| 77 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 78 | px->srv_act = px->srv_bck = 0; |
| 79 | px->lbprm.tot_wact = px->lbprm.tot_wbck = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 80 | px->lbprm.fbck = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 82 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 83 | continue; |
| 84 | |
| 85 | if (srv->state & SRV_BACKUP) { |
| 86 | if (!px->srv_bck && |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 87 | !(px->options & PR_O_USE_ALL_BK)) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 88 | px->lbprm.fbck = srv; |
| 89 | px->srv_bck++; |
| 90 | px->lbprm.tot_wbck += srv->eweight; |
| 91 | } else { |
| 92 | px->srv_act++; |
| 93 | px->lbprm.tot_wact += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 96 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 97 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 98 | /* This function simply updates the backend's tot_weight and tot_used values |
| 99 | * after servers weights have been updated. It is designed to be used after |
| 100 | * recount_servers() or equivalent. |
| 101 | */ |
| 102 | static void update_backend_weight(struct proxy *px) |
| 103 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 104 | if (px->srv_act) { |
| 105 | px->lbprm.tot_weight = px->lbprm.tot_wact; |
| 106 | px->lbprm.tot_used = px->srv_act; |
| 107 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 108 | else if (px->lbprm.fbck) { |
| 109 | /* use only the first backup server */ |
| 110 | px->lbprm.tot_weight = px->lbprm.fbck->eweight; |
| 111 | px->lbprm.tot_used = 1; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 112 | } |
| 113 | else { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 114 | px->lbprm.tot_weight = px->lbprm.tot_wbck; |
| 115 | px->lbprm.tot_used = px->srv_bck; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 116 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* this function updates the map according to server <srv>'s new state */ |
| 120 | static void map_set_server_status_down(struct server *srv) |
| 121 | { |
| 122 | struct proxy *p = srv->proxy; |
| 123 | |
| 124 | if (srv->state == srv->prev_state && |
| 125 | srv->eweight == srv->prev_eweight) |
| 126 | return; |
| 127 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 128 | if (srv_is_usable(srv->state, srv->eweight)) |
| 129 | goto out_update_state; |
| 130 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 131 | /* FIXME: could be optimized since we know what changed */ |
| 132 | recount_servers(p); |
| 133 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 134 | p->lbprm.map.state |= PR_MAP_RECALC; |
| 135 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 136 | srv->prev_state = srv->state; |
| 137 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 140 | /* This function updates the map according to server <srv>'s new state */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 141 | static void map_set_server_status_up(struct server *srv) |
| 142 | { |
| 143 | struct proxy *p = srv->proxy; |
| 144 | |
| 145 | if (srv->state == srv->prev_state && |
| 146 | srv->eweight == srv->prev_eweight) |
| 147 | return; |
| 148 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 149 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 150 | goto out_update_state; |
| 151 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 152 | /* FIXME: could be optimized since we know what changed */ |
| 153 | recount_servers(p); |
| 154 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 155 | p->lbprm.map.state |= PR_MAP_RECALC; |
| 156 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 157 | srv->prev_state = srv->state; |
| 158 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 161 | /* This function recomputes the server map for proxy px. It relies on |
| 162 | * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be |
| 163 | * called after recount_servers(). It also expects px->lbprm.map.srv |
| 164 | * to be allocated with the largest size needed. It updates tot_weight. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | */ |
| 166 | void recalc_server_map(struct proxy *px) |
| 167 | { |
| 168 | int o, tot, flag; |
| 169 | struct server *cur, *best; |
| 170 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 171 | switch (px->lbprm.tot_used) { |
| 172 | case 0: /* no server */ |
| 173 | px->lbprm.map.state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 174 | return; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 175 | case 1: /* only one server, just fill first entry */ |
| 176 | tot = 1; |
| 177 | break; |
| 178 | default: |
| 179 | tot = px->lbprm.tot_weight; |
| 180 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 183 | /* here we *know* that we have some servers */ |
| 184 | if (px->srv_act) |
| 185 | flag = SRV_RUNNING; |
| 186 | else |
| 187 | flag = SRV_RUNNING | SRV_BACKUP; |
| 188 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | /* this algorithm gives priority to the first server, which means that |
| 190 | * it will respect the declaration order for equivalent weights, and |
| 191 | * that whatever the weights, the first server called will always be |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 192 | * the first declared. This is an important asumption for the backup |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | * case, where we want the first server only. |
| 194 | */ |
| 195 | for (cur = px->srv; cur; cur = cur->next) |
| 196 | cur->wscore = 0; |
| 197 | |
| 198 | for (o = 0; o < tot; o++) { |
| 199 | int max = 0; |
| 200 | best = NULL; |
| 201 | for (cur = px->srv; cur; cur = cur->next) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 202 | if (flag == (cur->state & |
| 203 | (SRV_RUNNING | SRV_GOINGDOWN | SRV_BACKUP))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 204 | int v; |
| 205 | |
| 206 | /* If we are forced to return only one server, we don't want to |
| 207 | * go further, because we would return the wrong one due to |
| 208 | * divide overflow. |
| 209 | */ |
| 210 | if (tot == 1) { |
| 211 | best = cur; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 212 | /* note that best->wscore will be wrong but we don't care */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 216 | cur->wscore += cur->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 217 | v = (cur->wscore + tot) / tot; /* result between 0 and 3 */ |
| 218 | if (best == NULL || v > max) { |
| 219 | max = v; |
| 220 | best = cur; |
| 221 | } |
| 222 | } |
| 223 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 224 | px->lbprm.map.srv[o] = best; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | best->wscore -= tot; |
| 226 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 227 | px->lbprm.map.state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 230 | /* This function is responsible of building the server MAP for map-based LB |
| 231 | * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the |
| 232 | * weights if applicable. It should be called only once per proxy, at config |
| 233 | * time. |
| 234 | */ |
| 235 | void init_server_map(struct proxy *p) |
| 236 | { |
| 237 | struct server *srv; |
| 238 | int pgcd; |
| 239 | int act, bck; |
| 240 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 241 | p->lbprm.set_server_status_up = map_set_server_status_up; |
| 242 | p->lbprm.set_server_status_down = map_set_server_status_down; |
| 243 | p->lbprm.update_server_eweight = NULL; |
| 244 | |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 245 | if (!p->srv) |
| 246 | return; |
| 247 | |
| 248 | /* We will factor the weights to reduce the table, |
| 249 | * using Euclide's largest common divisor algorithm |
| 250 | */ |
| 251 | pgcd = p->srv->uweight; |
| 252 | for (srv = p->srv->next; srv && pgcd > 1; srv = srv->next) { |
| 253 | int w = srv->uweight; |
| 254 | while (w) { |
| 255 | int t = pgcd % w; |
| 256 | pgcd = w; |
| 257 | w = t; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* It is sometimes useful to know what factor to apply |
| 262 | * to the backend's effective weight to know its real |
| 263 | * weight. |
| 264 | */ |
| 265 | p->lbprm.wmult = pgcd; |
| 266 | |
| 267 | act = bck = 0; |
| 268 | for (srv = p->srv; srv; srv = srv->next) { |
| 269 | srv->eweight = srv->uweight / pgcd; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 270 | srv->prev_eweight = srv->eweight; |
| 271 | srv->prev_state = srv->state; |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 272 | if (srv->state & SRV_BACKUP) |
| 273 | bck += srv->eweight; |
| 274 | else |
| 275 | act += srv->eweight; |
| 276 | } |
| 277 | |
| 278 | /* this is the largest map we will ever need for this servers list */ |
| 279 | if (act < bck) |
| 280 | act = bck; |
| 281 | |
| 282 | p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *)); |
| 283 | /* recounts servers and their weights */ |
| 284 | p->lbprm.map.state = PR_MAP_RECALC; |
| 285 | recount_servers(p); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 286 | update_backend_weight(p); |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 287 | recalc_server_map(p); |
| 288 | } |
| 289 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 290 | /* This function updates the server trees according to server <srv>'s new |
| 291 | * state. It should be called when server <srv>'s status changes to down. |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 292 | * It is not important whether the server was already down or not. It is not |
| 293 | * important either that the new state is completely down (the caller may not |
| 294 | * know all the variables of a server's state). |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 295 | */ |
| 296 | static void fwrr_set_server_status_down(struct server *srv) |
| 297 | { |
| 298 | struct proxy *p = srv->proxy; |
| 299 | struct fwrr_group *grp; |
| 300 | |
| 301 | if (srv->state == srv->prev_state && |
| 302 | srv->eweight == srv->prev_eweight) |
| 303 | return; |
| 304 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 305 | if (srv_is_usable(srv->state, srv->eweight)) |
| 306 | goto out_update_state; |
| 307 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 308 | if (!srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 309 | /* server was already down */ |
| 310 | goto out_update_backend; |
| 311 | |
| 312 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 313 | grp->next_weight -= srv->prev_eweight; |
| 314 | |
| 315 | if (srv->state & SRV_BACKUP) { |
| 316 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 317 | p->srv_bck--; |
| 318 | |
| 319 | if (srv == p->lbprm.fbck) { |
| 320 | /* we lost the first backup server in a single-backup |
| 321 | * configuration, we must search another one. |
| 322 | */ |
| 323 | struct server *srv2 = p->lbprm.fbck; |
| 324 | do { |
| 325 | srv2 = srv2->next; |
| 326 | } while (srv2 && |
| 327 | !((srv2->state & SRV_BACKUP) && |
| 328 | srv_is_usable(srv2->state, srv2->eweight))); |
| 329 | p->lbprm.fbck = srv2; |
| 330 | } |
| 331 | } else { |
| 332 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 333 | p->srv_act--; |
| 334 | } |
| 335 | |
| 336 | fwrr_dequeue_srv(srv); |
| 337 | fwrr_remove_from_tree(srv); |
| 338 | |
| 339 | out_update_backend: |
| 340 | /* check/update tot_used, tot_weight */ |
| 341 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 342 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 343 | srv->prev_state = srv->state; |
| 344 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* This function updates the server trees according to server <srv>'s new |
| 348 | * state. It should be called when server <srv>'s status changes to up. |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 349 | * It is not important whether the server was already down or not. It is not |
| 350 | * important either that the new state is completely UP (the caller may not |
| 351 | * know all the variables of a server's state). This function will not change |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 352 | * the weight of a server which was already up. |
| 353 | */ |
| 354 | static void fwrr_set_server_status_up(struct server *srv) |
| 355 | { |
| 356 | struct proxy *p = srv->proxy; |
| 357 | struct fwrr_group *grp; |
| 358 | |
| 359 | if (srv->state == srv->prev_state && |
| 360 | srv->eweight == srv->prev_eweight) |
| 361 | return; |
| 362 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 363 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 364 | goto out_update_state; |
| 365 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 366 | if (srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 367 | /* server was already up */ |
| 368 | goto out_update_backend; |
| 369 | |
| 370 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 371 | grp->next_weight += srv->eweight; |
| 372 | |
| 373 | if (srv->state & SRV_BACKUP) { |
| 374 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 375 | p->srv_bck++; |
| 376 | |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 377 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 378 | if (!p->lbprm.fbck) { |
| 379 | /* there was no backup server anymore */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 380 | p->lbprm.fbck = srv; |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 381 | } else { |
| 382 | /* we may have restored a backup server prior to fbck, |
| 383 | * in which case it should replace it. |
| 384 | */ |
| 385 | struct server *srv2 = srv; |
| 386 | do { |
| 387 | srv2 = srv2->next; |
| 388 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 389 | if (srv2) |
| 390 | p->lbprm.fbck = srv; |
| 391 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 392 | } |
| 393 | } else { |
| 394 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 395 | p->srv_act++; |
| 396 | } |
| 397 | |
| 398 | /* note that eweight cannot be 0 here */ |
| 399 | fwrr_get_srv(srv); |
| 400 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 401 | fwrr_queue_srv(srv); |
| 402 | |
| 403 | out_update_backend: |
| 404 | /* check/update tot_used, tot_weight */ |
| 405 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 406 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 407 | srv->prev_state = srv->state; |
| 408 | srv->prev_eweight = srv->eweight; |
| 409 | } |
| 410 | |
| 411 | /* This function must be called after an update to server <srv>'s effective |
| 412 | * weight. It may be called after a state change too. |
| 413 | */ |
| 414 | static void fwrr_update_server_weight(struct server *srv) |
| 415 | { |
| 416 | int old_state, new_state; |
| 417 | struct proxy *p = srv->proxy; |
| 418 | struct fwrr_group *grp; |
| 419 | |
| 420 | if (srv->state == srv->prev_state && |
| 421 | srv->eweight == srv->prev_eweight) |
| 422 | return; |
| 423 | |
| 424 | /* If changing the server's weight changes its state, we simply apply |
| 425 | * the procedures we already have for status change. If the state |
| 426 | * remains down, the server is not in any tree, so it's as easy as |
| 427 | * updating its values. If the state remains up with different weights, |
| 428 | * there are some computations to perform to find a new place and |
| 429 | * possibly a new tree for this server. |
| 430 | */ |
| 431 | |
| 432 | old_state = srv_is_usable(srv->prev_state, srv->prev_eweight); |
| 433 | new_state = srv_is_usable(srv->state, srv->eweight); |
| 434 | |
| 435 | if (!old_state && !new_state) { |
| 436 | srv->prev_state = srv->state; |
| 437 | srv->prev_eweight = srv->eweight; |
| 438 | return; |
| 439 | } |
| 440 | else if (!old_state && new_state) { |
| 441 | fwrr_set_server_status_up(srv); |
| 442 | return; |
| 443 | } |
| 444 | else if (old_state && !new_state) { |
| 445 | fwrr_set_server_status_down(srv); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 450 | grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight; |
| 451 | |
| 452 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 453 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 454 | |
| 455 | if (srv->lb_tree == grp->init) { |
| 456 | fwrr_dequeue_srv(srv); |
| 457 | fwrr_queue_by_weight(grp->init, srv); |
| 458 | } |
| 459 | else if (!srv->lb_tree) { |
| 460 | /* FIXME: server was down. This is not possible right now but |
| 461 | * may be needed soon for slowstart or graceful shutdown. |
| 462 | */ |
| 463 | fwrr_dequeue_srv(srv); |
| 464 | fwrr_get_srv(srv); |
| 465 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 466 | fwrr_queue_srv(srv); |
| 467 | } else { |
| 468 | /* The server is either active or in the next queue. If it's |
| 469 | * still in the active queue and it has not consumed all of its |
| 470 | * places, let's adjust its next position. |
| 471 | */ |
| 472 | fwrr_get_srv(srv); |
| 473 | |
| 474 | if (srv->eweight > 0) { |
| 475 | int prev_next = srv->npos; |
| 476 | int step = grp->next_weight / srv->eweight; |
| 477 | |
| 478 | srv->npos = srv->lpos + step; |
| 479 | srv->rweight = 0; |
| 480 | |
| 481 | if (srv->npos > prev_next) |
| 482 | srv->npos = prev_next; |
| 483 | if (srv->npos < grp->curr_pos + 2) |
| 484 | srv->npos = grp->curr_pos + step; |
| 485 | } else { |
| 486 | /* push it into the next tree */ |
| 487 | srv->npos = grp->curr_pos + grp->curr_weight; |
| 488 | } |
| 489 | |
| 490 | fwrr_dequeue_srv(srv); |
| 491 | fwrr_queue_srv(srv); |
| 492 | } |
| 493 | |
| 494 | update_backend_weight(p); |
| 495 | srv->prev_state = srv->state; |
| 496 | srv->prev_eweight = srv->eweight; |
| 497 | } |
| 498 | |
| 499 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 500 | * function is meant to be called when a server is going down or has its |
| 501 | * weight disabled. |
| 502 | */ |
| 503 | static inline void fwrr_remove_from_tree(struct server *s) |
| 504 | { |
| 505 | s->lb_tree = NULL; |
| 506 | } |
| 507 | |
| 508 | /* Queue a server in the weight tree <root>, assuming the weight is >0. |
| 509 | * We want to sort them by inverted weights, because we need to place |
| 510 | * heavy servers first in order to get a smooth distribution. |
| 511 | */ |
| 512 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s) |
| 513 | { |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 514 | s->lb_node.key = SRV_EWGHT_MAX - s->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 515 | eb32_insert(root, &s->lb_node); |
| 516 | s->lb_tree = root; |
| 517 | } |
| 518 | |
| 519 | /* This function is responsible for building the weight trees in case of fast |
| 520 | * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight |
| 521 | * ratio. Both active and backup groups are initialized. |
| 522 | */ |
| 523 | void fwrr_init_server_groups(struct proxy *p) |
| 524 | { |
| 525 | struct server *srv; |
| 526 | struct eb_root init_head = EB_ROOT; |
| 527 | |
| 528 | p->lbprm.set_server_status_up = fwrr_set_server_status_up; |
| 529 | p->lbprm.set_server_status_down = fwrr_set_server_status_down; |
| 530 | p->lbprm.update_server_eweight = fwrr_update_server_weight; |
| 531 | |
| 532 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 533 | for (srv = p->srv; srv; srv = srv->next) { |
| 534 | srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE; |
| 535 | srv->prev_state = srv->state; |
| 536 | } |
| 537 | |
| 538 | recount_servers(p); |
| 539 | update_backend_weight(p); |
| 540 | |
| 541 | /* prepare the active servers group */ |
| 542 | p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight = |
| 543 | p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact; |
| 544 | p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 = |
| 545 | p->lbprm.fwrr.act.t1 = init_head; |
| 546 | p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0; |
| 547 | p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1; |
| 548 | |
| 549 | /* prepare the backup servers group */ |
| 550 | p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight = |
| 551 | p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck; |
| 552 | p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 = |
| 553 | p->lbprm.fwrr.bck.t1 = init_head; |
| 554 | p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0; |
| 555 | p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1; |
| 556 | |
| 557 | /* queue active and backup servers in two distinct groups */ |
| 558 | for (srv = p->srv; srv; srv = srv->next) { |
| 559 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 560 | continue; |
| 561 | fwrr_queue_by_weight((srv->state & SRV_BACKUP) ? |
| 562 | p->lbprm.fwrr.bck.init : |
| 563 | p->lbprm.fwrr.act.init, |
| 564 | srv); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* simply removes a server from a weight tree */ |
| 569 | static inline void fwrr_dequeue_srv(struct server *s) |
| 570 | { |
| 571 | eb32_delete(&s->lb_node); |
| 572 | } |
| 573 | |
| 574 | /* queues a server into the appropriate group and tree depending on its |
| 575 | * backup status, and ->npos. If the server is disabled, simply assign |
| 576 | * it to the NULL tree. |
| 577 | */ |
| 578 | static void fwrr_queue_srv(struct server *s) |
| 579 | { |
| 580 | struct proxy *p = s->proxy; |
| 581 | struct fwrr_group *grp; |
| 582 | |
| 583 | grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 584 | |
| 585 | /* Delay everything which does not fit into the window and everything |
| 586 | * which does not fit into the theorical new window. |
| 587 | */ |
| 588 | if (!srv_is_usable(s->state, s->eweight)) { |
| 589 | fwrr_remove_from_tree(s); |
| 590 | } |
| 591 | else if (s->eweight <= 0 || |
| 592 | s->npos >= 2 * grp->curr_weight || |
| 593 | s->npos >= grp->curr_weight + grp->next_weight) { |
| 594 | /* put into next tree, and readjust npos in case we could |
| 595 | * finally take this back to current. */ |
| 596 | s->npos -= grp->curr_weight; |
| 597 | fwrr_queue_by_weight(grp->next, s); |
| 598 | } |
| 599 | else { |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 600 | /* The sorting key is stored in units of s->npos * user_weight |
| 601 | * in order to avoid overflows. As stated in backend.h, the |
| 602 | * lower the scale, the rougher the weights modulation, and the |
| 603 | * higher the scale, the lower the number of servers without |
| 604 | * overflow. With this formula, the result is always positive, |
| 605 | * so we can use eb3é_insert(). |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 606 | */ |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 607 | s->lb_node.key = SRV_UWGHT_RANGE * s->npos + |
| 608 | (unsigned)(SRV_EWGHT_MAX + s->rweight - s->eweight) / BE_WEIGHT_SCALE; |
| 609 | |
| 610 | eb32_insert(&grp->curr, &s->lb_node); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 611 | s->lb_tree = &grp->curr; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* prepares a server when extracting it from the "init" tree */ |
| 616 | static inline void fwrr_get_srv_init(struct server *s) |
| 617 | { |
| 618 | s->npos = s->rweight = 0; |
| 619 | } |
| 620 | |
| 621 | /* prepares a server when extracting it from the "next" tree */ |
| 622 | static inline void fwrr_get_srv_next(struct server *s) |
| 623 | { |
| 624 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 625 | &s->proxy->lbprm.fwrr.bck : |
| 626 | &s->proxy->lbprm.fwrr.act; |
| 627 | |
| 628 | s->npos += grp->curr_weight; |
| 629 | } |
| 630 | |
| 631 | /* prepares a server when it was marked down */ |
| 632 | static inline void fwrr_get_srv_down(struct server *s) |
| 633 | { |
| 634 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 635 | &s->proxy->lbprm.fwrr.bck : |
| 636 | &s->proxy->lbprm.fwrr.act; |
| 637 | |
| 638 | s->npos = grp->curr_pos; |
| 639 | } |
| 640 | |
| 641 | /* prepares a server when extracting it from its tree */ |
| 642 | static void fwrr_get_srv(struct server *s) |
| 643 | { |
| 644 | struct proxy *p = s->proxy; |
| 645 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 646 | &p->lbprm.fwrr.bck : |
| 647 | &p->lbprm.fwrr.act; |
| 648 | |
| 649 | if (s->lb_tree == grp->init) { |
| 650 | fwrr_get_srv_init(s); |
| 651 | } |
| 652 | else if (s->lb_tree == grp->next) { |
| 653 | fwrr_get_srv_next(s); |
| 654 | } |
| 655 | else if (s->lb_tree == NULL) { |
| 656 | fwrr_get_srv_down(s); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty |
| 661 | * when this happens, and "next" filled with servers sorted by weights. |
| 662 | */ |
| 663 | static inline void fwrr_switch_trees(struct fwrr_group *grp) |
| 664 | { |
| 665 | struct eb_root *swap; |
| 666 | swap = grp->init; |
| 667 | grp->init = grp->next; |
| 668 | grp->next = swap; |
| 669 | grp->curr_weight = grp->next_weight; |
| 670 | grp->curr_pos = grp->curr_weight; |
| 671 | } |
| 672 | |
| 673 | /* return next server from the current tree in FWRR group <grp>, or a server |
| 674 | * from the "init" tree if appropriate. If both trees are empty, return NULL. |
| 675 | */ |
| 676 | static struct server *fwrr_get_server_from_group(struct fwrr_group *grp) |
| 677 | { |
| 678 | struct eb32_node *node; |
| 679 | struct server *s; |
| 680 | |
| 681 | node = eb32_first(&grp->curr); |
| 682 | s = eb32_entry(node, struct server, lb_node); |
| 683 | |
| 684 | if (!node || s->npos > grp->curr_pos) { |
| 685 | /* either we have no server left, or we have a hole */ |
| 686 | struct eb32_node *node2; |
| 687 | node2 = eb32_first(grp->init); |
| 688 | if (node2) { |
| 689 | node = node2; |
| 690 | s = eb32_entry(node, struct server, lb_node); |
| 691 | fwrr_get_srv_init(s); |
| 692 | if (s->eweight == 0) /* FIXME: is it possible at all ? */ |
| 693 | node = NULL; |
| 694 | } |
| 695 | } |
| 696 | if (node) |
| 697 | return s; |
| 698 | else |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | /* Computes next position of server <s> in the group. It is mandatory for <s> |
| 703 | * to have a non-zero, positive eweight. |
| 704 | */ |
| 705 | static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s) |
| 706 | { |
| 707 | if (!s->npos) { |
| 708 | /* first time ever for this server */ |
| 709 | s->lpos = grp->curr_pos; |
| 710 | s->npos = grp->curr_pos + grp->next_weight / s->eweight; |
| 711 | s->rweight += grp->next_weight % s->eweight; |
| 712 | |
| 713 | if (s->rweight >= s->eweight) { |
| 714 | s->rweight -= s->eweight; |
| 715 | s->npos++; |
| 716 | } |
| 717 | } else { |
| 718 | s->lpos = s->npos; |
| 719 | s->npos += grp->next_weight / s->eweight; |
| 720 | s->rweight += grp->next_weight % s->eweight; |
| 721 | |
| 722 | if (s->rweight >= s->eweight) { |
| 723 | s->rweight -= s->eweight; |
| 724 | s->npos++; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* Return next server from the current tree in backend <p>, or a server from |
| 730 | * the init tree if appropriate. If both trees are empty, return NULL. |
| 731 | * Saturated servers are skipped and requeued. |
| 732 | */ |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 733 | static struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 734 | { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 735 | struct server *srv, *full, *avoided; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 736 | struct fwrr_group *grp; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 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; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 749 | avoided = NULL; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 750 | full = NULL; /* NULL-terminated list of saturated servers */ |
| 751 | while (1) { |
| 752 | /* if we see an empty group, let's first try to collect weights |
| 753 | * which might have recently changed. |
| 754 | */ |
| 755 | if (!grp->curr_weight) |
| 756 | grp->curr_pos = grp->curr_weight = grp->next_weight; |
| 757 | |
| 758 | /* get first server from the "current" tree. When the end of |
| 759 | * the tree is reached, we may have to switch, but only once. |
| 760 | */ |
| 761 | while (1) { |
| 762 | srv = fwrr_get_server_from_group(grp); |
| 763 | if (srv) |
| 764 | break; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 765 | if (switched) { |
| 766 | if (avoided) { |
| 767 | srv = avoided; |
| 768 | break; |
| 769 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 770 | goto requeue_servers; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 771 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 772 | switched = 1; |
| 773 | fwrr_switch_trees(grp); |
| 774 | |
| 775 | } |
| 776 | |
| 777 | /* OK, we have a server. However, it may be saturated, in which |
| 778 | * case we don't want to reconsider it for now. We'll update |
| 779 | * its position and dequeue it anyway, so that we can move it |
| 780 | * to a better place afterwards. |
| 781 | */ |
| 782 | fwrr_update_position(grp, srv); |
| 783 | fwrr_dequeue_srv(srv); |
| 784 | grp->curr_pos++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 785 | if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 786 | /* make sure it is not the server we are trying to exclude... */ |
| 787 | if (srv != srvtoavoid || avoided) |
| 788 | break; |
| 789 | |
| 790 | avoided = srv; /* ...but remember that is was selected yet avoided */ |
| 791 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 792 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 793 | /* the server is saturated or avoided, let's chain it for later reinsertion */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 794 | srv->next_full = full; |
| 795 | full = srv; |
| 796 | } |
| 797 | |
| 798 | /* OK, we got the best server, let's update it */ |
| 799 | fwrr_queue_srv(srv); |
| 800 | |
| 801 | requeue_servers: |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 802 | /* Requeue all extracted servers. If full==srv then it was |
| 803 | * avoided (unsucessfully) and chained, omit it now. |
| 804 | */ |
Willy Tarreau | 70bcfb7 | 2008-01-27 02:21:53 +0100 | [diff] [blame] | 805 | if (unlikely(full != NULL)) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 806 | if (switched) { |
| 807 | /* the tree has switched, requeue all extracted servers |
| 808 | * into "init", because their place was lost, and only |
| 809 | * their weight matters. |
| 810 | */ |
| 811 | do { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 812 | if (likely(full != srv)) |
| 813 | fwrr_queue_by_weight(grp->init, full); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 814 | full = full->next_full; |
| 815 | } while (full); |
| 816 | } else { |
| 817 | /* requeue all extracted servers just as if they were consumed |
| 818 | * so that they regain their expected place. |
| 819 | */ |
| 820 | do { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 821 | if (likely(full != srv)) |
| 822 | fwrr_queue_srv(full); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 823 | full = full->next_full; |
| 824 | } while (full); |
| 825 | } |
| 826 | } |
| 827 | return srv; |
| 828 | } |
| 829 | |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 830 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 831 | * function is meant to be called when a server is going down or has its |
| 832 | * weight disabled. |
| 833 | */ |
| 834 | static inline void fwlc_remove_from_tree(struct server *s) |
| 835 | { |
| 836 | s->lb_tree = NULL; |
| 837 | } |
| 838 | |
| 839 | /* simply removes a server from a tree */ |
| 840 | static inline void fwlc_dequeue_srv(struct server *s) |
| 841 | { |
| 842 | eb32_delete(&s->lb_node); |
| 843 | } |
| 844 | |
| 845 | /* Queue a server in its associated tree, assuming the weight is >0. |
| 846 | * Servers are sorted by #conns/weight. To ensure maximum accuracy, |
| 847 | * we use #conns*SRV_EWGHT_MAX/eweight as the sorting key. |
| 848 | */ |
| 849 | static inline void fwlc_queue_srv(struct server *s) |
| 850 | { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 851 | s->lb_node.key = s->served * SRV_EWGHT_MAX / s->eweight; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 852 | eb32_insert(s->lb_tree, &s->lb_node); |
| 853 | } |
| 854 | |
| 855 | /* Re-position the server in the FWLC tree after it has been assigned one |
| 856 | * connection or after it has released one. Note that it is possible that |
| 857 | * the server has been moved out of the tree due to failed health-checks. |
| 858 | */ |
| 859 | static void fwlc_srv_reposition(struct server *s) |
| 860 | { |
| 861 | if (!s->lb_tree) |
| 862 | return; |
| 863 | fwlc_dequeue_srv(s); |
| 864 | fwlc_queue_srv(s); |
| 865 | } |
| 866 | |
| 867 | /* This function updates the server trees according to server <srv>'s new |
| 868 | * state. It should be called when server <srv>'s status changes to down. |
| 869 | * It is not important whether the server was already down or not. It is not |
| 870 | * important either that the new state is completely down (the caller may not |
| 871 | * know all the variables of a server's state). |
| 872 | */ |
| 873 | static void fwlc_set_server_status_down(struct server *srv) |
| 874 | { |
| 875 | struct proxy *p = srv->proxy; |
| 876 | |
| 877 | if (srv->state == srv->prev_state && |
| 878 | srv->eweight == srv->prev_eweight) |
| 879 | return; |
| 880 | |
| 881 | if (srv_is_usable(srv->state, srv->eweight)) |
| 882 | goto out_update_state; |
| 883 | |
| 884 | if (!srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 885 | /* server was already down */ |
| 886 | goto out_update_backend; |
| 887 | |
| 888 | if (srv->state & SRV_BACKUP) { |
| 889 | p->lbprm.tot_wbck -= srv->prev_eweight; |
| 890 | p->srv_bck--; |
| 891 | |
| 892 | if (srv == p->lbprm.fbck) { |
| 893 | /* we lost the first backup server in a single-backup |
| 894 | * configuration, we must search another one. |
| 895 | */ |
| 896 | struct server *srv2 = p->lbprm.fbck; |
| 897 | do { |
| 898 | srv2 = srv2->next; |
| 899 | } while (srv2 && |
| 900 | !((srv2->state & SRV_BACKUP) && |
| 901 | srv_is_usable(srv2->state, srv2->eweight))); |
| 902 | p->lbprm.fbck = srv2; |
| 903 | } |
| 904 | } else { |
| 905 | p->lbprm.tot_wact -= srv->prev_eweight; |
| 906 | p->srv_act--; |
| 907 | } |
| 908 | |
| 909 | fwlc_dequeue_srv(srv); |
| 910 | fwlc_remove_from_tree(srv); |
| 911 | |
| 912 | out_update_backend: |
| 913 | /* check/update tot_used, tot_weight */ |
| 914 | update_backend_weight(p); |
| 915 | out_update_state: |
| 916 | srv->prev_state = srv->state; |
| 917 | srv->prev_eweight = srv->eweight; |
| 918 | } |
| 919 | |
| 920 | /* This function updates the server trees according to server <srv>'s new |
| 921 | * state. It should be called when server <srv>'s status changes to up. |
| 922 | * It is not important whether the server was already down or not. It is not |
| 923 | * important either that the new state is completely UP (the caller may not |
| 924 | * know all the variables of a server's state). This function will not change |
| 925 | * the weight of a server which was already up. |
| 926 | */ |
| 927 | static void fwlc_set_server_status_up(struct server *srv) |
| 928 | { |
| 929 | struct proxy *p = srv->proxy; |
| 930 | |
| 931 | if (srv->state == srv->prev_state && |
| 932 | srv->eweight == srv->prev_eweight) |
| 933 | return; |
| 934 | |
| 935 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 936 | goto out_update_state; |
| 937 | |
| 938 | if (srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 939 | /* server was already up */ |
| 940 | goto out_update_backend; |
| 941 | |
| 942 | if (srv->state & SRV_BACKUP) { |
| 943 | srv->lb_tree = &p->lbprm.fwlc.bck; |
| 944 | p->lbprm.tot_wbck += srv->eweight; |
| 945 | p->srv_bck++; |
| 946 | |
| 947 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 948 | if (!p->lbprm.fbck) { |
| 949 | /* there was no backup server anymore */ |
| 950 | p->lbprm.fbck = srv; |
| 951 | } else { |
| 952 | /* we may have restored a backup server prior to fbck, |
| 953 | * in which case it should replace it. |
| 954 | */ |
| 955 | struct server *srv2 = srv; |
| 956 | do { |
| 957 | srv2 = srv2->next; |
| 958 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 959 | if (srv2) |
| 960 | p->lbprm.fbck = srv; |
| 961 | } |
| 962 | } |
| 963 | } else { |
| 964 | srv->lb_tree = &p->lbprm.fwlc.act; |
| 965 | p->lbprm.tot_wact += srv->eweight; |
| 966 | p->srv_act++; |
| 967 | } |
| 968 | |
| 969 | /* note that eweight cannot be 0 here */ |
| 970 | fwlc_queue_srv(srv); |
| 971 | |
| 972 | out_update_backend: |
| 973 | /* check/update tot_used, tot_weight */ |
| 974 | update_backend_weight(p); |
| 975 | out_update_state: |
| 976 | srv->prev_state = srv->state; |
| 977 | srv->prev_eweight = srv->eweight; |
| 978 | } |
| 979 | |
| 980 | /* This function must be called after an update to server <srv>'s effective |
| 981 | * weight. It may be called after a state change too. |
| 982 | */ |
| 983 | static void fwlc_update_server_weight(struct server *srv) |
| 984 | { |
| 985 | int old_state, new_state; |
| 986 | struct proxy *p = srv->proxy; |
| 987 | |
| 988 | if (srv->state == srv->prev_state && |
| 989 | srv->eweight == srv->prev_eweight) |
| 990 | return; |
| 991 | |
| 992 | /* If changing the server's weight changes its state, we simply apply |
| 993 | * the procedures we already have for status change. If the state |
| 994 | * remains down, the server is not in any tree, so it's as easy as |
| 995 | * updating its values. If the state remains up with different weights, |
| 996 | * there are some computations to perform to find a new place and |
| 997 | * possibly a new tree for this server. |
| 998 | */ |
| 999 | |
| 1000 | old_state = srv_is_usable(srv->prev_state, srv->prev_eweight); |
| 1001 | new_state = srv_is_usable(srv->state, srv->eweight); |
| 1002 | |
| 1003 | if (!old_state && !new_state) { |
| 1004 | srv->prev_state = srv->state; |
| 1005 | srv->prev_eweight = srv->eweight; |
| 1006 | return; |
| 1007 | } |
| 1008 | else if (!old_state && new_state) { |
| 1009 | fwlc_set_server_status_up(srv); |
| 1010 | return; |
| 1011 | } |
| 1012 | else if (old_state && !new_state) { |
| 1013 | fwlc_set_server_status_down(srv); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | if (srv->lb_tree) |
| 1018 | fwlc_dequeue_srv(srv); |
| 1019 | |
| 1020 | if (srv->state & SRV_BACKUP) { |
| 1021 | p->lbprm.tot_wbck += srv->eweight - srv->prev_eweight; |
| 1022 | srv->lb_tree = &p->lbprm.fwlc.bck; |
| 1023 | } else { |
| 1024 | p->lbprm.tot_wact += srv->eweight - srv->prev_eweight; |
| 1025 | srv->lb_tree = &p->lbprm.fwlc.act; |
| 1026 | } |
| 1027 | |
| 1028 | fwlc_queue_srv(srv); |
| 1029 | |
| 1030 | update_backend_weight(p); |
| 1031 | srv->prev_state = srv->state; |
| 1032 | srv->prev_eweight = srv->eweight; |
| 1033 | } |
| 1034 | |
| 1035 | /* This function is responsible for building the trees in case of fast |
| 1036 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 1037 | * uweight ratio. Both active and backup groups are initialized. |
| 1038 | */ |
| 1039 | void fwlc_init_server_tree(struct proxy *p) |
| 1040 | { |
| 1041 | struct server *srv; |
| 1042 | struct eb_root init_head = EB_ROOT; |
| 1043 | |
| 1044 | p->lbprm.set_server_status_up = fwlc_set_server_status_up; |
| 1045 | p->lbprm.set_server_status_down = fwlc_set_server_status_down; |
| 1046 | p->lbprm.update_server_eweight = fwlc_update_server_weight; |
| 1047 | p->lbprm.server_take_conn = fwlc_srv_reposition; |
| 1048 | p->lbprm.server_drop_conn = fwlc_srv_reposition; |
| 1049 | |
| 1050 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 1051 | for (srv = p->srv; srv; srv = srv->next) { |
| 1052 | srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE; |
| 1053 | srv->prev_state = srv->state; |
| 1054 | } |
| 1055 | |
| 1056 | recount_servers(p); |
| 1057 | update_backend_weight(p); |
| 1058 | |
| 1059 | p->lbprm.fwlc.act = init_head; |
| 1060 | p->lbprm.fwlc.bck = init_head; |
| 1061 | |
| 1062 | /* queue active and backup servers in two distinct groups */ |
| 1063 | for (srv = p->srv; srv; srv = srv->next) { |
| 1064 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 1065 | continue; |
| 1066 | srv->lb_tree = (srv->state & SRV_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act; |
| 1067 | fwlc_queue_srv(srv); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* Return next server from the FWLC tree in backend <p>. If the tree is empty, |
| 1072 | * return NULL. Saturated servers are skipped. |
| 1073 | */ |
| 1074 | static struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 1075 | { |
| 1076 | struct server *srv, *avoided; |
| 1077 | struct eb32_node *node; |
| 1078 | |
| 1079 | srv = avoided = NULL; |
| 1080 | |
| 1081 | if (p->srv_act) |
| 1082 | node = eb32_first(&p->lbprm.fwlc.act); |
| 1083 | else if (p->lbprm.fbck) |
| 1084 | return p->lbprm.fbck; |
| 1085 | else if (p->srv_bck) |
| 1086 | node = eb32_first(&p->lbprm.fwlc.bck); |
| 1087 | else |
| 1088 | return NULL; |
| 1089 | |
| 1090 | while (node) { |
| 1091 | /* OK, we have a server. However, it may be saturated, in which |
| 1092 | * case we don't want to reconsider it for now, so we'll simply |
| 1093 | * skip it. Same if it's the server we try to avoid, in which |
| 1094 | * case we simply remember it for later use if needed. |
| 1095 | */ |
| 1096 | struct server *s; |
| 1097 | |
| 1098 | s = eb32_entry(node, struct server, lb_node); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1099 | if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) { |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1100 | if (s != srvtoavoid) { |
| 1101 | srv = s; |
| 1102 | break; |
| 1103 | } |
| 1104 | avoided = s; |
| 1105 | } |
| 1106 | node = eb32_next(node); |
| 1107 | } |
| 1108 | |
| 1109 | if (!srv) |
| 1110 | srv = avoided; |
| 1111 | |
| 1112 | return srv; |
| 1113 | } |
| 1114 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1115 | /* |
| 1116 | * This function tries to find a running server for the proxy <px> following |
| 1117 | * the URL parameter hash method. It looks for a specific parameter in the |
| 1118 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 1119 | * performance by avoiding bounces between servers in contexts where sessions |
| 1120 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 1121 | * is returned. If any server is found, it will be returned. If no valid server |
| 1122 | * is found, NULL is returned. |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1123 | */ |
| 1124 | struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len) |
| 1125 | { |
| 1126 | unsigned long hash = 0; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1127 | const char *p; |
| 1128 | const char *params; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1129 | int plen; |
| 1130 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1131 | /* when tot_weight is 0 then so is srv_count */ |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1132 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1133 | return NULL; |
| 1134 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1135 | if ((p = memchr(uri, '?', uri_len)) == NULL) |
| 1136 | return NULL; |
| 1137 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1138 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 1139 | recalc_server_map(px); |
| 1140 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1141 | p++; |
| 1142 | |
| 1143 | uri_len -= (p - uri); |
| 1144 | plen = px->url_param_len; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1145 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1146 | |
| 1147 | while (uri_len > plen) { |
| 1148 | /* Look for the parameter name followed by an equal symbol */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1149 | if (params[plen] == '=') { |
| 1150 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 1151 | /* OK, we have the parameter here at <params>, and |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1152 | * the value after the equal sign, at <p> |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1153 | * skip the equal symbol |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1154 | */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1155 | p += plen + 1; |
| 1156 | uri_len -= plen + 1; |
| 1157 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1158 | while (uri_len && *p != '&') { |
| 1159 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 1160 | uri_len--; |
| 1161 | p++; |
| 1162 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 1163 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1164 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1165 | } |
| 1166 | /* skip to next parameter */ |
| 1167 | p = memchr(params, '&', uri_len); |
| 1168 | if (!p) |
| 1169 | return NULL; |
| 1170 | p++; |
| 1171 | uri_len -= (p - params); |
| 1172 | params = p; |
| 1173 | } |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * this does the same as the previous server_ph, but check the body contents |
| 1179 | */ |
| 1180 | struct server *get_server_ph_post(struct session *s) |
| 1181 | { |
| 1182 | unsigned long hash = 0; |
| 1183 | struct http_txn *txn = &s->txn; |
| 1184 | struct buffer *req = s->req; |
| 1185 | struct http_msg *msg = &txn->req; |
| 1186 | struct proxy *px = s->be; |
| 1187 | unsigned int plen = px->url_param_len; |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 1188 | unsigned long body; |
| 1189 | unsigned long len; |
| 1190 | const char *params; |
| 1191 | struct hdr_ctx ctx; |
| 1192 | const char *p; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1193 | |
| 1194 | /* tot_weight appears to mean srv_count */ |
| 1195 | if (px->lbprm.tot_weight == 0) |
| 1196 | return NULL; |
| 1197 | |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 1198 | body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 1199 | len = req->l - body; |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 1200 | params = req->data + body; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1201 | |
| 1202 | if ( len == 0 ) |
| 1203 | return NULL; |
| 1204 | |
| 1205 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 1206 | recalc_server_map(px); |
| 1207 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1208 | ctx.idx = 0; |
| 1209 | |
| 1210 | /* if the message is chunked, we skip the chunk size, but use the value as len */ |
| 1211 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 1212 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1213 | unsigned int chunk = 0; |
| 1214 | while ( params < req->rlim && !HTTP_IS_CRLF(*params)) { |
| 1215 | char c = *params; |
| 1216 | if (ishex(c)) { |
| 1217 | unsigned int hex = toupper(c) - '0'; |
| 1218 | if ( hex > 9 ) |
| 1219 | hex -= 'A' - '9' - 1; |
| 1220 | chunk = (chunk << 4) | hex; |
| 1221 | } |
| 1222 | else |
| 1223 | return NULL; |
| 1224 | params++; |
| 1225 | len--; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1226 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1227 | /* spec says we get CRLF */ |
| 1228 | if (HTTP_IS_CRLF(*params) && HTTP_IS_CRLF(params[1])) |
| 1229 | params += 2; |
| 1230 | else |
| 1231 | return NULL; |
| 1232 | /* ok we have some encoded length, just inspect the first chunk */ |
| 1233 | len = chunk; |
| 1234 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 1236 | p = params; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1237 | |
| 1238 | while (len > plen) { |
| 1239 | /* Look for the parameter name followed by an equal symbol */ |
| 1240 | if (params[plen] == '=') { |
| 1241 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 1242 | /* OK, we have the parameter here at <params>, and |
| 1243 | * the value after the equal sign, at <p> |
| 1244 | * skip the equal symbol |
| 1245 | */ |
| 1246 | p += plen + 1; |
| 1247 | len -= plen + 1; |
| 1248 | |
| 1249 | while (len && *p != '&') { |
| 1250 | if (unlikely(!HTTP_IS_TOKEN(*p))) { |
| 1251 | /* if in a POST, body must be URI encoded or its not a URI. |
| 1252 | * Do not interprete any possible binary data as a parameter. |
| 1253 | */ |
| 1254 | if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */ |
| 1255 | break; |
| 1256 | return NULL; /* oh, no; this is not uri-encoded. |
| 1257 | * This body does not contain parameters. |
| 1258 | */ |
| 1259 | } |
| 1260 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 1261 | len--; |
| 1262 | p++; |
| 1263 | /* should we break if vlen exceeds limit? */ |
| 1264 | } |
| 1265 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
| 1266 | } |
| 1267 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1268 | /* skip to next parameter */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1269 | p = memchr(params, '&', len); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1270 | if (!p) |
| 1271 | return NULL; |
| 1272 | p++; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1273 | len -= (p - params); |
| 1274 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1275 | } |
| 1276 | return NULL; |
| 1277 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1278 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1279 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1280 | /* |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1281 | * This function applies the load-balancing algorithm to the session, as |
| 1282 | * defined by the backend it is assigned to. The session is then marked as |
| 1283 | * 'assigned'. |
| 1284 | * |
| 1285 | * This function MAY NOT be called with SN_ASSIGNED already set. If the session |
| 1286 | * had a server previously assigned, it is rebalanced, trying to avoid the same |
| 1287 | * server. |
| 1288 | * The function tries to keep the original connection slot if it reconnects to |
| 1289 | * the same server, otherwise it releases it and tries to offer it. |
| 1290 | * |
| 1291 | * It is illegal to call this function with a session in a queue. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1292 | * |
| 1293 | * It may return : |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1294 | * SRV_STATUS_OK if everything is OK. Session assigned to ->srv |
| 1295 | * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED |
| 1296 | * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1297 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 1298 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1299 | * Upon successful return, the session flag SN_ASSIGNED is set to indicate that |
| 1300 | * it does not need to be called anymore. This means that s->srv can be trusted |
| 1301 | * in balance and direct modes. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1302 | * |
| 1303 | */ |
| 1304 | |
| 1305 | int assign_server(struct session *s) |
| 1306 | { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1307 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1308 | struct server *conn_slot; |
| 1309 | int err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1311 | #ifdef DEBUG_FULL |
| 1312 | fprintf(stderr,"assign_server : s=%p\n",s); |
| 1313 | #endif |
| 1314 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1315 | err = SRV_STATUS_INTERNAL; |
| 1316 | if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED)) |
| 1317 | goto out_err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1318 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1319 | s->prev_srv = s->prev_srv; |
| 1320 | conn_slot = s->srv_conn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1321 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1322 | /* We have to release any connection slot before applying any LB algo, |
| 1323 | * otherwise we may erroneously end up with no available slot. |
| 1324 | */ |
| 1325 | if (conn_slot) |
| 1326 | sess_change_server(s, NULL); |
| 1327 | |
| 1328 | /* We will now try to find the good server and store it into <s->srv>. |
| 1329 | * Note that <s->srv> may be NULL in case of dispatch or proxy mode, |
| 1330 | * as well as if no server is available (check error code). |
| 1331 | */ |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 1332 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1333 | s->srv = NULL; |
| 1334 | if (s->be->lbprm.algo & BE_LB_ALGO) { |
| 1335 | int len; |
| 1336 | /* we must check if we have at least one server available */ |
| 1337 | if (!s->be->lbprm.tot_weight) { |
| 1338 | err = SRV_STATUS_NOSRV; |
| 1339 | goto out; |
| 1340 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1341 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1342 | switch (s->be->lbprm.algo & BE_LB_ALGO) { |
| 1343 | case BE_LB_ALGO_RR: |
| 1344 | s->srv = fwrr_get_next_server(s->be, s->prev_srv); |
| 1345 | if (!s->srv) { |
| 1346 | err = SRV_STATUS_FULL; |
| 1347 | goto out; |
| 1348 | } |
| 1349 | break; |
| 1350 | case BE_LB_ALGO_LC: |
| 1351 | s->srv = fwlc_get_next_server(s->be, s->prev_srv); |
| 1352 | if (!s->srv) { |
| 1353 | err = SRV_STATUS_FULL; |
| 1354 | goto out; |
| 1355 | } |
| 1356 | break; |
| 1357 | case BE_LB_ALGO_SH: |
| 1358 | if (s->cli_addr.ss_family == AF_INET) |
| 1359 | len = 4; |
| 1360 | else if (s->cli_addr.ss_family == AF_INET6) |
| 1361 | len = 16; |
| 1362 | else { |
| 1363 | /* unknown IP family */ |
| 1364 | err = SRV_STATUS_INTERNAL; |
| 1365 | goto out; |
| 1366 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1367 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1368 | s->srv = get_server_sh(s->be, |
| 1369 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1370 | len); |
| 1371 | break; |
| 1372 | case BE_LB_ALGO_UH: |
| 1373 | /* URI hashing */ |
| 1374 | s->srv = get_server_uh(s->be, |
| 1375 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 1376 | s->txn.req.sl.rq.u_l); |
| 1377 | break; |
| 1378 | case BE_LB_ALGO_PH: |
| 1379 | /* URL Parameter hashing */ |
| 1380 | if (s->txn.meth == HTTP_METH_POST && |
| 1381 | memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&', |
| 1382 | s->txn.req.sl.rq.u_l ) == NULL) |
| 1383 | s->srv = get_server_ph_post(s); |
| 1384 | else |
| 1385 | s->srv = get_server_ph(s->be, |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 1386 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 1387 | s->txn.req.sl.rq.u_l); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1388 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1389 | if (!s->srv) { |
| 1390 | /* parameter not found, fall back to round robin on the map */ |
| 1391 | s->srv = get_server_rr_with_conns(s->be, s->prev_srv); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1392 | if (!s->srv) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1393 | err = SRV_STATUS_FULL; |
| 1394 | goto out; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1395 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1396 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1397 | break; |
| 1398 | default: |
| 1399 | /* unknown balancing algorithm */ |
| 1400 | err = SRV_STATUS_INTERNAL; |
| 1401 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1402 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1403 | if (s->srv != s->prev_srv) { |
| 1404 | s->be->cum_lbconn++; |
| 1405 | s->srv->cum_lbconn++; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1406 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1407 | } |
| 1408 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 1409 | if (!s->srv_addr.sin_addr.s_addr) { |
| 1410 | err = SRV_STATUS_NOSRV; |
| 1411 | goto out; |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 1412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1413 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1414 | else if (!*(int *)&s->be->dispatch_addr.sin_addr && |
| 1415 | !(s->fe->options & PR_O_TRANSP)) { |
| 1416 | err = SRV_STATUS_NOSRV; |
| 1417 | goto out; |
| 1418 | } |
| 1419 | |
| 1420 | s->flags |= SN_ASSIGNED; |
| 1421 | err = SRV_STATUS_OK; |
| 1422 | out: |
| 1423 | |
| 1424 | /* Either we take back our connection slot, or we offer it to someone |
| 1425 | * else if we don't need it anymore. |
| 1426 | */ |
| 1427 | if (conn_slot) { |
| 1428 | if (conn_slot == s->srv) { |
| 1429 | sess_change_server(s, s->srv); |
| 1430 | } else { |
| 1431 | if (may_dequeue_tasks(conn_slot, s->be)) |
| 1432 | process_srv_queue(conn_slot); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | out_err: |
| 1437 | return err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | /* |
| 1442 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 1443 | * The address is taken from the currently assigned server, or from the |
| 1444 | * dispatch or transparent address. |
| 1445 | * |
| 1446 | * It may return : |
| 1447 | * SRV_STATUS_OK if everything is OK. |
| 1448 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 1449 | * |
| 1450 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 1451 | * not cleared, so it's to the caller to clear it if required. |
| 1452 | * |
| 1453 | */ |
| 1454 | int assign_server_address(struct session *s) |
| 1455 | { |
| 1456 | #ifdef DEBUG_FULL |
| 1457 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 1458 | #endif |
| 1459 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1460 | if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1461 | /* A server is necessarily known for this session */ |
| 1462 | if (!(s->flags & SN_ASSIGNED)) |
| 1463 | return SRV_STATUS_INTERNAL; |
| 1464 | |
| 1465 | s->srv_addr = s->srv->addr; |
| 1466 | |
| 1467 | /* if this server remaps proxied ports, we'll use |
| 1468 | * the port the client connected to with an offset. */ |
| 1469 | if (s->srv->state & SRV_MAPPORTS) { |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 1470 | if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET)) |
| 1471 | get_frt_addr(s); |
| 1472 | if (s->frt_addr.ss_family == AF_INET) { |
| 1473 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 1474 | ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port)); |
| 1475 | } else { |
| 1476 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 1477 | ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port)); |
| 1478 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1479 | } |
| 1480 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1481 | else if (*(int *)&s->be->dispatch_addr.sin_addr) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1482 | /* connect to the defined dispatch addr */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1483 | s->srv_addr = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1484 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 1485 | else if (s->fe->options & PR_O_TRANSP) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1486 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
Willy Tarreau | bd41428 | 2008-01-19 13:46:35 +0100 | [diff] [blame] | 1487 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 1488 | get_frt_addr(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1489 | |
Willy Tarreau | bd41428 | 2008-01-19 13:46:35 +0100 | [diff] [blame] | 1490 | memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr))); |
| 1491 | /* when we support IPv6 on the backend, we may add other tests */ |
| 1492 | //qfprintf(stderr, "Cannot get original server address.\n"); |
| 1493 | //return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1494 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1495 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 1496 | /* If HTTP PROXY option is set, then server is already assigned |
| 1497 | * during incoming client request parsing. */ |
| 1498 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 1499 | else { |
| 1500 | /* no server and no LB algorithm ! */ |
| 1501 | return SRV_STATUS_INTERNAL; |
| 1502 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1503 | |
| 1504 | s->flags |= SN_ADDR_SET; |
| 1505 | return SRV_STATUS_OK; |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | /* This function assigns a server to session <s> if required, and can add the |
| 1510 | * connection to either the assigned server's queue or to the proxy's queue. |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1511 | * If ->srv_conn is set, the session is first released from the server. |
| 1512 | * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will |
| 1513 | * be called before any connection and after any retry or redispatch occurs. |
| 1514 | * |
| 1515 | * It is not allowed to call this function with a session in a queue. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1516 | * |
| 1517 | * Returns : |
| 1518 | * |
| 1519 | * SRV_STATUS_OK if everything is OK. |
| 1520 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 1521 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 1522 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1523 | * connection could not be queued in s->srv, |
| 1524 | * which may be NULL if we queue on the backend. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1525 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 1526 | * |
| 1527 | */ |
| 1528 | int assign_server_and_queue(struct session *s) |
| 1529 | { |
| 1530 | struct pendconn *p; |
| 1531 | int err; |
| 1532 | |
| 1533 | if (s->pend_pos) |
| 1534 | return SRV_STATUS_INTERNAL; |
| 1535 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1536 | err = SRV_STATUS_OK; |
| 1537 | if (!(s->flags & SN_ASSIGNED)) { |
| 1538 | err = assign_server(s); |
| 1539 | if (s->prev_srv) { |
| 1540 | /* This session was previously assigned to a server. We have to |
| 1541 | * update the session's and the server's stats : |
| 1542 | * - if the server changed : |
| 1543 | * - set TX_CK_DOWN if txn.flags was TX_CK_VALID |
| 1544 | * - set SN_REDISP if it was successfully redispatched |
| 1545 | * - increment srv->redispatches and be->redispatches |
| 1546 | * - if the server remained the same : update retries. |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1547 | */ |
| 1548 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1549 | if (s->prev_srv != s->srv) { |
| 1550 | if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) { |
| 1551 | s->txn.flags &= ~TX_CK_MASK; |
| 1552 | s->txn.flags |= TX_CK_DOWN; |
| 1553 | } |
| 1554 | s->flags |= SN_REDISP; |
| 1555 | s->prev_srv->redispatches++; |
| 1556 | s->be->redispatches++; |
| 1557 | } else { |
| 1558 | s->prev_srv->retries++; |
| 1559 | s->be->retries++; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1560 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1564 | switch (err) { |
| 1565 | case SRV_STATUS_OK: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1566 | /* we have SN_ASSIGNED set */ |
| 1567 | if (!s->srv) |
| 1568 | return SRV_STATUS_OK; /* dispatch or proxy mode */ |
| 1569 | |
| 1570 | /* If we already have a connection slot, no need to check any queue */ |
| 1571 | if (s->srv_conn == s->srv) |
| 1572 | return SRV_STATUS_OK; |
| 1573 | |
| 1574 | /* OK, this session already has an assigned server, but no |
| 1575 | * connection slot yet. Either it is a redispatch, or it was |
| 1576 | * assigned from persistence information (direct mode). |
| 1577 | */ |
| 1578 | if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) { |
| 1579 | /* server scheduled for redirection, and already assigned. We |
| 1580 | * don't want to go further nor check the queue. |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1581 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1582 | sess_change_server(s, s->srv); /* not really needed in fact */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1583 | return SRV_STATUS_OK; |
| 1584 | } |
| 1585 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1586 | /* We might have to queue this session if the assigned server is full. |
| 1587 | * We know we have to queue it into the server's queue, so if a maxqueue |
| 1588 | * is set on the server, we must also check that the server's queue is |
| 1589 | * not full, in which case we have to return FULL. |
| 1590 | */ |
| 1591 | if (s->srv->maxconn && |
| 1592 | (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) { |
| 1593 | |
| 1594 | if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) |
| 1595 | return SRV_STATUS_FULL; |
| 1596 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1597 | p = pendconn_add(s); |
| 1598 | if (p) |
| 1599 | return SRV_STATUS_QUEUED; |
| 1600 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1601 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1602 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1603 | |
| 1604 | /* OK, we can use this server. Let's reserve our place */ |
| 1605 | sess_change_server(s, s->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1606 | return SRV_STATUS_OK; |
| 1607 | |
| 1608 | case SRV_STATUS_FULL: |
| 1609 | /* queue this session into the proxy's queue */ |
| 1610 | p = pendconn_add(s); |
| 1611 | if (p) |
| 1612 | return SRV_STATUS_QUEUED; |
| 1613 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1614 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1615 | |
| 1616 | case SRV_STATUS_NOSRV: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1617 | return err; |
| 1618 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1619 | case SRV_STATUS_INTERNAL: |
| 1620 | return err; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1622 | default: |
| 1623 | return SRV_STATUS_INTERNAL; |
| 1624 | } |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1625 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1626 | |
| 1627 | /* |
| 1628 | * This function initiates a connection to the server assigned to this session |
| 1629 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 1630 | * It can return one of : |
| 1631 | * - SN_ERR_NONE if everything's OK |
| 1632 | * - SN_ERR_SRVTO if there are no more servers |
| 1633 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1634 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1635 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1636 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1637 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1638 | */ |
| 1639 | int connect_server(struct session *s) |
| 1640 | { |
| 1641 | int fd, err; |
| 1642 | |
| 1643 | if (!(s->flags & SN_ADDR_SET)) { |
| 1644 | err = assign_server_address(s); |
| 1645 | if (err != SRV_STATUS_OK) |
| 1646 | return SN_ERR_INTERNAL; |
| 1647 | } |
| 1648 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1649 | if ((fd = s->req->cons->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1650 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 1651 | |
| 1652 | if (errno == ENFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1653 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1654 | "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] | 1655 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1656 | else if (errno == EMFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1657 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1658 | "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] | 1659 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1660 | else if (errno == ENOBUFS || errno == ENOMEM) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1661 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1662 | "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] | 1663 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1664 | /* this is a resource error */ |
| 1665 | return SN_ERR_RESOURCE; |
| 1666 | } |
| 1667 | |
| 1668 | if (fd >= global.maxsock) { |
| 1669 | /* do not log anything there, it's a normal condition when this option |
| 1670 | * is used to serialize connections to a server ! |
| 1671 | */ |
| 1672 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1673 | close(fd); |
| 1674 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 1675 | } |
| 1676 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1677 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1678 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1679 | /* TCP splicing supported by both FE and BE */ |
| 1680 | tcp_splice_initfd(s->cli_fd, fd); |
| 1681 | } |
| 1682 | #endif |
| 1683 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1684 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1685 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 1686 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 1687 | close(fd); |
| 1688 | return SN_ERR_INTERNAL; |
| 1689 | } |
| 1690 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1691 | if (s->be->options & PR_O_TCP_SRV_KA) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1692 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1693 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 1694 | if (s->be->options & PR_O_TCP_NOLING) |
| 1695 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 1696 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1697 | /* allow specific binding : |
| 1698 | * - server-specific at first |
| 1699 | * - proxy-specific next |
| 1700 | */ |
| 1701 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1702 | struct sockaddr_in *remote = NULL; |
| 1703 | int ret, flags = 0; |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1704 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1705 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 786d191 | 2008-01-13 18:10:06 +0100 | [diff] [blame] | 1706 | switch (s->srv->state & SRV_TPROXY_MASK) { |
| 1707 | case SRV_TPROXY_ADDR: |
| 1708 | remote = (struct sockaddr_in *)&s->srv->tproxy_addr; |
| 1709 | flags = 3; |
| 1710 | break; |
| 1711 | case SRV_TPROXY_CLI: |
| 1712 | flags |= 2; |
| 1713 | /* fall through */ |
| 1714 | case SRV_TPROXY_CIP: |
| 1715 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1716 | flags |= 1; |
| 1717 | remote = (struct sockaddr_in *)&s->cli_addr; |
| 1718 | break; |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1719 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1720 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1721 | ret = tcpv4_bind_socket(fd, flags, &s->srv->source_addr, remote); |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1722 | if (ret) { |
| 1723 | close(fd); |
| 1724 | if (ret == 1) { |
| 1725 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1726 | s->be->id, s->srv->id); |
| 1727 | send_log(s->be, LOG_EMERG, |
| 1728 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1729 | s->be->id, s->srv->id); |
| 1730 | } else { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1731 | 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] | 1732 | s->be->id, s->srv->id); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1733 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1734 | "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] | 1735 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1736 | } |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1737 | return SN_ERR_RESOURCE; |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1738 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1739 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1740 | else if (s->be->options & PR_O_BIND_SRC) { |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1741 | struct sockaddr_in *remote = NULL; |
| 1742 | int ret, flags = 0; |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1743 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1744 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 786d191 | 2008-01-13 18:10:06 +0100 | [diff] [blame] | 1745 | switch (s->be->options & PR_O_TPXY_MASK) { |
| 1746 | case PR_O_TPXY_ADDR: |
| 1747 | remote = (struct sockaddr_in *)&s->be->tproxy_addr; |
| 1748 | flags = 3; |
| 1749 | break; |
| 1750 | case PR_O_TPXY_CLI: |
| 1751 | flags |= 2; |
| 1752 | /* fall through */ |
| 1753 | case PR_O_TPXY_CIP: |
| 1754 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1755 | flags |= 1; |
| 1756 | remote = (struct sockaddr_in *)&s->cli_addr; |
| 1757 | break; |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1758 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame] | 1759 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1760 | ret = tcpv4_bind_socket(fd, flags, &s->be->source_addr, remote); |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1761 | if (ret) { |
| 1762 | close(fd); |
| 1763 | if (ret == 1) { |
| 1764 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 1765 | s->be->id); |
| 1766 | send_log(s->be, LOG_EMERG, |
| 1767 | "Cannot bind to source address before connect() for proxy %s.\n", |
| 1768 | s->be->id); |
| 1769 | } else { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1770 | 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] | 1771 | s->be->id); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1772 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | fe10a06 | 2008-01-12 22:22:34 +0100 | [diff] [blame] | 1773 | "Cannot bind to tproxy source address before connect() for proxy %s.\n", |
| 1774 | s->be->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1775 | } |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 1776 | return SN_ERR_RESOURCE; |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1777 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1778 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1779 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1780 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1781 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1782 | |
| 1783 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1784 | char *msg; |
| 1785 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1786 | msg = "no free ports"; |
| 1787 | else |
| 1788 | msg = "local address already in use"; |
| 1789 | |
| 1790 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
| 1791 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1792 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1793 | "Connect() failed for server %s/%s: %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1794 | s->be->id, s->srv->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1795 | return SN_ERR_RESOURCE; |
| 1796 | } else if (errno == ETIMEDOUT) { |
| 1797 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 1798 | close(fd); |
| 1799 | return SN_ERR_SRVTO; |
| 1800 | } else { |
| 1801 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 1802 | //qfprintf(stderr,"Connect(): %d", errno); |
| 1803 | close(fd); |
| 1804 | return SN_ERR_SRVCL; |
| 1805 | } |
| 1806 | } |
| 1807 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1808 | fdtab[fd].owner = s->req->cons; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1809 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1810 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1811 | fdtab[fd].cb[DIR_RD].b = s->rep; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 1812 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1813 | fdtab[fd].cb[DIR_WR].b = s->req; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 1814 | |
| 1815 | fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr; |
| 1816 | fdtab[fd].peerlen = sizeof(s->srv_addr); |
| 1817 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1818 | fd_insert(fd); |
Willy Tarreau | 788e284 | 2008-08-26 13:25:39 +0200 | [diff] [blame] | 1819 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
| 1820 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1821 | s->req->cons->state = SI_ST_CON; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1822 | if (s->srv) { |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame^] | 1823 | s->flags |= SN_CURR_SESS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1824 | s->srv->cur_sess++; |
| 1825 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 1826 | s->srv->cur_sess_max = s->srv->cur_sess; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1827 | if (s->be->lbprm.server_take_conn) |
| 1828 | s->be->lbprm.server_take_conn(s->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1829 | } |
| 1830 | |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 1831 | s->req->wex = tick_add_ifset(now_ms, s->be->timeout.connect); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1832 | return SN_ERR_NONE; /* connection is OK */ |
| 1833 | } |
| 1834 | |
| 1835 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1836 | /* This function performs the "redispatch" part of a connection attempt. It |
| 1837 | * will assign a server if required, queue the connection if required, and |
| 1838 | * handle errors that might arise at this level. It can change the server |
| 1839 | * state. It will return 1 if it encounters an error, switches the server |
| 1840 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 1841 | * that the connection is ready to use. |
| 1842 | */ |
| 1843 | |
| 1844 | int srv_redispatch_connect(struct session *t) |
| 1845 | { |
| 1846 | int conn_err; |
| 1847 | |
| 1848 | /* We know that we don't have any connection pending, so we will |
| 1849 | * try to get a new one, and wait in this state if it's queued |
| 1850 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1851 | redispatch: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1852 | conn_err = assign_server_and_queue(t); |
| 1853 | switch (conn_err) { |
| 1854 | case SRV_STATUS_OK: |
| 1855 | break; |
| 1856 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1857 | case SRV_STATUS_FULL: |
| 1858 | /* The server has reached its maxqueue limit. Either PR_O_REDISP is set |
| 1859 | * and we can redispatch to another server, or it is not and we return |
| 1860 | * 503. This only makes sense in DIRECT mode however, because normal LB |
| 1861 | * algorithms would never select such a server, and hash algorithms |
| 1862 | * would bring us on the same server again. Note that t->srv is set in |
| 1863 | * this case. |
| 1864 | */ |
| 1865 | if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) { |
| 1866 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 1867 | t->prev_srv = t->srv; |
| 1868 | goto redispatch; |
| 1869 | } |
| 1870 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1871 | //t->req->wex = TICK_ETERNITY; |
| 1872 | //srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
| 1873 | // 503, error_message(t, HTTP_ERR_503)); |
| 1874 | |
| 1875 | if (!t->req->cons->err_type) { |
| 1876 | t->req->cons->err_type = SI_ET_QUEUE_ERR; |
| 1877 | t->req->cons->err_loc = t->srv; |
| 1878 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1879 | |
| 1880 | t->srv->failed_conns++; |
| 1881 | t->be->failed_conns++; |
| 1882 | return 1; |
| 1883 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1884 | case SRV_STATUS_NOSRV: |
| 1885 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1886 | //t->req->wex = TICK_ETERNITY; |
| 1887 | //srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C, |
| 1888 | // 503, error_message(t, HTTP_ERR_503)); |
| 1889 | |
| 1890 | if (!t->req->cons->err_type) { |
| 1891 | t->req->cons->err_type = SI_ET_CONN_ERR; |
| 1892 | t->req->cons->err_loc = NULL; |
| 1893 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1894 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1895 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1896 | return 1; |
| 1897 | |
| 1898 | case SRV_STATUS_QUEUED: |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 1899 | t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1900 | t->req->cons->state = SI_ST_QUE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1901 | /* do nothing else and do not wake any other session up */ |
| 1902 | return 1; |
| 1903 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1904 | case SRV_STATUS_INTERNAL: |
| 1905 | default: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1906 | //t->req->wex = TICK_ETERNITY; |
| 1907 | //srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
| 1908 | // 500, error_message(t, HTTP_ERR_500)); |
| 1909 | |
| 1910 | if (!t->req->cons->err_type) { |
| 1911 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 1912 | t->req->cons->err_loc = t->srv; |
| 1913 | } |
| 1914 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1915 | if (t->srv) |
Willy Tarreau | 98937b8 | 2007-12-10 15:05:42 +0100 | [diff] [blame] | 1916 | t->srv->cum_sess++; |
| 1917 | if (t->srv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1918 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1919 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1920 | |
| 1921 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1922 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1923 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1924 | return 1; |
| 1925 | } |
| 1926 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 1927 | * means that the connection has not been queued. |
| 1928 | */ |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1932 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 1933 | 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] | 1934 | return px->down_time; |
| 1935 | |
| 1936 | return now.tv_sec - px->last_change + px->down_time; |
| 1937 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1938 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1939 | /* This function parses a "balance" statement in a backend section describing |
| 1940 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 1941 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 1942 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 1943 | * written. The function must be called with <args> pointing to the first word |
| 1944 | * after "balance". |
| 1945 | */ |
| 1946 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 1947 | { |
| 1948 | if (!*(args[0])) { |
| 1949 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1950 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1951 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1956 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1957 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1958 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1959 | else if (!strcmp(args[0], "leastconn")) { |
| 1960 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1961 | curproxy->lbprm.algo |= BE_LB_ALGO_LC; |
| 1962 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1963 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1964 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1965 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1966 | } |
| 1967 | else if (!strcmp(args[0], "uri")) { |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1968 | int arg = 1; |
| 1969 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1970 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1971 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1972 | |
| 1973 | while (*args[arg]) { |
| 1974 | if (!strcmp(args[arg], "len")) { |
| 1975 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1976 | snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]); |
| 1977 | return -1; |
| 1978 | } |
| 1979 | curproxy->uri_len_limit = atoi(args[arg+1]); |
| 1980 | arg += 2; |
| 1981 | } |
| 1982 | else if (!strcmp(args[arg], "depth")) { |
| 1983 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1984 | snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]); |
| 1985 | return -1; |
| 1986 | } |
| 1987 | /* hint: we store the position of the ending '/' (depth+1) so |
| 1988 | * that we avoid a comparison while computing the hash. |
| 1989 | */ |
| 1990 | curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1; |
| 1991 | arg += 2; |
| 1992 | } |
| 1993 | else { |
| 1994 | snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]); |
| 1995 | return -1; |
| 1996 | } |
| 1997 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1998 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1999 | else if (!strcmp(args[0], "url_param")) { |
| 2000 | if (!*args[1]) { |
| 2001 | snprintf(err, errlen, "'balance url_param' requires an URL parameter name."); |
| 2002 | return -1; |
| 2003 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 2004 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 2005 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2006 | |
| 2007 | free(curproxy->url_param_name); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 2008 | curproxy->url_param_name = strdup(args[1]); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 2009 | curproxy->url_param_len = strlen(args[1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 2010 | if (*args[2]) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2011 | if (strcmp(args[2], "check_post")) { |
| 2012 | snprintf(err, errlen, "'balance url_param' only accepts check_post modifier."); |
| 2013 | return -1; |
| 2014 | } |
| 2015 | if (*args[3]) { |
| 2016 | /* TODO: maybe issue a warning if there is no value, no digits or too long */ |
| 2017 | curproxy->url_param_post_limit = str2ui(args[3]); |
| 2018 | } |
| 2019 | /* if no limit, or faul value in args[3], then default to a moderate wordlen */ |
| 2020 | if (!curproxy->url_param_post_limit) |
| 2021 | curproxy->url_param_post_limit = 48; |
| 2022 | else if ( curproxy->url_param_post_limit < 3 ) |
| 2023 | curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */ |
| 2024 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 2025 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 2026 | else { |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2027 | snprintf(err, errlen, "'balance' only supports 'roundrobin', 'leastconn', 'source', 'uri' and 'url_param' options."); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 2028 | return -1; |
| 2029 | } |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 2033 | |
| 2034 | /************************************************************************/ |
| 2035 | /* All supported keywords must be declared here. */ |
| 2036 | /************************************************************************/ |
| 2037 | |
| 2038 | /* set test->i to the number of enabled servers on the proxy */ |
| 2039 | static int |
| 2040 | acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2041 | struct acl_expr *expr, struct acl_test *test) |
| 2042 | { |
| 2043 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2044 | if (expr->arg_len) { |
| 2045 | /* another proxy was designated, we must look for it */ |
| 2046 | for (px = proxy; px; px = px->next) |
| 2047 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 2048 | break; |
| 2049 | } |
| 2050 | if (!px) |
| 2051 | return 0; |
| 2052 | |
| 2053 | if (px->srv_act) |
| 2054 | test->i = px->srv_act; |
| 2055 | else if (px->lbprm.fbck) |
| 2056 | test->i = 1; |
| 2057 | else |
| 2058 | test->i = px->srv_bck; |
| 2059 | |
| 2060 | return 1; |
| 2061 | } |
| 2062 | |
| 2063 | |
| 2064 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2065 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 2066 | { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING }, |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 2067 | { NULL, NULL, NULL, NULL }, |
| 2068 | }}; |
| 2069 | |
| 2070 | |
| 2071 | __attribute__((constructor)) |
| 2072 | static void __backend_init(void) |
| 2073 | { |
| 2074 | acl_register_keywords(&acl_kws); |
| 2075 | } |
| 2076 | |
| 2077 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2078 | /* |
| 2079 | * Local variables: |
| 2080 | * c-indent-level: 8 |
| 2081 | * c-basic-offset: 8 |
| 2082 | * End: |
| 2083 | */ |