blob: 1601c408c7715897b385157b2bca3d1bdbbcde6b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
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 Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020021#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
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 Tarreau80587432006-12-24 17:47:20 +010033#include <proto/httperr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#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 Tarreau77074d52006-11-12 23:57:19 +010040#ifdef CONFIG_HAP_CTTPROXY
41#include <import/ip_tproxy.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau6d1a9882007-01-07 02:03:04 +010044#ifdef CONFIG_HAP_TCPSPLICE
45#include <libtcpsplice.h>
46#endif
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048/*
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 */
53void 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 */
76void 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
153int 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 Tarreau830ff452006-12-17 19:31:23 +0100163 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 Tarreaubaaee002006-06-26 02:48:02 +0200165 return SRV_STATUS_NOSRV;
166
Willy Tarreau830ff452006-12-17 19:31:23 +0100167 if (s->be->beprm->options & PR_O_BALANCE_RR) {
168 s->srv = get_server_rr_with_conns(s->be->beprm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 if (!s->srv)
170 return SRV_STATUS_FULL;
171 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100172 else if (s->be->beprm->options & PR_O_BALANCE_SH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 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 Tarreau830ff452006-12-17 19:31:23 +0100182 s->srv = get_server_sh(s->be->beprm,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
184 len);
185 }
186 else /* unknown balancing algorithm */
187 return SRV_STATUS_INTERNAL;
188 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100189 else if (*(int *)&s->be->beprm->dispatch_addr.sin_addr || s->fe->options & PR_O_TRANSP)
190 s->flags |= SN_ASSIGNED;
191 else
192 return SRV_STATUS_NOSRV;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 }
194 return SRV_STATUS_OK;
195}
196
197
198/*
199 * This function assigns a server address to a session, and sets SN_ADDR_SET.
200 * The address is taken from the currently assigned server, or from the
201 * dispatch or transparent address.
202 *
203 * It may return :
204 * SRV_STATUS_OK if everything is OK.
205 * SRV_STATUS_INTERNAL for other unrecoverable errors.
206 *
207 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
208 * not cleared, so it's to the caller to clear it if required.
209 *
210 */
211int assign_server_address(struct session *s)
212{
213#ifdef DEBUG_FULL
214 fprintf(stderr,"assign_server_address : s=%p\n",s);
215#endif
216
Willy Tarreau830ff452006-12-17 19:31:23 +0100217 if (s->flags & SN_DIRECT || s->be->beprm->options & PR_O_BALANCE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 /* A server is necessarily known for this session */
219 if (!(s->flags & SN_ASSIGNED))
220 return SRV_STATUS_INTERNAL;
221
222 s->srv_addr = s->srv->addr;
223
224 /* if this server remaps proxied ports, we'll use
225 * the port the client connected to with an offset. */
226 if (s->srv->state & SRV_MAPPORTS) {
227 struct sockaddr_in sockname;
228 socklen_t namelen = sizeof(sockname);
229
Willy Tarreau73de9892006-11-30 11:40:23 +0100230 if (!(s->fe->options & PR_O_TRANSP) ||
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1)
232 getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen);
233 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port));
234 }
235 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100236 else if (*(int *)&s->be->beprm->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 /* connect to the defined dispatch addr */
Willy Tarreau830ff452006-12-17 19:31:23 +0100238 s->srv_addr = s->be->beprm->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 }
Willy Tarreau73de9892006-11-30 11:40:23 +0100240 else if (s->fe->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 /* in transparent mode, use the original dest addr if no dispatch specified */
242 socklen_t salen = sizeof(s->srv_addr);
243
244 if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) {
245 qfprintf(stderr, "Cannot get original server address.\n");
246 return SRV_STATUS_INTERNAL;
247 }
248 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100249 else {
250 /* no server and no LB algorithm ! */
251 return SRV_STATUS_INTERNAL;
252 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
254 s->flags |= SN_ADDR_SET;
255 return SRV_STATUS_OK;
256}
257
258
259/* This function assigns a server to session <s> if required, and can add the
260 * connection to either the assigned server's queue or to the proxy's queue.
261 *
262 * Returns :
263 *
264 * SRV_STATUS_OK if everything is OK.
265 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
266 * SRV_STATUS_QUEUED if the connection has been queued.
267 * SRV_STATUS_FULL if the server(s) is/are saturated and the
268 * connection could not be queued.
269 * SRV_STATUS_INTERNAL for other unrecoverable errors.
270 *
271 */
272int assign_server_and_queue(struct session *s)
273{
274 struct pendconn *p;
275 int err;
276
277 if (s->pend_pos)
278 return SRV_STATUS_INTERNAL;
279
280 if (s->flags & SN_ASSIGNED) {
281 /* a server does not need to be assigned, perhaps because we're in
282 * direct mode, or in dispatch or transparent modes where the server
283 * is not needed.
284 */
285 if (s->srv &&
286 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
287 p = pendconn_add(s);
288 if (p)
289 return SRV_STATUS_QUEUED;
290 else
291 return SRV_STATUS_FULL;
292 }
293 return SRV_STATUS_OK;
294 }
295
296 /* a server needs to be assigned */
297 err = assign_server(s);
298 switch (err) {
299 case SRV_STATUS_OK:
300 /* in balance mode, we might have servers with connection limits */
301 if (s->srv &&
302 s->srv->maxconn && s->srv->cur_sess >= srv_dynamic_maxconn(s->srv)) {
303 p = pendconn_add(s);
304 if (p)
305 return SRV_STATUS_QUEUED;
306 else
307 return SRV_STATUS_FULL;
308 }
309 return SRV_STATUS_OK;
310
311 case SRV_STATUS_FULL:
312 /* queue this session into the proxy's queue */
313 p = pendconn_add(s);
314 if (p)
315 return SRV_STATUS_QUEUED;
316 else
317 return SRV_STATUS_FULL;
318
319 case SRV_STATUS_NOSRV:
320 case SRV_STATUS_INTERNAL:
321 return err;
322 default:
323 return SRV_STATUS_INTERNAL;
324 }
325}
326
327
328/*
329 * This function initiates a connection to the server assigned to this session
330 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
331 * It can return one of :
332 * - SN_ERR_NONE if everything's OK
333 * - SN_ERR_SRVTO if there are no more servers
334 * - SN_ERR_SRVCL if the connection was refused by the server
335 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
336 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
337 * - SN_ERR_INTERNAL for any other purely internal errors
338 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
339 */
340int connect_server(struct session *s)
341{
342 int fd, err;
343
344 if (!(s->flags & SN_ADDR_SET)) {
345 err = assign_server_address(s);
346 if (err != SRV_STATUS_OK)
347 return SN_ERR_INTERNAL;
348 }
349
350 if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
351 qfprintf(stderr, "Cannot get a server socket.\n");
352
353 if (errno == ENFILE)
Willy Tarreau830ff452006-12-17 19:31:23 +0100354 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100356 s->be->beprm->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 else if (errno == EMFILE)
Willy Tarreau830ff452006-12-17 19:31:23 +0100358 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100360 s->be->beprm->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 else if (errno == ENOBUFS || errno == ENOMEM)
Willy Tarreau830ff452006-12-17 19:31:23 +0100362 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100364 s->be->beprm->id, maxfd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 /* this is a resource error */
366 return SN_ERR_RESOURCE;
367 }
368
369 if (fd >= global.maxsock) {
370 /* do not log anything there, it's a normal condition when this option
371 * is used to serialize connections to a server !
372 */
373 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
374 close(fd);
375 return SN_ERR_PRXCOND; /* it is a configuration limit */
376 }
377
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100378#ifdef CONFIG_HAP_TCPSPLICE
379 if ((s->fe->options & s->be->beprm->options) & PR_O_TCPSPLICE) {
380 /* TCP splicing supported by both FE and BE */
381 tcp_splice_initfd(s->cli_fd, fd);
382 }
383#endif
384
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
386 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
387 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
388 close(fd);
389 return SN_ERR_INTERNAL;
390 }
391
Willy Tarreau830ff452006-12-17 19:31:23 +0100392 if (s->be->beprm->options & PR_O_TCP_SRV_KA)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
394
395 /* allow specific binding :
396 * - server-specific at first
397 * - proxy-specific next
398 */
399 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
400 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
401 if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) {
402 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100403 s->be->beprm->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 close(fd);
Willy Tarreau830ff452006-12-17 19:31:23 +0100405 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100407 s->be->beprm->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408 return SN_ERR_RESOURCE;
409 }
Willy Tarreau77074d52006-11-12 23:57:19 +0100410#ifdef CONFIG_HAP_CTTPROXY
411 if (s->srv->state & SRV_TPROXY_MASK) {
412 struct in_tproxy itp1, itp2;
413 memset(&itp1, 0, sizeof(itp1));
414
415 itp1.op = TPROXY_ASSIGN;
416 switch (s->srv->state & SRV_TPROXY_MASK) {
417 case SRV_TPROXY_ADDR:
418 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
419 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
420 break;
421 case SRV_TPROXY_CLI:
422 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
423 /* fall through */
424 case SRV_TPROXY_CIP:
425 /* FIXME: what can we do if the client connects in IPv6 ? */
426 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
427 break;
428 }
429
430 /* set connect flag on socket */
431 itp2.op = TPROXY_FLAGS;
432 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
433
434 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
435 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
436 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100437 s->be->beprm->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +0100438 close(fd);
Willy Tarreau830ff452006-12-17 19:31:23 +0100439 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +0100440 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100441 s->be->beprm->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +0100442 return SN_ERR_RESOURCE;
443 }
444 }
445#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100447 else if (s->be->beprm->options & PR_O_BIND_SRC) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
Willy Tarreau830ff452006-12-17 19:31:23 +0100449 if (bind(fd, (struct sockaddr *)&s->be->beprm->source_addr, sizeof(s->be->beprm->source_addr)) == -1) {
450 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->be->beprm->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 close(fd);
Willy Tarreau830ff452006-12-17 19:31:23 +0100452 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 "Cannot bind to source address before connect() for server %s/%s.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100454 s->be->beprm->id, s->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 return SN_ERR_RESOURCE;
456 }
Willy Tarreau77074d52006-11-12 23:57:19 +0100457#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau830ff452006-12-17 19:31:23 +0100458 if (s->be->beprm->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +0100459 struct in_tproxy itp1, itp2;
460 memset(&itp1, 0, sizeof(itp1));
461
462 itp1.op = TPROXY_ASSIGN;
Willy Tarreau830ff452006-12-17 19:31:23 +0100463 switch (s->be->beprm->options & PR_O_TPXY_MASK) {
Willy Tarreau77074d52006-11-12 23:57:19 +0100464 case PR_O_TPXY_ADDR:
465 itp1.v.addr.faddr = s->srv->tproxy_addr.sin_addr;
466 itp1.v.addr.fport = s->srv->tproxy_addr.sin_port;
467 break;
468 case PR_O_TPXY_CLI:
469 itp1.v.addr.fport = ((struct sockaddr_in *)&s->cli_addr)->sin_port;
470 /* fall through */
471 case PR_O_TPXY_CIP:
472 /* FIXME: what can we do if the client connects in IPv6 ? */
473 itp1.v.addr.faddr = ((struct sockaddr_in *)&s->cli_addr)->sin_addr;
474 break;
475 }
476
477 /* set connect flag on socket */
478 itp2.op = TPROXY_FLAGS;
479 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
480
481 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1 ||
482 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) == -1) {
483 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100484 s->be->beprm->id);
Willy Tarreau77074d52006-11-12 23:57:19 +0100485 close(fd);
Willy Tarreau830ff452006-12-17 19:31:23 +0100486 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreau77074d52006-11-12 23:57:19 +0100487 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100488 s->be->beprm->id, s->srv->id);
Willy Tarreau77074d52006-11-12 23:57:19 +0100489 return SN_ERR_RESOURCE;
490 }
491 }
492#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493 }
494
495 if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) &&
496 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
497
498 if (errno == EAGAIN || errno == EADDRINUSE) {
499 char *msg;
500 if (errno == EAGAIN) /* no free ports left, try again later */
501 msg = "no free ports";
502 else
503 msg = "local address already in use";
504
505 qfprintf(stderr,"Cannot connect: %s.\n",msg);
506 close(fd);
Willy Tarreau830ff452006-12-17 19:31:23 +0100507 send_log(s->be->beprm, LOG_EMERG,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508 "Connect() failed for server %s/%s: %s.\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100509 s->be->beprm->id, s->srv->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510 return SN_ERR_RESOURCE;
511 } else if (errno == ETIMEDOUT) {
512 //qfprintf(stderr,"Connect(): ETIMEDOUT");
513 close(fd);
514 return SN_ERR_SRVTO;
515 } else {
516 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
517 //qfprintf(stderr,"Connect(): %d", errno);
518 close(fd);
519 return SN_ERR_SRVCL;
520 }
521 }
522
523 fdtab[fd].owner = s->task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreaud7971282006-07-29 18:36:34 +0200525 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
Willy Tarreau54469402006-07-29 16:59:06 +0200526 fdtab[fd].cb[DIR_RD].b = s->rep;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200527 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
Willy Tarreau54469402006-07-29 16:59:06 +0200528 fdtab[fd].cb[DIR_WR].b = s->req;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreau2a429502006-10-15 14:52:29 +0200530 MY_FD_SET(fd, StaticWriteEvent); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531#if defined(DEBUG_FULL) && defined(ENABLE_EPOLL)
532 if (PrevReadEvent) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200533 assert(!(MY_FD_ISSET(fd, PrevReadEvent)));
534 assert(!(MY_FD_ISSET(fd, PrevWriteEvent)));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535 }
536#endif
537
538 fd_insert(fd);
539 if (s->srv) {
540 s->srv->cur_sess++;
541 if (s->srv->cur_sess > s->srv->cur_sess_max)
542 s->srv->cur_sess_max = s->srv->cur_sess;
543 }
544
Willy Tarreau830ff452006-12-17 19:31:23 +0100545 if (s->be->beprm->contimeout)
546 tv_delayfrom(&s->req->cex, &now, s->be->beprm->contimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200548 tv_eternity(&s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549 return SN_ERR_NONE; /* connection is OK */
550}
551
552
553/*
554 * This function checks the retry count during the connect() job.
555 * It updates the session's srv_state and retries, so that the caller knows
556 * what it has to do. It uses the last connection error to set the log when
557 * it expires. It returns 1 when it has expired, and 0 otherwise.
558 */
559int srv_count_retry_down(struct session *t, int conn_err)
560{
561 /* we are in front of a retryable error */
562 t->conn_retries--;
563 if (t->conn_retries < 0) {
564 /* if not retryable anymore, let's abort */
Willy Tarreaud7971282006-07-29 18:36:34 +0200565 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566 srv_close_with_err(t, conn_err, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +0100567 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 if (t->srv)
569 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +0100570 t->be->beprm->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571
572 /* We used to have a free connection slot. Since we'll never use it,
573 * we have to inform the server that it may be used by another session.
574 */
Willy Tarreau830ff452006-12-17 19:31:23 +0100575 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 task_wakeup(&rq, t->srv->queue_mgt);
577 return 1;
578 }
579 return 0;
580}
581
582
583/*
584 * This function performs the retryable part of the connect() job.
585 * It updates the session's srv_state and retries, so that the caller knows
586 * what it has to do. It returns 1 when it breaks out of the loop, or 0 if
587 * it needs to redispatch.
588 */
589int srv_retryable_connect(struct session *t)
590{
591 int conn_err;
592
593 /* This loop ensures that we stop before the last retry in case of a
594 * redispatchable server.
595 */
596 do {
597 /* initiate a connection to the server */
598 conn_err = connect_server(t);
599 switch (conn_err) {
600
601 case SN_ERR_NONE:
602 //fprintf(stderr,"0: c=%d, s=%d\n", c, s);
603 t->srv_state = SV_STCONN;
604 return 1;
605
606 case SN_ERR_INTERNAL:
Willy Tarreaud7971282006-07-29 18:36:34 +0200607 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +0100609 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610 if (t->srv)
611 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +0100612 t->be->beprm->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613 /* release other sessions waiting for this server */
Willy Tarreau830ff452006-12-17 19:31:23 +0100614 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615 task_wakeup(&rq, t->srv->queue_mgt);
616 return 1;
617 }
618 /* ensure that we have enough retries left */
619 if (srv_count_retry_down(t, conn_err)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 return 1;
621 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100622 } while (t->srv == NULL || t->conn_retries > 0 || !(t->be->beprm->options & PR_O_REDISP));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623
624 /* We're on our last chance, and the REDISP option was specified.
625 * We will ignore cookie and force to balance or use the dispatcher.
626 */
627 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +0100628 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 task_wakeup(&rq, t->srv->queue_mgt);
630
631 if (t->srv)
632 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +0100633 t->be->beprm->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634
635 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
636 t->srv = NULL; /* it's left to the dispatcher to choose a server */
637 if ((t->flags & SN_CK_MASK) == SN_CK_VALID) {
638 t->flags &= ~SN_CK_MASK;
639 t->flags |= SN_CK_DOWN;
640 }
641 return 0;
642}
643
644
645/* This function performs the "redispatch" part of a connection attempt. It
646 * will assign a server if required, queue the connection if required, and
647 * handle errors that might arise at this level. It can change the server
648 * state. It will return 1 if it encounters an error, switches the server
649 * state, or has to queue a connection. Otherwise, it will return 0 indicating
650 * that the connection is ready to use.
651 */
652
653int srv_redispatch_connect(struct session *t)
654{
655 int conn_err;
656
657 /* We know that we don't have any connection pending, so we will
658 * try to get a new one, and wait in this state if it's queued
659 */
660 conn_err = assign_server_and_queue(t);
661 switch (conn_err) {
662 case SRV_STATUS_OK:
663 break;
664
665 case SRV_STATUS_NOSRV:
666 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaud7971282006-07-29 18:36:34 +0200667 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +0100669 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 if (t->srv)
671 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +0100672 t->be->beprm->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
674 return 1;
675
676 case SRV_STATUS_QUEUED:
677 /* FIXME-20060503 : we should use the queue timeout instead */
Willy Tarreau830ff452006-12-17 19:31:23 +0100678 if (t->be->beprm->contimeout)
679 tv_delayfrom(&t->req->cex, &now, t->be->beprm->contimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200681 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 t->srv_state = SV_STIDLE;
683 /* do nothing else and do not wake any other session up */
684 return 1;
685
686 case SRV_STATUS_FULL:
687 case SRV_STATUS_INTERNAL:
688 default:
Willy Tarreaud7971282006-07-29 18:36:34 +0200689 tv_eternity(&t->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 srv_close_with_err(t, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau80587432006-12-24 17:47:20 +0100691 500, error_message(t, HTTP_ERR_500));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 if (t->srv)
693 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +0100694 t->be->beprm->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
696 /* release other sessions waiting for this server */
Willy Tarreau830ff452006-12-17 19:31:23 +0100697 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698 task_wakeup(&rq, t->srv->queue_mgt);
699 return 1;
700 }
701 /* if we get here, it's because we got SRV_STATUS_OK, which also
702 * means that the connection has not been queued.
703 */
704 return 0;
705}
706
707
708/*
709 * Local variables:
710 * c-indent-level: 8
711 * c-basic-offset: 8
712 * End:
713 */