blob: bd33c84e0bafbc507bcc1c77193339a1c55beb2b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Proxy variables and functions.
3 *
Willy Tarreaud825eef2007-05-12 22:35:00 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 <fcntl.h>
14#include <unistd.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <sys/stat.h>
18
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/defaults.h>
20#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020021#include <common/config.h>
Willy Tarreau4d2d0982007-05-14 00:39:29 +020022#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <types/global.h>
26#include <types/polling.h>
27
28#include <proto/client.h>
Alexandre Cassen87ea5482007-10-11 20:48:58 +020029#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <proto/fd.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
33
34
35int listeners; /* # of listeners */
36struct proxy *proxy = NULL; /* list of all existing proxies */
37
Willy Tarreau977b8e42006-12-29 14:19:17 +010038/*
39 * This function returns a string containing the type of the proxy in a format
40 * suitable for error messages, from its capabilities.
41 */
Willy Tarreau2b5652f2006-12-31 17:46:05 +010042const char *proxy_type_str(struct proxy *proxy)
Willy Tarreau977b8e42006-12-29 14:19:17 +010043{
Willy Tarreau2b5652f2006-12-31 17:46:05 +010044 int cap = proxy->cap;
Willy Tarreau977b8e42006-12-29 14:19:17 +010045 if ((cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
46 return "listener";
47 else if (cap & PR_CAP_FE)
48 return "frontend";
49 else if (cap & PR_CAP_BE)
50 return "backend";
51 else if (cap & PR_CAP_RS)
52 return "ruleset";
53 else
54 return "proxy";
55}
56
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
58/*
Willy Tarreau2ff76222007-04-09 19:29:56 +020059 * This function creates all proxy sockets. It should be done very early,
60 * typically before privileges are dropped. The sockets will be registered
61 * but not added to any fd_set, in order not to loose them across the fork().
62 * The proxies also start in IDLE state, meaning that it will be
63 * maintain_proxies that will finally complete their loading.
64 *
65 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
66 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 */
68int start_proxies(int verbose)
69{
70 struct proxy *curproxy;
71 struct listener *listener;
72 int err = ERR_NONE;
73 int fd, pxerr;
74
75 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
76 if (curproxy->state != PR_STNEW)
77 continue; /* already initialized */
78
79 pxerr = 0;
80 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
81 if (listener->fd != -1)
82 continue; /* already initialized */
83
84 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
85 if (verbose)
86 Alert("cannot create listening socket for proxy %s. Aborting.\n",
87 curproxy->id);
88 err |= ERR_RETRYABLE;
89 pxerr |= 1;
90 continue;
91 }
92
93 if (fd >= global.maxsock) {
94 Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n",
95 curproxy->id);
96 close(fd);
97 err |= ERR_FATAL;
98 pxerr |= 1;
99 break;
100 }
101
102 if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
103 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
104 (char *) &one, sizeof(one)) == -1)) {
105 Alert("cannot make socket non-blocking for proxy %s. Aborting.\n",
106 curproxy->id);
107 close(fd);
108 err |= ERR_FATAL;
109 pxerr |= 1;
110 break;
111 }
112
113 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
114 Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n",
115 curproxy->id);
116 }
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200117
118 if (curproxy->options & PR_O_TCP_NOLING)
119 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
121#ifdef SO_REUSEPORT
122 /* OpenBSD supports this. As it's present in old libc versions of Linux,
123 * it might return an error that we will silently ignore.
124 */
125 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
126#endif
127 if (bind(fd,
128 (struct sockaddr *)&listener->addr,
129 listener->addr.ss_family == AF_INET6 ?
130 sizeof(struct sockaddr_in6) :
131 sizeof(struct sockaddr_in)) == -1) {
132 if (verbose)
133 Alert("cannot bind socket for proxy %s. Aborting.\n",
134 curproxy->id);
135 close(fd);
136 err |= ERR_RETRYABLE;
137 pxerr |= 1;
138 continue;
139 }
140
141 if (listen(fd, curproxy->maxconn) == -1) {
142 if (verbose)
143 Alert("cannot listen to socket for proxy %s. Aborting.\n",
144 curproxy->id);
145 close(fd);
146 err |= ERR_RETRYABLE;
147 pxerr |= 1;
148 continue;
149 }
150
151 /* the socket is ready */
152 listener->fd = fd;
153
154 /* the function for the accept() event */
Willy Tarreau7a966482007-04-15 10:58:02 +0200155 fd_insert(fd);
Willy Tarreau54469402006-07-29 16:59:06 +0200156 fdtab[fd].cb[DIR_RD].f = &event_accept;
157 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
158 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */
160 fdtab[fd].state = FD_STLISTEN;
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200161 fdtab[fd].ev = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162 listeners++;
163 }
164
165 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200166 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
168 }
169 }
170
171 return err;
172}
173
174
175/*
176 * this function enables proxies when there are enough free sessions,
177 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200178 * select_loop(). It returns the date of next expiration event during stop
179 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200181void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182{
183 struct proxy *p;
184 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185
186 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200187 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188
189 /* if there are enough free sessions, we'll activate proxies */
190 if (actconn < global.maxconn) {
191 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100192 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 if (p->state == PR_STIDLE) {
194 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200195 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 }
197 p->state = PR_STRUN;
198 }
199 }
200 else {
201 if (p->state == PR_STRUN) {
202 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200203 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204 }
205 p->state = PR_STIDLE;
206 }
207 }
208 p = p->next;
209 }
210 }
211 else { /* block all proxies */
212 while (p) {
213 if (p->state == PR_STRUN) {
214 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200215 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216 }
217 p->state = PR_STIDLE;
218 }
219 p = p->next;
220 }
221 }
222
223 if (stopping) {
224 p = proxy;
225 while (p) {
226 if (p->state != PR_STSTOPPED) {
227 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200228 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229 if (t == 0) {
230 Warning("Proxy %s stopped.\n", p->id);
231 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
232
233 for (l = p->listen; l != NULL; l = l->next) {
234 fd_delete(l->fd);
235 listeners--;
236 }
237 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200238 /* try to free more memory */
239 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 }
241 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200242 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243 }
244 }
245 p = p->next;
246 }
247 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200248 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249}
250
251
252/*
253 * this function disables health-check servers so that the process will quickly be ignored
254 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
255 * time will not be used since it would already not listen anymore to the socket.
256 */
257void soft_stop(void)
258{
259 struct proxy *p;
260
261 stopping = 1;
262 p = proxy;
263 tv_now(&now); /* else, the old time before select will be used */
264 while (p) {
265 if (p->state != PR_STSTOPPED) {
266 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
267 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200268 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 }
270 p = p->next;
271 }
272}
273
274
275/*
276 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
277 * Solaris refuses either shutdown().
278 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
279 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
280 * If disabling at least one listener returns an error, then the proxy
281 * state is set to PR_STERROR because we don't know how to resume from this.
282 */
283void pause_proxy(struct proxy *p)
284{
285 struct listener *l;
286 for (l = p->listen; l != NULL; l = l->next) {
287 if (shutdown(l->fd, SHUT_WR) == 0 &&
288 listen(l->fd, p->maxconn) == 0 &&
289 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200290 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291 if (p->state != PR_STERROR)
292 p->state = PR_STPAUSED;
293 }
294 else
295 p->state = PR_STERROR;
296 }
297}
298
299/*
300 * This function temporarily disables listening so that another new instance
301 * can start listening. It is designed to be called upon reception of a
302 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
303 * the proxy, or a SIGTTIN can be sent to listen again.
304 */
305void pause_proxies(void)
306{
307 int err;
308 struct proxy *p;
309
310 err = 0;
311 p = proxy;
312 tv_now(&now); /* else, the old time before select will be used */
313 while (p) {
314 if (p->state != PR_STERROR &&
315 p->state != PR_STSTOPPED &&
316 p->state != PR_STPAUSED) {
317 Warning("Pausing proxy %s.\n", p->id);
318 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
319 pause_proxy(p);
320 if (p->state != PR_STPAUSED) {
321 err |= 1;
322 Warning("Proxy %s failed to enter pause mode.\n", p->id);
323 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
324 }
325 }
326 p = p->next;
327 }
328 if (err) {
329 Warning("Some proxies refused to pause, performing soft stop now.\n");
330 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
331 soft_stop();
332 }
333}
334
335
336/*
337 * This function reactivates listening. This can be used after a call to
338 * sig_pause(), for example when a new instance has failed starting up.
339 * It is designed to be called upon reception of a SIGTTIN.
340 */
341void listen_proxies(void)
342{
343 struct proxy *p;
344 struct listener *l;
345
346 p = proxy;
347 tv_now(&now); /* else, the old time before select will be used */
348 while (p) {
349 if (p->state == PR_STPAUSED) {
350 Warning("Enabling proxy %s.\n", p->id);
351 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
352
353 for (l = p->listen; l != NULL; l = l->next) {
354 if (listen(l->fd, p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100355 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200356 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 p->state = PR_STRUN;
358 }
359 else
360 p->state = PR_STIDLE;
361 } else {
362 int port;
363
364 if (l->addr.ss_family == AF_INET6)
365 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
366 else
367 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
368
369 Warning("Port %d busy while trying to enable proxy %s.\n",
370 port, p->id);
371 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
372 port, p->id);
373 /* Another port might have been enabled. Let's stop everything. */
374 pause_proxy(p);
375 break;
376 }
377 }
378 }
379 p = p->next;
380 }
381}
382
383
384/*
385 * Local variables:
386 * c-indent-level: 8
387 * c-basic-offset: 8
388 * End:
389 */