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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
| 24 | #include <types/buffers.h> |
| 25 | #include <types/global.h> |
| 26 | #include <types/polling.h> |
| 27 | #include <types/proxy.h> |
| 28 | #include <types/server.h> |
| 29 | #include <types/session.h> |
| 30 | |
| 31 | #include <proto/backend.h> |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 32 | #include <proto/client.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/fd.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 34 | #include <proto/httperr.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | #include <proto/log.h> |
| 36 | #include <proto/proto_http.h> |
| 37 | #include <proto/queue.h> |
| 38 | #include <proto/stream_sock.h> |
| 39 | #include <proto/task.h> |
| 40 | |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 41 | #ifdef CONFIG_HAP_CTTPROXY |
| 42 | #include <import/ip_tproxy.h> |
| 43 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 45 | #ifdef CONFIG_HAP_TCPSPLICE |
| 46 | #include <libtcpsplice.h> |
| 47 | #endif |
| 48 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | /* |
| 50 | * This function recounts the number of usable active and backup servers for |
| 51 | * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck. |
| 52 | * This function also recomputes the total active and backup weights. |
| 53 | */ |
| 54 | void recount_servers(struct proxy *px) |
| 55 | { |
| 56 | struct server *srv; |
| 57 | |
| 58 | px->srv_act = 0; px->srv_bck = px->tot_wact = px->tot_wbck = 0; |
| 59 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
| 60 | if (srv->state & SRV_RUNNING) { |
| 61 | if (srv->state & SRV_BACKUP) { |
| 62 | px->srv_bck++; |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 63 | px->tot_wbck += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | } else { |
| 65 | px->srv_act++; |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 66 | px->tot_wact += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* This function recomputes the server map for proxy px. It |
| 73 | * relies on px->tot_wact and px->tot_wbck, so it must be |
| 74 | * called after recount_servers(). It also expects px->srv_map |
| 75 | * to be initialized to the largest value needed. |
| 76 | */ |
| 77 | void recalc_server_map(struct proxy *px) |
| 78 | { |
| 79 | int o, tot, flag; |
| 80 | struct server *cur, *best; |
| 81 | |
| 82 | if (px->srv_act) { |
| 83 | flag = SRV_RUNNING; |
| 84 | tot = px->tot_wact; |
| 85 | } else if (px->srv_bck) { |
| 86 | flag = SRV_RUNNING | SRV_BACKUP; |
| 87 | if (px->options & PR_O_USE_ALL_BK) |
| 88 | tot = px->tot_wbck; |
| 89 | else |
| 90 | tot = 1; /* the first server is enough */ |
| 91 | } else { |
| 92 | px->srv_map_sz = 0; |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 93 | px->map_state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | /* this algorithm gives priority to the first server, which means that |
| 98 | * it will respect the declaration order for equivalent weights, and |
| 99 | * that whatever the weights, the first server called will always be |
| 100 | * the first declard. This is an important asumption for the backup |
| 101 | * case, where we want the first server only. |
| 102 | */ |
| 103 | for (cur = px->srv; cur; cur = cur->next) |
| 104 | cur->wscore = 0; |
| 105 | |
| 106 | for (o = 0; o < tot; o++) { |
| 107 | int max = 0; |
| 108 | best = NULL; |
| 109 | for (cur = px->srv; cur; cur = cur->next) { |
| 110 | if ((cur->state & (SRV_RUNNING | SRV_BACKUP)) == flag) { |
| 111 | int v; |
| 112 | |
| 113 | /* If we are forced to return only one server, we don't want to |
| 114 | * go further, because we would return the wrong one due to |
| 115 | * divide overflow. |
| 116 | */ |
| 117 | if (tot == 1) { |
| 118 | best = cur; |
| 119 | break; |
| 120 | } |
| 121 | |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 122 | cur->wscore += cur->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | v = (cur->wscore + tot) / tot; /* result between 0 and 3 */ |
| 124 | if (best == NULL || v > max) { |
| 125 | max = v; |
| 126 | best = cur; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | px->srv_map[o] = best; |
| 131 | best->wscore -= tot; |
| 132 | } |
| 133 | px->srv_map_sz = tot; |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 134 | px->map_state &= ~PR_MAP_RECALC; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | /* |
| 139 | * This function marks the session as 'assigned' in direct or dispatch modes, |
| 140 | * or tries to assign one in balance mode, according to the algorithm. It does |
| 141 | * nothing if the session had already been assigned a server. |
| 142 | * |
| 143 | * It may return : |
| 144 | * SRV_STATUS_OK if everything is OK. s->srv will be valid. |
| 145 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 146 | * SRV_STATUS_FULL if all servers are saturated. s->srv = NULL. |
| 147 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 148 | * |
| 149 | * Upon successful return, the session flag SN_ASSIGNED to indicate that it does |
| 150 | * not need to be called anymore. This usually means that s->srv can be trusted |
| 151 | * in balance and direct modes. This flag is not cleared, so it's to the caller |
| 152 | * to clear it if required (eg: redispatch). |
| 153 | * |
| 154 | */ |
| 155 | |
| 156 | int assign_server(struct session *s) |
| 157 | { |
| 158 | #ifdef DEBUG_FULL |
| 159 | fprintf(stderr,"assign_server : s=%p\n",s); |
| 160 | #endif |
| 161 | |
| 162 | if (s->pend_pos) |
| 163 | return SRV_STATUS_INTERNAL; |
| 164 | |
| 165 | if (!(s->flags & SN_ASSIGNED)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 166 | if (s->be->options & PR_O_BALANCE) { |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 167 | int len; |
| 168 | |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 169 | if (s->flags & SN_DIRECT) { |
| 170 | s->flags |= SN_ASSIGNED; |
| 171 | return SRV_STATUS_OK; |
| 172 | } |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 173 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 174 | if (!s->be->srv_act && !s->be->srv_bck) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | return SRV_STATUS_NOSRV; |
| 176 | |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 177 | switch (s->be->options & PR_O_BALANCE) { |
| 178 | case PR_O_BALANCE_RR: |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 179 | s->srv = get_server_rr_with_conns(s->be); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 180 | if (!s->srv) |
| 181 | return SRV_STATUS_FULL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 182 | break; |
| 183 | case PR_O_BALANCE_SH: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 184 | if (s->cli_addr.ss_family == AF_INET) |
| 185 | len = 4; |
| 186 | else if (s->cli_addr.ss_family == AF_INET6) |
| 187 | len = 16; |
| 188 | else /* unknown IP family */ |
| 189 | return SRV_STATUS_INTERNAL; |
| 190 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 191 | s->srv = get_server_sh(s->be, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 192 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 193 | len); |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 194 | break; |
| 195 | case PR_O_BALANCE_UH: |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 196 | /* URI hashing */ |
| 197 | s->srv = get_server_uh(s->be, |
| 198 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 199 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 200 | break; |
| 201 | default: |
| 202 | /* unknown balancing algorithm */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 203 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 204 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 205 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 206 | else if (!*(int *)&s->be->dispatch_addr.sin_addr && |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 207 | !(s->fe->options & PR_O_TRANSP)) { |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 208 | return SRV_STATUS_NOSRV; |
Willy Tarreau | 5d65bbb | 2007-01-21 12:47:26 +0100 | [diff] [blame] | 209 | } |
| 210 | s->flags |= SN_ASSIGNED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 211 | } |
| 212 | return SRV_STATUS_OK; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* |
| 217 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 218 | * The address is taken from the currently assigned server, or from the |
| 219 | * dispatch or transparent address. |
| 220 | * |
| 221 | * It may return : |
| 222 | * SRV_STATUS_OK if everything is OK. |
| 223 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 224 | * |
| 225 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 226 | * not cleared, so it's to the caller to clear it if required. |
| 227 | * |
| 228 | */ |
| 229 | int assign_server_address(struct session *s) |
| 230 | { |
| 231 | #ifdef DEBUG_FULL |
| 232 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 233 | #endif |
| 234 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 235 | if ((s->flags & SN_DIRECT) || (s->be->options & PR_O_BALANCE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 236 | /* A server is necessarily known for this session */ |
| 237 | if (!(s->flags & SN_ASSIGNED)) |
| 238 | return SRV_STATUS_INTERNAL; |
| 239 | |
| 240 | s->srv_addr = s->srv->addr; |
| 241 | |
| 242 | /* if this server remaps proxied ports, we'll use |
| 243 | * the port the client connected to with an offset. */ |
| 244 | if (s->srv->state & SRV_MAPPORTS) { |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 245 | if (!(s->fe->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET)) |
| 246 | get_frt_addr(s); |
| 247 | if (s->frt_addr.ss_family == AF_INET) { |
| 248 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 249 | ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port)); |
| 250 | } else { |
| 251 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 252 | ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port)); |
| 253 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 256 | else if (*(int *)&s->be->dispatch_addr.sin_addr) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 257 | /* connect to the defined dispatch addr */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 258 | s->srv_addr = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 259 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 260 | else if (s->fe->options & PR_O_TRANSP) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 261 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
| 262 | socklen_t salen = sizeof(s->srv_addr); |
| 263 | |
| 264 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 265 | qfprintf(stderr, "Cannot get original server address.\n"); |
| 266 | return SRV_STATUS_INTERNAL; |
| 267 | } |
| 268 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 269 | else { |
| 270 | /* no server and no LB algorithm ! */ |
| 271 | return SRV_STATUS_INTERNAL; |
| 272 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | |
| 274 | s->flags |= SN_ADDR_SET; |
| 275 | return SRV_STATUS_OK; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* This function assigns a server to session <s> if required, and can add the |
| 280 | * connection to either the assigned server's queue or to the proxy's queue. |
| 281 | * |
| 282 | * Returns : |
| 283 | * |
| 284 | * SRV_STATUS_OK if everything is OK. |
| 285 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 286 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 287 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
| 288 | * connection could not be queued. |
| 289 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 290 | * |
| 291 | */ |
| 292 | int assign_server_and_queue(struct session *s) |
| 293 | { |
| 294 | struct pendconn *p; |
| 295 | int err; |
| 296 | |
| 297 | if (s->pend_pos) |
| 298 | return SRV_STATUS_INTERNAL; |
| 299 | |
| 300 | if (s->flags & SN_ASSIGNED) { |
Elijah Epifanov | acafc5f | 2007-10-25 20:15:38 +0200 | [diff] [blame] | 301 | if (s->srv && s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) { |
| 302 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 303 | s->srv = NULL; |
| 304 | http_flush_cookie_flags(&s->txn); |
| 305 | } else { |
| 306 | /* a server does not need to be assigned, perhaps because we're in |
| 307 | * direct mode, or in dispatch or transparent modes where the server |
| 308 | * is not needed. |
| 309 | */ |
| 310 | if (s->srv && |
| 311 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 312 | p = pendconn_add(s); |
| 313 | if (p) |
| 314 | return SRV_STATUS_QUEUED; |
| 315 | else |
| 316 | return SRV_STATUS_FULL; |
| 317 | } |
| 318 | return SRV_STATUS_OK; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* a server needs to be assigned */ |
| 323 | err = assign_server(s); |
| 324 | switch (err) { |
| 325 | case SRV_STATUS_OK: |
| 326 | /* in balance mode, we might have servers with connection limits */ |
| 327 | if (s->srv && |
| 328 | s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) { |
| 329 | p = pendconn_add(s); |
| 330 | if (p) |
| 331 | return SRV_STATUS_QUEUED; |
| 332 | else |
| 333 | return SRV_STATUS_FULL; |
| 334 | } |
| 335 | return SRV_STATUS_OK; |
| 336 | |
| 337 | case SRV_STATUS_FULL: |
| 338 | /* queue this session into the proxy's queue */ |
| 339 | p = pendconn_add(s); |
| 340 | if (p) |
| 341 | return SRV_STATUS_QUEUED; |
| 342 | else |
| 343 | return SRV_STATUS_FULL; |
| 344 | |
| 345 | case SRV_STATUS_NOSRV: |
| 346 | case SRV_STATUS_INTERNAL: |
| 347 | return err; |
| 348 | default: |
| 349 | return SRV_STATUS_INTERNAL; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /* |
| 355 | * This function initiates a connection to the server assigned to this session |
| 356 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 357 | * It can return one of : |
| 358 | * - SN_ERR_NONE if everything's OK |
| 359 | * - SN_ERR_SRVTO if there are no more servers |
| 360 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 361 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 362 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 363 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 364 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 365 | */ |
| 366 | int connect_server(struct session *s) |
| 367 | { |
| 368 | int fd, err; |
| 369 | |
| 370 | if (!(s->flags & SN_ADDR_SET)) { |
| 371 | err = assign_server_address(s); |
| 372 | if (err != SRV_STATUS_OK) |
| 373 | return SN_ERR_INTERNAL; |
| 374 | } |
| 375 | |
| 376 | if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 377 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 378 | |
| 379 | if (errno == ENFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 380 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | "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] | 382 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 383 | else if (errno == EMFILE) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 384 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 385 | "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] | 386 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 387 | else if (errno == ENOBUFS || errno == ENOMEM) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 388 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | "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] | 390 | s->be->id, maxfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 391 | /* this is a resource error */ |
| 392 | return SN_ERR_RESOURCE; |
| 393 | } |
| 394 | |
| 395 | if (fd >= global.maxsock) { |
| 396 | /* do not log anything there, it's a normal condition when this option |
| 397 | * is used to serialize connections to a server ! |
| 398 | */ |
| 399 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 400 | close(fd); |
| 401 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 402 | } |
| 403 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 404 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 405 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 406 | /* TCP splicing supported by both FE and BE */ |
| 407 | tcp_splice_initfd(s->cli_fd, fd); |
| 408 | } |
| 409 | #endif |
| 410 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 412 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 413 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 414 | close(fd); |
| 415 | return SN_ERR_INTERNAL; |
| 416 | } |
| 417 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 418 | if (s->be->options & PR_O_TCP_SRV_KA) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 419 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 420 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 421 | if (s->be->options & PR_O_TCP_NOLING) |
| 422 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 423 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 424 | /* allow specific binding : |
| 425 | * - server-specific at first |
| 426 | * - proxy-specific next |
| 427 | */ |
| 428 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 429 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 430 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 431 | 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] | 432 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 433 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 434 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 436 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 437 | return SN_ERR_RESOURCE; |
| 438 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 439 | #ifdef CONFIG_HAP_CTTPROXY |
| 440 | if (s->srv->state & SRV_TPROXY_MASK) { |
| 441 | struct in_tproxy itp1, itp2; |
| 442 | memset(&itp1, 0, sizeof(itp1)); |
| 443 | |
| 444 | itp1.op = TPROXY_ASSIGN; |
| 445 | switch (s->srv->state & SRV_TPROXY_MASK) { |
| 446 | case SRV_TPROXY_ADDR: |
| 447 | itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr; |
| 448 | itp1.v.addr.fport = s->srv->tproxy_addr.sin_port; |
| 449 | break; |
| 450 | case SRV_TPROXY_CLI: |
| 451 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 452 | /* fall through */ |
| 453 | case SRV_TPROXY_CIP: |
| 454 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 455 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | /* set connect flag on socket */ |
| 460 | itp2.op = TPROXY_FLAGS; |
| 461 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 462 | |
| 463 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 464 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 465 | 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] | 466 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 467 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 468 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 469 | "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] | 470 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 471 | return SN_ERR_RESOURCE; |
| 472 | } |
| 473 | } |
| 474 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 475 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 476 | else if (s->be->options & PR_O_BIND_SRC) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 477 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 478 | if (bind(fd, (struct sockaddr *)&s->be->source_addr, sizeof(s->be->source_addr)) == -1) { |
| 479 | 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] | 480 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 481 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 482 | "Cannot bind to source address before connect() for server %s/%s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 483 | s->be->id, s->srv->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 484 | return SN_ERR_RESOURCE; |
| 485 | } |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 486 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 487 | if (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 488 | struct in_tproxy itp1, itp2; |
| 489 | memset(&itp1, 0, sizeof(itp1)); |
| 490 | |
| 491 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 492 | switch (s->be->options & PR_O_TPXY_MASK) { |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 493 | case PR_O_TPXY_ADDR: |
| 494 | itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr; |
| 495 | itp1.v.addr.fport = s->srv->tproxy_addr.sin_port; |
| 496 | break; |
| 497 | case PR_O_TPXY_CLI: |
| 498 | itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port; |
| 499 | /* fall through */ |
| 500 | case PR_O_TPXY_CIP: |
| 501 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 502 | itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | /* set connect flag on socket */ |
| 507 | itp2.op = TPROXY_FLAGS; |
| 508 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 509 | |
| 510 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 || |
| 511 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) { |
| 512 | 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] | 513 | s->be->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 514 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 515 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 516 | "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] | 517 | s->be->id, s->srv->id); |
Willy Tarreau | 77074d5 | 2006-11-12 23:57:19 +0100 | [diff] [blame] | 518 | return SN_ERR_RESOURCE; |
| 519 | } |
| 520 | } |
| 521 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 525 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 526 | |
| 527 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 528 | char *msg; |
| 529 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 530 | msg = "no free ports"; |
| 531 | else |
| 532 | msg = "local address already in use"; |
| 533 | |
| 534 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
| 535 | close(fd); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 536 | send_log(s->be, LOG_EMERG, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 537 | "Connect() failed for server %s/%s: %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 538 | s->be->id, s->srv->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 539 | return SN_ERR_RESOURCE; |
| 540 | } else if (errno == ETIMEDOUT) { |
| 541 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 542 | close(fd); |
| 543 | return SN_ERR_SRVTO; |
| 544 | } else { |
| 545 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 546 | //qfprintf(stderr,"Connect(): %d", errno); |
| 547 | close(fd); |
| 548 | return SN_ERR_SRVCL; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | fdtab[fd].owner = s->task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 554 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 555 | fdtab[fd].cb[DIR_RD].b = s->rep; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 556 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 557 | fdtab[fd].cb[DIR_WR].b = s->req; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 558 | |
| 559 | fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr; |
| 560 | fdtab[fd].peerlen = sizeof(s->srv_addr); |
| 561 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 562 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 563 | |
| 564 | fd_insert(fd); |
| 565 | if (s->srv) { |
| 566 | s->srv->cur_sess++; |
| 567 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 568 | s->srv->cur_sess_max = s->srv->cur_sess; |
| 569 | } |
| 570 | |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 571 | if (!tv_add_ifset(&s->req->cex, &now, &s->be->contimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 572 | tv_eternity(&s->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | return SN_ERR_NONE; /* connection is OK */ |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /* |
| 578 | * This function checks the retry count during the connect() job. |
| 579 | * It updates the session's srv_state and retries, so that the caller knows |
| 580 | * what it has to do. It uses the last connection error to set the log when |
| 581 | * it expires. It returns 1 when it has expired, and 0 otherwise. |
| 582 | */ |
| 583 | int srv_count_retry_down(struct session *t, int conn_err) |
| 584 | { |
| 585 | /* we are in front of a retryable error */ |
| 586 | t->conn_retries--; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 587 | if (t->srv) |
| 588 | t->srv->retries++; |
| 589 | t->be->retries++; |
| 590 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 591 | if (t->conn_retries < 0) { |
| 592 | /* if not retryable anymore, let's abort */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 593 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 594 | srv_close_with_err(t, conn_err, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 595 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 596 | if (t->srv) |
| 597 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 598 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 599 | |
| 600 | /* We used to have a free connection slot. Since we'll never use it, |
| 601 | * we have to inform the server that it may be used by another session. |
| 602 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 603 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 604 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 605 | return 1; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /* |
| 612 | * This function performs the retryable part of the connect() job. |
| 613 | * It updates the session's srv_state and retries, so that the caller knows |
| 614 | * what it has to do. It returns 1 when it breaks out of the loop, or 0 if |
| 615 | * it needs to redispatch. |
| 616 | */ |
| 617 | int srv_retryable_connect(struct session *t) |
| 618 | { |
| 619 | int conn_err; |
| 620 | |
| 621 | /* This loop ensures that we stop before the last retry in case of a |
| 622 | * redispatchable server. |
| 623 | */ |
| 624 | do { |
| 625 | /* initiate a connection to the server */ |
| 626 | conn_err = connect_server(t); |
| 627 | switch (conn_err) { |
| 628 | |
| 629 | case SN_ERR_NONE: |
| 630 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 631 | t->srv_state = SV_STCONN; |
| 632 | return 1; |
| 633 | |
| 634 | case SN_ERR_INTERNAL: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 635 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 637 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | if (t->srv) |
| 639 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 640 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 642 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 643 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | /* ensure that we have enough retries left */ |
| 647 | if (srv_count_retry_down(t, conn_err)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | return 1; |
| 649 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 650 | } 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] | 651 | |
| 652 | /* We're on our last chance, and the REDISP option was specified. |
| 653 | * We will ignore cookie and force to balance or use the dispatcher. |
| 654 | */ |
| 655 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 656 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 657 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 658 | |
| 659 | if (t->srv) |
| 660 | t->srv->failed_conns++; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 661 | t->be->redispatches++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
| 663 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 664 | 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] | 665 | http_flush_cookie_flags(&t->txn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | /* This function performs the "redispatch" part of a connection attempt. It |
| 671 | * will assign a server if required, queue the connection if required, and |
| 672 | * handle errors that might arise at this level. It can change the server |
| 673 | * state. It will return 1 if it encounters an error, switches the server |
| 674 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 675 | * that the connection is ready to use. |
| 676 | */ |
| 677 | |
| 678 | int srv_redispatch_connect(struct session *t) |
| 679 | { |
| 680 | int conn_err; |
| 681 | |
| 682 | /* We know that we don't have any connection pending, so we will |
| 683 | * try to get a new one, and wait in this state if it's queued |
| 684 | */ |
| 685 | conn_err = assign_server_and_queue(t); |
| 686 | switch (conn_err) { |
| 687 | case SRV_STATUS_OK: |
| 688 | break; |
| 689 | |
| 690 | case SRV_STATUS_NOSRV: |
| 691 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 692 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 693 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 694 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 695 | if (t->srv) |
| 696 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 697 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 698 | |
| 699 | return 1; |
| 700 | |
| 701 | case SRV_STATUS_QUEUED: |
| 702 | /* FIXME-20060503 : we should use the queue timeout instead */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 703 | if (!tv_add_ifset(&t->req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 704 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 705 | t->srv_state = SV_STIDLE; |
| 706 | /* do nothing else and do not wake any other session up */ |
| 707 | return 1; |
| 708 | |
| 709 | case SRV_STATUS_FULL: |
| 710 | case SRV_STATUS_INTERNAL: |
| 711 | default: |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 712 | tv_eternity(&t->req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 713 | srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 714 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 715 | if (t->srv) |
| 716 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 717 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 718 | |
| 719 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 720 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 721 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 722 | return 1; |
| 723 | } |
| 724 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 725 | * means that the connection has not been queued. |
| 726 | */ |
| 727 | return 0; |
| 728 | } |
| 729 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 730 | int be_downtime(struct proxy *px) { |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 731 | if ((px->srv_act || px->srv_bck) && px->last_change < now.tv_sec) // ignore negative time |
| 732 | return px->down_time; |
| 733 | |
| 734 | return now.tv_sec - px->last_change + px->down_time; |
| 735 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 736 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame^] | 737 | /* This function parses a "balance" statement in a backend section describing |
| 738 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 739 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 740 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 741 | * written. The function must be called with <args> pointing to the first word |
| 742 | * after "balance". |
| 743 | */ |
| 744 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 745 | { |
| 746 | if (!*(args[0])) { |
| 747 | /* if no option is set, use round-robin by default */ |
| 748 | curproxy->options &= ~PR_O_BALANCE; |
| 749 | curproxy->options |= PR_O_BALANCE_RR; |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | if (!strcmp(args[0], "roundrobin")) { |
| 754 | curproxy->options &= ~PR_O_BALANCE; |
| 755 | curproxy->options |= PR_O_BALANCE_RR; |
| 756 | } |
| 757 | else if (!strcmp(args[0], "source")) { |
| 758 | curproxy->options &= ~PR_O_BALANCE; |
| 759 | curproxy->options |= PR_O_BALANCE_SH; |
| 760 | } |
| 761 | else if (!strcmp(args[0], "uri")) { |
| 762 | curproxy->options &= ~PR_O_BALANCE; |
| 763 | curproxy->options |= PR_O_BALANCE_UH; |
| 764 | } |
| 765 | else { |
| 766 | snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source' and 'uri' options."); |
| 767 | return -1; |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | /* |
| 773 | * Local variables: |
| 774 | * c-indent-level: 8 |
| 775 | * c-basic-offset: 8 |
| 776 | * End: |
| 777 | */ |