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