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