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