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