Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Backend variables and functions. |
| 3 | * |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 21 | #include <common/config.h> |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 22 | #include <common/eb32tree.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 25 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <types/buffers.h> |
| 27 | #include <types/global.h> |
| 28 | #include <types/polling.h> |
| 29 | #include <types/proxy.h> |
| 30 | #include <types/server.h> |
| 31 | #include <types/session.h> |
| 32 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 33 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/backend.h> |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 35 | #include <proto/client.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | #include <proto/fd.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 37 | #include <proto/httperr.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <proto/log.h> |
| 39 | #include <proto/proto_http.h> |
| 40 | #include <proto/queue.h> |
| 41 | #include <proto/stream_sock.h> |
| 42 | #include <proto/task.h> |
| 43 | |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 44 | #ifdef CONFIG_HAP_CTTPROXY |
| 45 | #include <import/ip_tproxy.h> |
| 46 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 48 | #ifdef CONFIG_HAP_TCPSPLICE |
| 49 | #include <libtcpsplice.h> |
| 50 | #endif |
| 51 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 52 | static inline void fwrr_remove_from_tree(struct server *s); |
| 53 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s); |
| 54 | static inline void fwrr_dequeue_srv(struct server *s); |
| 55 | static void fwrr_get_srv(struct server *s); |
| 56 | static void fwrr_queue_srv(struct server *s); |
| 57 | |
| 58 | /* This function returns non-zero if a server with the given weight and state |
| 59 | * is usable for LB, otherwise zero. |
| 60 | */ |
| 61 | static inline int srv_is_usable(int state, int weight) |
| 62 | { |
| 63 | if (!weight) |
| 64 | return 0; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 65 | if (state & SRV_GOINGDOWN) |
| 66 | return 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 67 | if (!(state & SRV_RUNNING)) |
| 68 | return 0; |
| 69 | return 1; |
| 70 | } |
| 71 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | /* |
| 73 | * This function recounts the number of usable active and backup servers for |
| 74 | * 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] | 75 | * This function also recomputes the total active and backup weights. However, |
| 76 | * it does nout update tot_weight nor tot_used. Use update_backend_weight() for |
| 77 | * this. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 79 | static void recount_servers(struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | { |
| 81 | struct server *srv; |
| 82 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 83 | px->srv_act = px->srv_bck = 0; |
| 84 | px->lbprm.tot_wact = px->lbprm.tot_wbck = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 85 | px->lbprm.fbck = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 87 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 88 | continue; |
| 89 | |
| 90 | if (srv->state & SRV_BACKUP) { |
| 91 | if (!px->srv_bck && |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 92 | !(px->lbprm.algo & PR_O_USE_ALL_BK)) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 93 | px->lbprm.fbck = srv; |
| 94 | px->srv_bck++; |
| 95 | px->lbprm.tot_wbck += srv->eweight; |
| 96 | } else { |
| 97 | px->srv_act++; |
| 98 | px->lbprm.tot_wact += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
| 100 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 101 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 103 | /* This function simply updates the backend's tot_weight and tot_used values |
| 104 | * after servers weights have been updated. It is designed to be used after |
| 105 | * recount_servers() or equivalent. |
| 106 | */ |
| 107 | static void update_backend_weight(struct proxy *px) |
| 108 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 109 | if (px->srv_act) { |
| 110 | px->lbprm.tot_weight = px->lbprm.tot_wact; |
| 111 | px->lbprm.tot_used = px->srv_act; |
| 112 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 113 | else if (px->lbprm.fbck) { |
| 114 | /* use only the first backup server */ |
| 115 | px->lbprm.tot_weight = px->lbprm.fbck->eweight; |
| 116 | px->lbprm.tot_used = 1; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 117 | } |
| 118 | else { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 119 | px->lbprm.tot_weight = px->lbprm.tot_wbck; |
| 120 | px->lbprm.tot_used = px->srv_bck; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 121 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* this function updates the map according to server <srv>'s new state */ |
| 125 | static void map_set_server_status_down(struct server *srv) |
| 126 | { |
| 127 | struct proxy *p = srv->proxy; |
| 128 | |
| 129 | if (srv->state == srv->prev_state && |
| 130 | srv->eweight == srv->prev_eweight) |
| 131 | return; |
| 132 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 133 | if (srv_is_usable(srv->state, srv->eweight)) |
| 134 | goto out_update_state; |
| 135 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 136 | /* FIXME: could be optimized since we know what changed */ |
| 137 | recount_servers(p); |
| 138 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 139 | p->lbprm.map.state |= PR_MAP_RECALC; |
| 140 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 141 | srv->prev_state = srv->state; |
| 142 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 145 | /* This function updates the map according to server <srv>'s new state */ |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 146 | static void map_set_server_status_up(struct server *srv) |
| 147 | { |
| 148 | struct proxy *p = srv->proxy; |
| 149 | |
| 150 | if (srv->state == srv->prev_state && |
| 151 | srv->eweight == srv->prev_eweight) |
| 152 | return; |
| 153 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 154 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 155 | goto out_update_state; |
| 156 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 157 | /* FIXME: could be optimized since we know what changed */ |
| 158 | recount_servers(p); |
| 159 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 160 | p->lbprm.map.state |= PR_MAP_RECALC; |
| 161 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 162 | srv->prev_state = srv->state; |
| 163 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 166 | /* This function recomputes the server map for proxy px. It relies on |
| 167 | * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be |
| 168 | * called after recount_servers(). It also expects px->lbprm.map.srv |
| 169 | * to be allocated with the largest size needed. It updates tot_weight. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | */ |
| 171 | void recalc_server_map(struct proxy *px) |
| 172 | { |
| 173 | int o, tot, flag; |
| 174 | struct server *cur, *best; |
| 175 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 176 | switch (px->lbprm.tot_used) { |
| 177 | case 0: /* no server */ |
| 178 | px->lbprm.map.state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | return; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 180 | case 1: /* only one server, just fill first entry */ |
| 181 | tot = 1; |
| 182 | break; |
| 183 | default: |
| 184 | tot = px->lbprm.tot_weight; |
| 185 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 188 | /* here we *know* that we have some servers */ |
| 189 | if (px->srv_act) |
| 190 | flag = SRV_RUNNING; |
| 191 | else |
| 192 | flag = SRV_RUNNING | SRV_BACKUP; |
| 193 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 194 | /* this algorithm gives priority to the first server, which means that |
| 195 | * it will respect the declaration order for equivalent weights, and |
| 196 | * that whatever the weights, the first server called will always be |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 197 | * the first declared. This is an important asumption for the backup |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 198 | * case, where we want the first server only. |
| 199 | */ |
| 200 | for (cur = px->srv; cur; cur = cur->next) |
| 201 | cur->wscore = 0; |
| 202 | |
| 203 | for (o = 0; o < tot; o++) { |
| 204 | int max = 0; |
| 205 | best = NULL; |
| 206 | for (cur = px->srv; cur; cur = cur->next) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 207 | if (flag == (cur->state & |
| 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, |
| 254 | * using Euclide's largest common divisor algorithm |
| 255 | */ |
| 256 | pgcd = p->srv->uweight; |
| 257 | for (srv = p->srv->next; srv && pgcd > 1; srv = srv->next) { |
| 258 | int w = srv->uweight; |
| 259 | while (w) { |
| 260 | int t = pgcd % w; |
| 261 | pgcd = w; |
| 262 | w = t; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* It is sometimes useful to know what factor to apply |
| 267 | * to the backend's effective weight to know its real |
| 268 | * weight. |
| 269 | */ |
| 270 | p->lbprm.wmult = pgcd; |
| 271 | |
| 272 | act = bck = 0; |
| 273 | for (srv = p->srv; srv; srv = srv->next) { |
| 274 | srv->eweight = srv->uweight / pgcd; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 275 | srv->prev_eweight = srv->eweight; |
| 276 | srv->prev_state = srv->state; |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 277 | if (srv->state & SRV_BACKUP) |
| 278 | bck += srv->eweight; |
| 279 | else |
| 280 | act += srv->eweight; |
| 281 | } |
| 282 | |
| 283 | /* this is the largest map we will ever need for this servers list */ |
| 284 | if (act < bck) |
| 285 | act = bck; |
| 286 | |
| 287 | p->lbprm.map.srv = (struct server **)calloc(act, sizeof(struct server *)); |
| 288 | /* recounts servers and their weights */ |
| 289 | p->lbprm.map.state = PR_MAP_RECALC; |
| 290 | recount_servers(p); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 291 | update_backend_weight(p); |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 292 | recalc_server_map(p); |
| 293 | } |
| 294 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 295 | /* This function updates the server trees according to server <srv>'s new |
| 296 | * 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] | 297 | * It is not important whether the server was already down or not. It is not |
| 298 | * important either that the new state is completely down (the caller may not |
| 299 | * know all the variables of a server's state). |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 300 | */ |
| 301 | static void fwrr_set_server_status_down(struct server *srv) |
| 302 | { |
| 303 | struct proxy *p = srv->proxy; |
| 304 | struct fwrr_group *grp; |
| 305 | |
| 306 | if (srv->state == srv->prev_state && |
| 307 | srv->eweight == srv->prev_eweight) |
| 308 | return; |
| 309 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 310 | if (srv_is_usable(srv->state, srv->eweight)) |
| 311 | goto out_update_state; |
| 312 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 313 | if (!srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 314 | /* server was already down */ |
| 315 | goto out_update_backend; |
| 316 | |
| 317 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 318 | grp->next_weight -= srv->prev_eweight; |
| 319 | |
| 320 | if (srv->state & SRV_BACKUP) { |
| 321 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 322 | p->srv_bck--; |
| 323 | |
| 324 | if (srv == p->lbprm.fbck) { |
| 325 | /* we lost the first backup server in a single-backup |
| 326 | * configuration, we must search another one. |
| 327 | */ |
| 328 | struct server *srv2 = p->lbprm.fbck; |
| 329 | do { |
| 330 | srv2 = srv2->next; |
| 331 | } while (srv2 && |
| 332 | !((srv2->state & SRV_BACKUP) && |
| 333 | srv_is_usable(srv2->state, srv2->eweight))); |
| 334 | p->lbprm.fbck = srv2; |
| 335 | } |
| 336 | } else { |
| 337 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 338 | p->srv_act--; |
| 339 | } |
| 340 | |
| 341 | fwrr_dequeue_srv(srv); |
| 342 | fwrr_remove_from_tree(srv); |
| 343 | |
| 344 | out_update_backend: |
| 345 | /* check/update tot_used, tot_weight */ |
| 346 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 347 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 348 | srv->prev_state = srv->state; |
| 349 | srv->prev_eweight = srv->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* This function updates the server trees according to server <srv>'s new |
| 353 | * 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] | 354 | * It is not important whether the server was already down or not. It is not |
| 355 | * important either that the new state is completely UP (the caller may not |
| 356 | * 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] | 357 | * the weight of a server which was already up. |
| 358 | */ |
| 359 | static void fwrr_set_server_status_up(struct server *srv) |
| 360 | { |
| 361 | struct proxy *p = srv->proxy; |
| 362 | struct fwrr_group *grp; |
| 363 | |
| 364 | if (srv->state == srv->prev_state && |
| 365 | srv->eweight == srv->prev_eweight) |
| 366 | return; |
| 367 | |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 368 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 369 | goto out_update_state; |
| 370 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 371 | if (srv_is_usable(srv->prev_state, srv->prev_eweight)) |
| 372 | /* server was already up */ |
| 373 | goto out_update_backend; |
| 374 | |
| 375 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 376 | grp->next_weight += srv->eweight; |
| 377 | |
| 378 | if (srv->state & SRV_BACKUP) { |
| 379 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 380 | p->srv_bck++; |
| 381 | |
| 382 | if (p->lbprm.fbck) { |
| 383 | /* we may have restored a backup server prior to fbck, |
| 384 | * in which case it should replace it. |
| 385 | */ |
| 386 | struct server *srv2 = srv; |
| 387 | do { |
| 388 | srv2 = srv2->next; |
| 389 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 390 | if (srv2) |
| 391 | p->lbprm.fbck = srv; |
| 392 | } |
| 393 | } else { |
| 394 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 395 | p->srv_act++; |
| 396 | } |
| 397 | |
| 398 | /* note that eweight cannot be 0 here */ |
| 399 | fwrr_get_srv(srv); |
| 400 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 401 | fwrr_queue_srv(srv); |
| 402 | |
| 403 | out_update_backend: |
| 404 | /* check/update tot_used, tot_weight */ |
| 405 | update_backend_weight(p); |
Willy Tarreau | 0ebe106 | 2007-11-30 11:11:02 +0100 | [diff] [blame] | 406 | out_update_state: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 407 | srv->prev_state = srv->state; |
| 408 | srv->prev_eweight = srv->eweight; |
| 409 | } |
| 410 | |
| 411 | /* This function must be called after an update to server <srv>'s effective |
| 412 | * weight. It may be called after a state change too. |
| 413 | */ |
| 414 | static void fwrr_update_server_weight(struct server *srv) |
| 415 | { |
| 416 | int old_state, new_state; |
| 417 | struct proxy *p = srv->proxy; |
| 418 | struct fwrr_group *grp; |
| 419 | |
| 420 | if (srv->state == srv->prev_state && |
| 421 | srv->eweight == srv->prev_eweight) |
| 422 | return; |
| 423 | |
| 424 | /* If changing the server's weight changes its state, we simply apply |
| 425 | * the procedures we already have for status change. If the state |
| 426 | * remains down, the server is not in any tree, so it's as easy as |
| 427 | * updating its values. If the state remains up with different weights, |
| 428 | * there are some computations to perform to find a new place and |
| 429 | * possibly a new tree for this server. |
| 430 | */ |
| 431 | |
| 432 | old_state = srv_is_usable(srv->prev_state, srv->prev_eweight); |
| 433 | new_state = srv_is_usable(srv->state, srv->eweight); |
| 434 | |
| 435 | if (!old_state && !new_state) { |
| 436 | srv->prev_state = srv->state; |
| 437 | srv->prev_eweight = srv->eweight; |
| 438 | return; |
| 439 | } |
| 440 | else if (!old_state && new_state) { |
| 441 | fwrr_set_server_status_up(srv); |
| 442 | return; |
| 443 | } |
| 444 | else if (old_state && !new_state) { |
| 445 | fwrr_set_server_status_down(srv); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 450 | grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight; |
| 451 | |
| 452 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 453 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 454 | |
| 455 | if (srv->lb_tree == grp->init) { |
| 456 | fwrr_dequeue_srv(srv); |
| 457 | fwrr_queue_by_weight(grp->init, srv); |
| 458 | } |
| 459 | else if (!srv->lb_tree) { |
| 460 | /* FIXME: server was down. This is not possible right now but |
| 461 | * may be needed soon for slowstart or graceful shutdown. |
| 462 | */ |
| 463 | fwrr_dequeue_srv(srv); |
| 464 | fwrr_get_srv(srv); |
| 465 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight; |
| 466 | fwrr_queue_srv(srv); |
| 467 | } else { |
| 468 | /* The server is either active or in the next queue. If it's |
| 469 | * still in the active queue and it has not consumed all of its |
| 470 | * places, let's adjust its next position. |
| 471 | */ |
| 472 | fwrr_get_srv(srv); |
| 473 | |
| 474 | if (srv->eweight > 0) { |
| 475 | int prev_next = srv->npos; |
| 476 | int step = grp->next_weight / srv->eweight; |
| 477 | |
| 478 | srv->npos = srv->lpos + step; |
| 479 | srv->rweight = 0; |
| 480 | |
| 481 | if (srv->npos > prev_next) |
| 482 | srv->npos = prev_next; |
| 483 | if (srv->npos < grp->curr_pos + 2) |
| 484 | srv->npos = grp->curr_pos + step; |
| 485 | } else { |
| 486 | /* push it into the next tree */ |
| 487 | srv->npos = grp->curr_pos + grp->curr_weight; |
| 488 | } |
| 489 | |
| 490 | fwrr_dequeue_srv(srv); |
| 491 | fwrr_queue_srv(srv); |
| 492 | } |
| 493 | |
| 494 | update_backend_weight(p); |
| 495 | srv->prev_state = srv->state; |
| 496 | srv->prev_eweight = srv->eweight; |
| 497 | } |
| 498 | |
| 499 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 500 | * function is meant to be called when a server is going down or has its |
| 501 | * weight disabled. |
| 502 | */ |
| 503 | static inline void fwrr_remove_from_tree(struct server *s) |
| 504 | { |
| 505 | s->lb_tree = NULL; |
| 506 | } |
| 507 | |
| 508 | /* Queue a server in the weight tree <root>, assuming the weight is >0. |
| 509 | * We want to sort them by inverted weights, because we need to place |
| 510 | * heavy servers first in order to get a smooth distribution. |
| 511 | */ |
| 512 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s) |
| 513 | { |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 514 | s->lb_node.key = SRV_EWGHT_MAX - s->eweight; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 515 | eb32_insert(root, &s->lb_node); |
| 516 | s->lb_tree = root; |
| 517 | } |
| 518 | |
| 519 | /* This function is responsible for building the weight trees in case of fast |
| 520 | * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight |
| 521 | * ratio. Both active and backup groups are initialized. |
| 522 | */ |
| 523 | void fwrr_init_server_groups(struct proxy *p) |
| 524 | { |
| 525 | struct server *srv; |
| 526 | struct eb_root init_head = EB_ROOT; |
| 527 | |
| 528 | p->lbprm.set_server_status_up = fwrr_set_server_status_up; |
| 529 | p->lbprm.set_server_status_down = fwrr_set_server_status_down; |
| 530 | p->lbprm.update_server_eweight = fwrr_update_server_weight; |
| 531 | |
| 532 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 533 | for (srv = p->srv; srv; srv = srv->next) { |
| 534 | srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE; |
| 535 | srv->prev_state = srv->state; |
| 536 | } |
| 537 | |
| 538 | recount_servers(p); |
| 539 | update_backend_weight(p); |
| 540 | |
| 541 | /* prepare the active servers group */ |
| 542 | p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight = |
| 543 | p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact; |
| 544 | p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 = |
| 545 | p->lbprm.fwrr.act.t1 = init_head; |
| 546 | p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0; |
| 547 | p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1; |
| 548 | |
| 549 | /* prepare the backup servers group */ |
| 550 | p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight = |
| 551 | p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck; |
| 552 | p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 = |
| 553 | p->lbprm.fwrr.bck.t1 = init_head; |
| 554 | p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0; |
| 555 | p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1; |
| 556 | |
| 557 | /* queue active and backup servers in two distinct groups */ |
| 558 | for (srv = p->srv; srv; srv = srv->next) { |
| 559 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 560 | continue; |
| 561 | fwrr_queue_by_weight((srv->state & SRV_BACKUP) ? |
| 562 | p->lbprm.fwrr.bck.init : |
| 563 | p->lbprm.fwrr.act.init, |
| 564 | srv); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* simply removes a server from a weight tree */ |
| 569 | static inline void fwrr_dequeue_srv(struct server *s) |
| 570 | { |
| 571 | eb32_delete(&s->lb_node); |
| 572 | } |
| 573 | |
| 574 | /* queues a server into the appropriate group and tree depending on its |
| 575 | * backup status, and ->npos. If the server is disabled, simply assign |
| 576 | * it to the NULL tree. |
| 577 | */ |
| 578 | static void fwrr_queue_srv(struct server *s) |
| 579 | { |
| 580 | struct proxy *p = s->proxy; |
| 581 | struct fwrr_group *grp; |
| 582 | |
| 583 | grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
| 584 | |
| 585 | /* Delay everything which does not fit into the window and everything |
| 586 | * which does not fit into the theorical new window. |
| 587 | */ |
| 588 | if (!srv_is_usable(s->state, s->eweight)) { |
| 589 | fwrr_remove_from_tree(s); |
| 590 | } |
| 591 | else if (s->eweight <= 0 || |
| 592 | s->npos >= 2 * grp->curr_weight || |
| 593 | s->npos >= grp->curr_weight + grp->next_weight) { |
| 594 | /* put into next tree, and readjust npos in case we could |
| 595 | * finally take this back to current. */ |
| 596 | s->npos -= grp->curr_weight; |
| 597 | fwrr_queue_by_weight(grp->next, s); |
| 598 | } |
| 599 | else { |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 600 | /* The sorting key is stored in units of s->npos * user_weight |
| 601 | * in order to avoid overflows. As stated in backend.h, the |
| 602 | * lower the scale, the rougher the weights modulation, and the |
| 603 | * higher the scale, the lower the number of servers without |
| 604 | * overflow. With this formula, the result is always positive, |
| 605 | * so we can use eb3é_insert(). |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 606 | */ |
Willy Tarreau | b698f0f | 2007-12-02 11:01:23 +0100 | [diff] [blame] | 607 | s->lb_node.key = SRV_UWGHT_RANGE * s->npos + |
| 608 | (unsigned)(SRV_EWGHT_MAX + s->rweight - s->eweight) / BE_WEIGHT_SCALE; |
| 609 | |
| 610 | eb32_insert(&grp->curr, &s->lb_node); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 611 | s->lb_tree = &grp->curr; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* prepares a server when extracting it from the "init" tree */ |
| 616 | static inline void fwrr_get_srv_init(struct server *s) |
| 617 | { |
| 618 | s->npos = s->rweight = 0; |
| 619 | } |
| 620 | |
| 621 | /* prepares a server when extracting it from the "next" tree */ |
| 622 | static inline void fwrr_get_srv_next(struct server *s) |
| 623 | { |
| 624 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 625 | &s->proxy->lbprm.fwrr.bck : |
| 626 | &s->proxy->lbprm.fwrr.act; |
| 627 | |
| 628 | s->npos += grp->curr_weight; |
| 629 | } |
| 630 | |
| 631 | /* prepares a server when it was marked down */ |
| 632 | static inline void fwrr_get_srv_down(struct server *s) |
| 633 | { |
| 634 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 635 | &s->proxy->lbprm.fwrr.bck : |
| 636 | &s->proxy->lbprm.fwrr.act; |
| 637 | |
| 638 | s->npos = grp->curr_pos; |
| 639 | } |
| 640 | |
| 641 | /* prepares a server when extracting it from its tree */ |
| 642 | static void fwrr_get_srv(struct server *s) |
| 643 | { |
| 644 | struct proxy *p = s->proxy; |
| 645 | struct fwrr_group *grp = (s->state & SRV_BACKUP) ? |
| 646 | &p->lbprm.fwrr.bck : |
| 647 | &p->lbprm.fwrr.act; |
| 648 | |
| 649 | if (s->lb_tree == grp->init) { |
| 650 | fwrr_get_srv_init(s); |
| 651 | } |
| 652 | else if (s->lb_tree == grp->next) { |
| 653 | fwrr_get_srv_next(s); |
| 654 | } |
| 655 | else if (s->lb_tree == NULL) { |
| 656 | fwrr_get_srv_down(s); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty |
| 661 | * when this happens, and "next" filled with servers sorted by weights. |
| 662 | */ |
| 663 | static inline void fwrr_switch_trees(struct fwrr_group *grp) |
| 664 | { |
| 665 | struct eb_root *swap; |
| 666 | swap = grp->init; |
| 667 | grp->init = grp->next; |
| 668 | grp->next = swap; |
| 669 | grp->curr_weight = grp->next_weight; |
| 670 | grp->curr_pos = grp->curr_weight; |
| 671 | } |
| 672 | |
| 673 | /* return next server from the current tree in FWRR group <grp>, or a server |
| 674 | * from the "init" tree if appropriate. If both trees are empty, return NULL. |
| 675 | */ |
| 676 | static struct server *fwrr_get_server_from_group(struct fwrr_group *grp) |
| 677 | { |
| 678 | struct eb32_node *node; |
| 679 | struct server *s; |
| 680 | |
| 681 | node = eb32_first(&grp->curr); |
| 682 | s = eb32_entry(node, struct server, lb_node); |
| 683 | |
| 684 | if (!node || s->npos > grp->curr_pos) { |
| 685 | /* either we have no server left, or we have a hole */ |
| 686 | struct eb32_node *node2; |
| 687 | node2 = eb32_first(grp->init); |
| 688 | if (node2) { |
| 689 | node = node2; |
| 690 | s = eb32_entry(node, struct server, lb_node); |
| 691 | fwrr_get_srv_init(s); |
| 692 | if (s->eweight == 0) /* FIXME: is it possible at all ? */ |
| 693 | node = NULL; |
| 694 | } |
| 695 | } |
| 696 | if (node) |
| 697 | return s; |
| 698 | else |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | /* Computes next position of server <s> in the group. It is mandatory for <s> |
| 703 | * to have a non-zero, positive eweight. |
| 704 | */ |
| 705 | static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s) |
| 706 | { |
| 707 | if (!s->npos) { |
| 708 | /* first time ever for this server */ |
| 709 | s->lpos = grp->curr_pos; |
| 710 | s->npos = grp->curr_pos + grp->next_weight / s->eweight; |
| 711 | s->rweight += grp->next_weight % s->eweight; |
| 712 | |
| 713 | if (s->rweight >= s->eweight) { |
| 714 | s->rweight -= s->eweight; |
| 715 | s->npos++; |
| 716 | } |
| 717 | } else { |
| 718 | s->lpos = s->npos; |
| 719 | s->npos += grp->next_weight / s->eweight; |
| 720 | s->rweight += grp->next_weight % s->eweight; |
| 721 | |
| 722 | if (s->rweight >= s->eweight) { |
| 723 | s->rweight -= s->eweight; |
| 724 | s->npos++; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* Return next server from the current tree in backend <p>, or a server from |
| 730 | * the init tree if appropriate. If both trees are empty, return NULL. |
| 731 | * Saturated servers are skipped and requeued. |
| 732 | */ |
| 733 | static struct server *fwrr_get_next_server(struct proxy *p) |
| 734 | { |
| 735 | struct server *srv; |
| 736 | struct fwrr_group *grp; |
| 737 | struct server *full; |
| 738 | int switched; |
| 739 | |
| 740 | if (p->srv_act) |
| 741 | grp = &p->lbprm.fwrr.act; |
| 742 | else if (p->lbprm.fbck) |
| 743 | return p->lbprm.fbck; |
| 744 | else if (p->srv_bck) |
| 745 | grp = &p->lbprm.fwrr.bck; |
| 746 | else |
| 747 | return NULL; |
| 748 | |
| 749 | switched = 0; |
| 750 | full = NULL; /* NULL-terminated list of saturated servers */ |
| 751 | while (1) { |
| 752 | /* if we see an empty group, let's first try to collect weights |
| 753 | * which might have recently changed. |
| 754 | */ |
| 755 | if (!grp->curr_weight) |
| 756 | grp->curr_pos = grp->curr_weight = grp->next_weight; |
| 757 | |
| 758 | /* get first server from the "current" tree. When the end of |
| 759 | * the tree is reached, we may have to switch, but only once. |
| 760 | */ |
| 761 | while (1) { |
| 762 | srv = fwrr_get_server_from_group(grp); |
| 763 | if (srv) |
| 764 | break; |
| 765 | if (switched) |
| 766 | goto requeue_servers; |
| 767 | switched = 1; |
| 768 | fwrr_switch_trees(grp); |
| 769 | |
| 770 | } |
| 771 | |
| 772 | /* OK, we have a server. However, it may be saturated, in which |
| 773 | * case we don't want to reconsider it for now. We'll update |
| 774 | * its position and dequeue it anyway, so that we can move it |
| 775 | * to a better place afterwards. |
| 776 | */ |
| 777 | fwrr_update_position(grp, srv); |
| 778 | fwrr_dequeue_srv(srv); |
| 779 | grp->curr_pos++; |
| 780 | if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) |
| 781 | break; |
| 782 | |
| 783 | /* the server is saturated, let's chain it for later reinsertion */ |
| 784 | srv->next_full = full; |
| 785 | full = srv; |
| 786 | } |
| 787 | |
| 788 | /* OK, we got the best server, let's update it */ |
| 789 | fwrr_queue_srv(srv); |
| 790 | |
| 791 | requeue_servers: |
| 792 | if (unlikely(full)) { |
| 793 | if (switched) { |
| 794 | /* the tree has switched, requeue all extracted servers |
| 795 | * into "init", because their place was lost, and only |
| 796 | * their weight matters. |
| 797 | */ |
| 798 | do { |
| 799 | fwrr_queue_by_weight(grp->init, full); |
| 800 | full = full->next_full; |
| 801 | } while (full); |
| 802 | } else { |
| 803 | /* requeue all extracted servers just as if they were consumed |
| 804 | * so that they regain their expected place. |
| 805 | */ |
| 806 | do { |
| 807 | fwrr_queue_srv(full); |
| 808 | full = full->next_full; |
| 809 | } while (full); |
| 810 | } |
| 811 | } |
| 812 | return srv; |
| 813 | } |
| 814 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 815 | /* |
| 816 | * This function tries to find a running server for the proxy <px> following |
| 817 | * the URL parameter hash method. It looks for a specific parameter in the |
| 818 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 819 | * performance by avoiding bounces between servers in contexts where sessions |
| 820 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 821 | * is returned. If any server is found, it will be returned. If no valid server |
| 822 | * is found, NULL is returned. |
| 823 | * |
| 824 | */ |
| 825 | struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len) |
| 826 | { |
| 827 | unsigned long hash = 0; |
| 828 | char *p; |
| 829 | int plen; |
| 830 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 831 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 832 | return NULL; |
| 833 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 834 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 835 | recalc_server_map(px); |
| 836 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 837 | p = memchr(uri, '?', uri_len); |
| 838 | if (!p) |
| 839 | return NULL; |
| 840 | p++; |
| 841 | |
| 842 | uri_len -= (p - uri); |
| 843 | plen = px->url_param_len; |
| 844 | |
| 845 | if (uri_len <= plen) |
| 846 | return NULL; |
| 847 | |
| 848 | while (uri_len > plen) { |
| 849 | /* Look for the parameter name followed by an equal symbol */ |
| 850 | if (p[plen] == '=') { |
| 851 | /* skip the equal symbol */ |
| 852 | uri = p; |
| 853 | p += plen + 1; |
| 854 | uri_len -= plen + 1; |
| 855 | if (memcmp(uri, px->url_param_name, plen) == 0) { |
| 856 | /* OK, we have the parameter here at <uri>, and |
| 857 | * the value after the equal sign, at <p> |
| 858 | */ |
| 859 | while (uri_len && *p != '&') { |
| 860 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 861 | uri_len--; |
| 862 | p++; |
| 863 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 864 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | /* skip to next parameter */ |
| 869 | uri = p; |
| 870 | p = memchr(uri, '&', uri_len); |
| 871 | if (!p) |
| 872 | return NULL; |
| 873 | p++; |
| 874 | uri_len -= (p - uri); |
| 875 | } |
| 876 | return NULL; |
| 877 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * This function marks the session as 'assigned' in direct or dispatch modes, |
| 881 | * or tries to assign one in balance mode, according to the algorithm. It does |
| 882 | * nothing if the session had already been assigned a server. |
| 883 | * |
| 884 | * It may return : |
| 885 | * SRV_STATUS_OK if everything is OK. s->srv will be valid. |
| 886 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 887 | * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL. |
| 888 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 889 | * |
| 890 | * Upon successful return, the session flag SN_ASSIGNED to indicate that it does |
| 891 | * not need to be called anymore. This usually means that s->srv can be trusted |
| 892 | * in balance and direct modes. This flag is not cleared, so it's to the caller |
| 893 | * to clear it if required (eg: redispatch). |
| 894 | * |
| 895 | */ |
| 896 | |
| 897 | int assign_server(struct session *s) |
| 898 | { |
| 899 | #ifdef DEBUG_FULL |
| 900 | fprintf(stderr,"assign_server : s=%p\n",s); |
| 901 | #endif |
| 902 | |
| 903 | if (s->pend_pos) |
| 904 | return SRV_STATUS_INTERNAL; |
| 905 | |
| 906 | if (!(s->flags & SN_ASSIGNED)) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 907 | if (s->be->lbprm.algo & BE_LB_ALGO) { |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 908 | int len; |
| 909 | |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 910 | if (s->flags & SN_DIRECT) { |
| 911 | s->flags |= SN_ASSIGNED; |
| 912 | return SRV_STATUS_OK; |
| 913 | } |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 914 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 915 | if (!s->be->lbprm.tot_weight) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 916 | return SRV_STATUS_NOSRV; |
| 917 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 918 | switch (s->be->lbprm.algo & BE_LB_ALGO) { |
| 919 | case BE_LB_ALGO_RR: |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 920 | s->srv = fwrr_get_next_server(s->be); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | if (!s->srv) |
| 922 | return SRV_STATUS_FULL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 923 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 924 | case BE_LB_ALGO_SH: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 925 | if (s->cli_addr.ss_family == AF_INET) |
| 926 | len = 4; |
| 927 | else if (s->cli_addr.ss_family == AF_INET6) |
| 928 | len = 16; |
| 929 | else /* unknown IP family */ |
| 930 | return SRV_STATUS_INTERNAL; |
| 931 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 932 | s->srv = get_server_sh(s->be, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 933 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 934 | len); |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 935 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 936 | case BE_LB_ALGO_UH: |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 937 | /* URI hashing */ |
| 938 | s->srv = get_server_uh(s->be, |
| 939 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 940 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 941 | break; |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 942 | case BE_LB_ALGO_PH: |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 943 | /* URL Parameter hashing */ |
| 944 | s->srv = get_server_ph(s->be, |
| 945 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 946 | s->txn.req.sl.rq.u_l); |
| 947 | if (!s->srv) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 948 | /* parameter not found, fall back to round robin on the map */ |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 949 | s->srv = get_server_rr_with_conns(s->be); |
| 950 | if (!s->srv) |
| 951 | return SRV_STATUS_FULL; |
| 952 | } |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 953 | break; |
| 954 | default: |
| 955 | /* unknown balancing algorithm */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 956 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 957 | } |
Willy Tarreau | ddbb82f | 2007-12-05 10:34:49 +0100 | [diff] [blame] | 958 | s->be->cum_lbconn++; |
| 959 | s->srv->cum_lbconn++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 960 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 961 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 962 | if (!s->srv_addr.sin_addr.s_addr) |
| 963 | return SRV_STATUS_NOSRV; |
| 964 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 965 | else if (!*(int *)&s->be->dispatch_addr.sin_addr && |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 966 | !(s->fe->options & PR_O_TRANSP)) { |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 967 | return SRV_STATUS_NOSRV; |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 968 | } |
| 969 | s->flags |= SN_ASSIGNED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 970 | } |
| 971 | return SRV_STATUS_OK; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | /* |
| 976 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 977 | * The address is taken from the currently assigned server, or from the |
| 978 | * dispatch or transparent address. |
| 979 | * |
| 980 | * It may return : |
| 981 | * SRV_STATUS_OK if everything is OK. |
| 982 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 983 | * |
| 984 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 985 | * not cleared, so it's to the caller to clear it if required. |
| 986 | * |
| 987 | */ |
| 988 | int assign_server_address(struct session *s) |
| 989 | { |
| 990 | #ifdef DEBUG_FULL |
| 991 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 992 | #endif |
| 993 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 994 | if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_ALGO)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 995 | /* A server is necessarily known for this session */ |
| 996 | if (!(s->flags & SN_ASSIGNED)) |
| 997 | return SRV_STATUS_INTERNAL; |
| 998 | |
| 999 | s->srv_addr = s->srv->addr; |
| 1000 | |
| 1001 | /* if this server remaps proxied ports, we'll use |
| 1002 | * the port the client connected to with an offset. */ |
| 1003 | if (s->srv->state & SRV_MAPPORTS) { |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 1004 | if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET)) |
| 1005 | get_frt_addr(s); |
| 1006 | if (s->frt_addr.ss_family == AF_INET) { |
| 1007 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 1008 | ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port)); |
| 1009 | } else { |
| 1010 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 1011 | ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port)); |
| 1012 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1013 | } |
| 1014 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1015 | else if (*(int *)&s->be->dispatch_addr.sin_addr) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1016 | /* connect to the defined dispatch addr */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1017 | s->srv_addr = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1018 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 1019 | else if (s->fe->options & PR_O_TRANSP) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1020 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
| 1021 | socklen_t salen = sizeof(s->srv_addr); |
| 1022 | |
| 1023 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1024 | qfprintf(stderr, "Cannot get original server address.\n"); |
| 1025 | return SRV_STATUS_INTERNAL; |
| 1026 | } |
| 1027 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1028 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 1029 | /* If HTTP PROXY option is set, then server is already assigned |
| 1030 | * during incoming client request parsing. */ |
| 1031 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 1032 | else { |
| 1033 | /* no server and no LB algorithm ! */ |
| 1034 | return SRV_STATUS_INTERNAL; |
| 1035 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1036 | |
| 1037 | s->flags |= SN_ADDR_SET; |
| 1038 | return SRV_STATUS_OK; |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | /* This function assigns a server to session <s> if required, and can add the |
| 1043 | * connection to either the assigned server's queue or to the proxy's queue. |
| 1044 | * |
| 1045 | * Returns : |
| 1046 | * |
| 1047 | * SRV_STATUS_OK if everything is OK. |
| 1048 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 1049 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 1050 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
| 1051 | * connection could not be queued. |
| 1052 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 1053 | * |
| 1054 | */ |
| 1055 | int assign_server_and_queue(struct session *s) |
| 1056 | { |
| 1057 | struct pendconn *p; |
| 1058 | int err; |
| 1059 | |
| 1060 | if (s->pend_pos) |
| 1061 | return SRV_STATUS_INTERNAL; |
| 1062 | |
| 1063 | if (s->flags & SN_ASSIGNED) { |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 1064 | if (s->srv && s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) { |
| 1065 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 1066 | s->srv = NULL; |
| 1067 | http_flush_cookie_flags(&s->txn); |
| 1068 | } else { |
| 1069 | /* a server does not need to be assigned, perhaps because we're in |
| 1070 | * direct mode, or in dispatch or transparent modes where the server |
| 1071 | * is not needed. |
| 1072 | */ |
| 1073 | if (s->srv && |
| 1074 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 1075 | p = pendconn_add(s); |
| 1076 | if (p) |
| 1077 | return SRV_STATUS_QUEUED; |
| 1078 | else |
| 1079 | return SRV_STATUS_FULL; |
| 1080 | } |
| 1081 | return SRV_STATUS_OK; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1082 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* a server needs to be assigned */ |
| 1086 | err = assign_server(s); |
| 1087 | switch (err) { |
| 1088 | case SRV_STATUS_OK: |
| 1089 | /* in balance mode, we might have servers with connection limits */ |
| 1090 | if (s->srv && |
| 1091 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 1092 | p = pendconn_add(s); |
| 1093 | if (p) |
| 1094 | return SRV_STATUS_QUEUED; |
| 1095 | else |
| 1096 | return SRV_STATUS_FULL; |
| 1097 | } |
| 1098 | return SRV_STATUS_OK; |
| 1099 | |
| 1100 | case SRV_STATUS_FULL: |
| 1101 | /* queue this session into the proxy's queue */ |
| 1102 | p = pendconn_add(s); |
| 1103 | if (p) |
| 1104 | return SRV_STATUS_QUEUED; |
| 1105 | else |
| 1106 | return SRV_STATUS_FULL; |
| 1107 | |
| 1108 | case SRV_STATUS_NOSRV: |
| 1109 | case SRV_STATUS_INTERNAL: |
| 1110 | return err; |
| 1111 | default: |
| 1112 | return SRV_STATUS_INTERNAL; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | /* |
| 1118 | * This function initiates a connection to the server assigned to this session |
| 1119 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 1120 | * It can return one of : |
| 1121 | * - SN_ERR_NONE if everything's OK |
| 1122 | * - SN_ERR_SRVTO if there are no more servers |
| 1123 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1124 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1125 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1126 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1127 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1128 | */ |
| 1129 | int connect_server(struct session *s) |
| 1130 | { |
| 1131 | int fd, err; |
| 1132 | |
| 1133 | if (!(s->flags & SN_ADDR_SET)) { |
| 1134 | err = assign_server_address(s); |
| 1135 | if (err != SRV_STATUS_OK) |
| 1136 | return SN_ERR_INTERNAL; |
| 1137 | } |
| 1138 | |
| 1139 | if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 1140 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 1141 | |
| 1142 | if (errno == ENFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1143 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1144 | "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] | 1145 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1146 | else if (errno == EMFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1147 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1148 | "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] | 1149 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1150 | else if (errno == ENOBUFS || errno == ENOMEM) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1151 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1152 | "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] | 1153 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1154 | /* this is a resource error */ |
| 1155 | return SN_ERR_RESOURCE; |
| 1156 | } |
| 1157 | |
| 1158 | if (fd >= global.maxsock) { |
| 1159 | /* do not log anything there, it's a normal condition when this option |
| 1160 | * is used to serialize connections to a server ! |
| 1161 | */ |
| 1162 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1163 | close(fd); |
| 1164 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 1165 | } |
| 1166 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1167 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1168 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 1169 | /* TCP splicing supported by both FE and BE */ |
| 1170 | tcp_splice_initfd(s->cli_fd, fd); |
| 1171 | } |
| 1172 | #endif |
| 1173 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1174 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1175 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 1176 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 1177 | close(fd); |
| 1178 | return SN_ERR_INTERNAL; |
| 1179 | } |
| 1180 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1181 | if (s->be->options & PR_O_TCP_SRV_KA) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1182 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1183 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 1184 | if (s->be->options & PR_O_TCP_NOLING) |
| 1185 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 1186 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1187 | /* allow specific binding : |
| 1188 | * - server-specific at first |
| 1189 | * - proxy-specific next |
| 1190 | */ |
| 1191 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 1192 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1193 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 1194 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1195 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1196 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1197 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1198 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1199 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1200 | return SN_ERR_RESOURCE; |
| 1201 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1202 | #ifdef CONFIG_HAP_CTTPROXY |
| 1203 | if (s->srv->state & SRV_TPROXY_MASK) { |
| 1204 | struct in_tproxy itp1, itp2; |
| 1205 | memset(&itp1, 0, sizeof(itp1)); |
| 1206 | |
| 1207 | itp1.op = TPROXY_ASSIGN; |
| 1208 | switch (s->srv->state & SRV_TPROXY_MASK) { |
| 1209 | case SRV_TPROXY_ADDR: |
| 1210 | itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr; |
| 1211 | itp1.v.addr.fport = s->srv->tproxy_addr.sin_port; |
| 1212 | break; |
| 1213 | case SRV_TPROXY_CLI: |
| 1214 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 1215 | /* fall through */ |
| 1216 | case SRV_TPROXY_CIP: |
| 1217 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1218 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 1219 | break; |
| 1220 | } |
| 1221 | |
| 1222 | /* set connect flag on socket */ |
| 1223 | itp2.op = TPROXY_FLAGS; |
| 1224 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 1225 | |
| 1226 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 1227 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 1228 | 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] | 1229 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1230 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1231 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1232 | "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] | 1233 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1234 | return SN_ERR_RESOURCE; |
| 1235 | } |
| 1236 | } |
| 1237 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1238 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1239 | else if (s->be->options & PR_O_BIND_SRC) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1240 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1241 | if (bind(fd, (struct sockaddr *)&s->be->source_addr, sizeof(s->be->source_addr)) == -1) { |
| 1242 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->be->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1243 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1244 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1245 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1246 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1247 | return SN_ERR_RESOURCE; |
| 1248 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1249 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1250 | if (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1251 | struct in_tproxy itp1, itp2; |
| 1252 | memset(&itp1, 0, sizeof(itp1)); |
| 1253 | |
| 1254 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1255 | switch (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1256 | case PR_O_TPXY_ADDR: |
Willy Tarreau | 4009f01 | 2007-12-14 19:54:43 +0100 | [diff] [blame] | 1257 | itp1.v.addr.faddr = s->be->tproxy_addr.sin_addr; |
| 1258 | itp1.v.addr.fport = s->be->tproxy_addr.sin_port; |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1259 | break; |
| 1260 | case PR_O_TPXY_CLI: |
| 1261 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 1262 | /* fall through */ |
| 1263 | case PR_O_TPXY_CIP: |
| 1264 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 1265 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | /* set connect flag on socket */ |
| 1270 | itp2.op = TPROXY_FLAGS; |
| 1271 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 1272 | |
| 1273 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 1274 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 1275 | 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] | 1276 | s->be->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1277 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1278 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1279 | "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] | 1280 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 1281 | return SN_ERR_RESOURCE; |
| 1282 | } |
| 1283 | } |
| 1284 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1288 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1289 | |
| 1290 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1291 | char *msg; |
| 1292 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1293 | msg = "no free ports"; |
| 1294 | else |
| 1295 | msg = "local address already in use"; |
| 1296 | |
| 1297 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
| 1298 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1299 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1300 | "Connect() failed for server %s/%s: %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1301 | s->be->id, s->srv->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1302 | return SN_ERR_RESOURCE; |
| 1303 | } else if (errno == ETIMEDOUT) { |
| 1304 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 1305 | close(fd); |
| 1306 | return SN_ERR_SRVTO; |
| 1307 | } else { |
| 1308 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 1309 | //qfprintf(stderr,"Connect(): %d", errno); |
| 1310 | close(fd); |
| 1311 | return SN_ERR_SRVCL; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | fdtab[fd].owner = s->task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1316 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1317 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1318 | fdtab[fd].cb[DIR_RD].b = s->rep; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 1319 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 1320 | fdtab[fd].cb[DIR_WR].b = s->req; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 1321 | |
| 1322 | fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr; |
| 1323 | fdtab[fd].peerlen = sizeof(s->srv_addr); |
| 1324 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1325 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1326 | |
| 1327 | fd_insert(fd); |
| 1328 | if (s->srv) { |
| 1329 | s->srv->cur_sess++; |
| 1330 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 1331 | s->srv->cur_sess_max = s->srv->cur_sess; |
| 1332 | } |
| 1333 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1334 | if (!tv_add_ifset(&s->req->cex, &now, &s->be->timeout.connect)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1335 | tv_eternity(&s->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1336 | return SN_ERR_NONE; /* connection is OK */ |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | /* |
| 1341 | * This function checks the retry count during the connect() job. |
| 1342 | * It updates the session's srv_state and retries, so that the caller knows |
| 1343 | * what it has to do. It uses the last connection error to set the log when |
| 1344 | * it expires. It returns 1 when it has expired, and 0 otherwise. |
| 1345 | */ |
| 1346 | int srv_count_retry_down(struct session *t, int conn_err) |
| 1347 | { |
| 1348 | /* we are in front of a retryable error */ |
| 1349 | t->conn_retries--; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1350 | if (t->srv) |
| 1351 | t->srv->retries++; |
| 1352 | t->be->retries++; |
| 1353 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1354 | if (t->conn_retries < 0) { |
| 1355 | /* if not retryable anymore, let's abort */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1356 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1357 | srv_close_with_err(t, conn_err, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1358 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1359 | if (t->srv) |
| 1360 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1361 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1362 | |
| 1363 | /* We used to have a free connection slot. Since we'll never use it, |
| 1364 | * we have to inform the server that it may be used by another session. |
| 1365 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1366 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1367 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1368 | return 1; |
| 1369 | } |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | /* |
| 1375 | * This function performs the retryable part of the connect() job. |
| 1376 | * It updates the session's srv_state and retries, so that the caller knows |
| 1377 | * what it has to do. It returns 1 when it breaks out of the loop, or 0 if |
| 1378 | * it needs to redispatch. |
| 1379 | */ |
| 1380 | int srv_retryable_connect(struct session *t) |
| 1381 | { |
| 1382 | int conn_err; |
| 1383 | |
| 1384 | /* This loop ensures that we stop before the last retry in case of a |
| 1385 | * redispatchable server. |
| 1386 | */ |
| 1387 | do { |
| 1388 | /* initiate a connection to the server */ |
| 1389 | conn_err = connect_server(t); |
| 1390 | switch (conn_err) { |
| 1391 | |
| 1392 | case SN_ERR_NONE: |
| 1393 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 1394 | t->srv_state = SV_STCONN; |
| 1395 | return 1; |
| 1396 | |
| 1397 | case SN_ERR_INTERNAL: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1398 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1400 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1401 | if (t->srv) |
| 1402 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1403 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1404 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1405 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1406 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1407 | return 1; |
| 1408 | } |
| 1409 | /* ensure that we have enough retries left */ |
| 1410 | if (srv_count_retry_down(t, conn_err)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1411 | return 1; |
| 1412 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1413 | } while (t->srv == NULL || t->conn_retries > 0 || !(t->be->options & PR_O_REDISP)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* We're on our last chance, and the REDISP option was specified. |
| 1416 | * We will ignore cookie and force to balance or use the dispatcher. |
| 1417 | */ |
| 1418 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1419 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1420 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1421 | |
| 1422 | if (t->srv) |
| 1423 | t->srv->failed_conns++; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 1424 | t->be->redispatches++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1425 | |
| 1426 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 1427 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1428 | http_flush_cookie_flags(&t->txn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | /* This function performs the "redispatch" part of a connection attempt. It |
| 1434 | * will assign a server if required, queue the connection if required, and |
| 1435 | * handle errors that might arise at this level. It can change the server |
| 1436 | * state. It will return 1 if it encounters an error, switches the server |
| 1437 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 1438 | * that the connection is ready to use. |
| 1439 | */ |
| 1440 | |
| 1441 | int srv_redispatch_connect(struct session *t) |
| 1442 | { |
| 1443 | int conn_err; |
| 1444 | |
| 1445 | /* We know that we don't have any connection pending, so we will |
| 1446 | * try to get a new one, and wait in this state if it's queued |
| 1447 | */ |
| 1448 | conn_err = assign_server_and_queue(t); |
| 1449 | switch (conn_err) { |
| 1450 | case SRV_STATUS_OK: |
| 1451 | break; |
| 1452 | |
| 1453 | case SRV_STATUS_NOSRV: |
| 1454 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1455 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1456 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1457 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1458 | if (t->srv) |
| 1459 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1460 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1461 | |
| 1462 | return 1; |
| 1463 | |
| 1464 | case SRV_STATUS_QUEUED: |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 1465 | /* note: we use the connect expiration date for the queue. */ |
| 1466 | if (!tv_add_ifset(&t->req->cex, &now, &t->be->timeout.queue)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1467 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1468 | t->srv_state = SV_STIDLE; |
| 1469 | /* do nothing else and do not wake any other session up */ |
| 1470 | return 1; |
| 1471 | |
| 1472 | case SRV_STATUS_FULL: |
| 1473 | case SRV_STATUS_INTERNAL: |
| 1474 | default: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1475 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1476 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1477 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1478 | if (t->srv) |
| 1479 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1480 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1481 | |
| 1482 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1483 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 1484 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1485 | return 1; |
| 1486 | } |
| 1487 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 1488 | * means that the connection has not been queued. |
| 1489 | */ |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1493 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 1494 | 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] | 1495 | return px->down_time; |
| 1496 | |
| 1497 | return now.tv_sec - px->last_change + px->down_time; |
| 1498 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1499 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1500 | /* This function parses a "balance" statement in a backend section describing |
| 1501 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 1502 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 1503 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 1504 | * written. The function must be called with <args> pointing to the first word |
| 1505 | * after "balance". |
| 1506 | */ |
| 1507 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 1508 | { |
| 1509 | if (!*(args[0])) { |
| 1510 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1511 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1512 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1517 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1518 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1519 | } |
| 1520 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1521 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1522 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1523 | } |
| 1524 | else if (!strcmp(args[0], "uri")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1525 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1526 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1527 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1528 | else if (!strcmp(args[0], "url_param")) { |
| 1529 | if (!*args[1]) { |
| 1530 | snprintf(err, errlen, "'balance url_param' requires an URL parameter name."); |
| 1531 | return -1; |
| 1532 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1533 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1534 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1535 | if (curproxy->url_param_name) |
| 1536 | free(curproxy->url_param_name); |
| 1537 | curproxy->url_param_name = strdup(args[1]); |
| 1538 | curproxy->url_param_len = strlen(args[1]); |
| 1539 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1540 | else { |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1541 | snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source', 'uri' and 'url_param' options."); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1542 | return -1; |
| 1543 | } |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1547 | |
| 1548 | /************************************************************************/ |
| 1549 | /* All supported keywords must be declared here. */ |
| 1550 | /************************************************************************/ |
| 1551 | |
| 1552 | /* set test->i to the number of enabled servers on the proxy */ |
| 1553 | static int |
| 1554 | acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1555 | struct acl_expr *expr, struct acl_test *test) |
| 1556 | { |
| 1557 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1558 | if (expr->arg_len) { |
| 1559 | /* another proxy was designated, we must look for it */ |
| 1560 | for (px = proxy; px; px = px->next) |
| 1561 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1562 | break; |
| 1563 | } |
| 1564 | if (!px) |
| 1565 | return 0; |
| 1566 | |
| 1567 | if (px->srv_act) |
| 1568 | test->i = px->srv_act; |
| 1569 | else if (px->lbprm.fbck) |
| 1570 | test->i = 1; |
| 1571 | else |
| 1572 | test->i = px->srv_bck; |
| 1573 | |
| 1574 | return 1; |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1579 | static struct acl_kw_list acl_kws = {{ },{ |
| 1580 | { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int }, |
| 1581 | { NULL, NULL, NULL, NULL }, |
| 1582 | }}; |
| 1583 | |
| 1584 | |
| 1585 | __attribute__((constructor)) |
| 1586 | static void __backend_init(void) |
| 1587 | { |
| 1588 | acl_register_keywords(&acl_kws); |
| 1589 | } |
| 1590 | |
| 1591 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1592 | /* |
| 1593 | * Local variables: |
| 1594 | * c-indent-level: 8 |
| 1595 | * c-basic-offset: 8 |
| 1596 | * End: |
| 1597 | */ |