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