blob: f2967de3118e2f190c993a55c02744ddff13a7fc [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 Tarreaue94ebd02007-10-09 17:14:37 +0200161 fdtab[fd].peeraddr = NULL;
162 fdtab[fd].peerlen = 0;
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200163 fdtab[fd].ev = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 listeners++;
165 }
166
167 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200168 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
170 }
171 }
172
173 return err;
174}
175
176
177/*
178 * this function enables proxies when there are enough free sessions,
179 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200180 * select_loop(). It returns the date of next expiration event during stop
181 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200183void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184{
185 struct proxy *p;
186 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187
188 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200189 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190
191 /* if there are enough free sessions, we'll activate proxies */
192 if (actconn < global.maxconn) {
193 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100194 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 if (p->state == PR_STIDLE) {
196 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200197 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198 }
199 p->state = PR_STRUN;
200 }
201 }
202 else {
203 if (p->state == PR_STRUN) {
204 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200205 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 }
207 p->state = PR_STIDLE;
208 }
209 }
210 p = p->next;
211 }
212 }
213 else { /* block all proxies */
214 while (p) {
215 if (p->state == PR_STRUN) {
216 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200217 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 }
219 p->state = PR_STIDLE;
220 }
221 p = p->next;
222 }
223 }
224
225 if (stopping) {
226 p = proxy;
227 while (p) {
228 if (p->state != PR_STSTOPPED) {
229 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200230 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 if (t == 0) {
232 Warning("Proxy %s stopped.\n", p->id);
233 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
234
235 for (l = p->listen; l != NULL; l = l->next) {
236 fd_delete(l->fd);
237 listeners--;
238 }
239 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200240 /* try to free more memory */
241 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 }
243 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200244 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 }
246 }
247 p = p->next;
248 }
249 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200250 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251}
252
253
254/*
255 * this function disables health-check servers so that the process will quickly be ignored
256 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
257 * time will not be used since it would already not listen anymore to the socket.
258 */
259void soft_stop(void)
260{
261 struct proxy *p;
262
263 stopping = 1;
264 p = proxy;
265 tv_now(&now); /* else, the old time before select will be used */
266 while (p) {
267 if (p->state != PR_STSTOPPED) {
268 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
269 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200270 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271 }
272 p = p->next;
273 }
274}
275
276
277/*
278 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
279 * Solaris refuses either shutdown().
280 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
281 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
282 * If disabling at least one listener returns an error, then the proxy
283 * state is set to PR_STERROR because we don't know how to resume from this.
284 */
285void pause_proxy(struct proxy *p)
286{
287 struct listener *l;
288 for (l = p->listen; l != NULL; l = l->next) {
289 if (shutdown(l->fd, SHUT_WR) == 0 &&
290 listen(l->fd, p->maxconn) == 0 &&
291 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200292 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 if (p->state != PR_STERROR)
294 p->state = PR_STPAUSED;
295 }
296 else
297 p->state = PR_STERROR;
298 }
299}
300
301/*
302 * This function temporarily disables listening so that another new instance
303 * can start listening. It is designed to be called upon reception of a
304 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
305 * the proxy, or a SIGTTIN can be sent to listen again.
306 */
307void pause_proxies(void)
308{
309 int err;
310 struct proxy *p;
311
312 err = 0;
313 p = proxy;
314 tv_now(&now); /* else, the old time before select will be used */
315 while (p) {
316 if (p->state != PR_STERROR &&
317 p->state != PR_STSTOPPED &&
318 p->state != PR_STPAUSED) {
319 Warning("Pausing proxy %s.\n", p->id);
320 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
321 pause_proxy(p);
322 if (p->state != PR_STPAUSED) {
323 err |= 1;
324 Warning("Proxy %s failed to enter pause mode.\n", p->id);
325 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
326 }
327 }
328 p = p->next;
329 }
330 if (err) {
331 Warning("Some proxies refused to pause, performing soft stop now.\n");
332 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
333 soft_stop();
334 }
335}
336
337
338/*
339 * This function reactivates listening. This can be used after a call to
340 * sig_pause(), for example when a new instance has failed starting up.
341 * It is designed to be called upon reception of a SIGTTIN.
342 */
343void listen_proxies(void)
344{
345 struct proxy *p;
346 struct listener *l;
347
348 p = proxy;
349 tv_now(&now); /* else, the old time before select will be used */
350 while (p) {
351 if (p->state == PR_STPAUSED) {
352 Warning("Enabling proxy %s.\n", p->id);
353 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
354
355 for (l = p->listen; l != NULL; l = l->next) {
356 if (listen(l->fd, p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100357 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200358 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 p->state = PR_STRUN;
360 }
361 else
362 p->state = PR_STIDLE;
363 } else {
364 int port;
365
366 if (l->addr.ss_family == AF_INET6)
367 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
368 else
369 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
370
371 Warning("Port %d busy while trying to enable proxy %s.\n",
372 port, p->id);
373 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
374 port, p->id);
375 /* Another port might have been enabled. Let's stop everything. */
376 pause_proxy(p);
377 break;
378 }
379 }
380 }
381 p = p->next;
382 }
383}
384
385
386/*
387 * Local variables:
388 * c-indent-level: 8
389 * c-basic-offset: 8
390 * End:
391 */