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