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