blob: ff3cdc7847f94042bed03f63570732a4da3de0ae [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Proxy 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 <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 Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <types/global.h>
25#include <types/polling.h>
26
27#include <proto/client.h>
28#include <proto/fd.h>
29#include <proto/log.h>
30#include <proto/proxy.h>
31
32
33int listeners; /* # of listeners */
34struct proxy *proxy = NULL; /* list of all existing proxies */
35
Willy Tarreau977b8e42006-12-29 14:19:17 +010036/*
37 * This function returns a string containing the type of the proxy in a format
38 * suitable for error messages, from its capabilities.
39 */
Willy Tarreau2b5652f2006-12-31 17:46:05 +010040const char *proxy_type_str(struct proxy *proxy)
Willy Tarreau977b8e42006-12-29 14:19:17 +010041{
Willy Tarreau2b5652f2006-12-31 17:46:05 +010042 int cap = proxy->cap;
Willy Tarreau977b8e42006-12-29 14:19:17 +010043 if ((cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
44 return "listener";
45 else if (cap & PR_CAP_FE)
46 return "frontend";
47 else if (cap & PR_CAP_BE)
48 return "backend";
49 else if (cap & PR_CAP_RS)
50 return "ruleset";
51 else
52 return "proxy";
53}
54
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
56/*
57 * this function starts all the proxies. Its return value is composed from
58 * ERR_NONE, ERR_RETRYABLE and ERR_FATAL. Retryable errors will only be printed
59 * if <verbose> is not zero.
60 */
61int start_proxies(int verbose)
62{
63 struct proxy *curproxy;
64 struct listener *listener;
65 int err = ERR_NONE;
66 int fd, pxerr;
67
68 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
69 if (curproxy->state != PR_STNEW)
70 continue; /* already initialized */
71
72 pxerr = 0;
73 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
74 if (listener->fd != -1)
75 continue; /* already initialized */
76
77 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
78 if (verbose)
79 Alert("cannot create listening socket for proxy %s. Aborting.\n",
80 curproxy->id);
81 err |= ERR_RETRYABLE;
82 pxerr |= 1;
83 continue;
84 }
85
86 if (fd >= global.maxsock) {
87 Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n",
88 curproxy->id);
89 close(fd);
90 err |= ERR_FATAL;
91 pxerr |= 1;
92 break;
93 }
94
95 if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
96 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
97 (char *) &one, sizeof(one)) == -1)) {
98 Alert("cannot make socket non-blocking for proxy %s. Aborting.\n",
99 curproxy->id);
100 close(fd);
101 err |= ERR_FATAL;
102 pxerr |= 1;
103 break;
104 }
105
106 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
107 Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n",
108 curproxy->id);
109 }
110
111#ifdef SO_REUSEPORT
112 /* OpenBSD supports this. As it's present in old libc versions of Linux,
113 * it might return an error that we will silently ignore.
114 */
115 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
116#endif
117 if (bind(fd,
118 (struct sockaddr *)&listener->addr,
119 listener->addr.ss_family == AF_INET6 ?
120 sizeof(struct sockaddr_in6) :
121 sizeof(struct sockaddr_in)) == -1) {
122 if (verbose)
123 Alert("cannot bind socket for proxy %s. Aborting.\n",
124 curproxy->id);
125 close(fd);
126 err |= ERR_RETRYABLE;
127 pxerr |= 1;
128 continue;
129 }
130
131 if (listen(fd, curproxy->maxconn) == -1) {
132 if (verbose)
133 Alert("cannot listen to socket for proxy %s. Aborting.\n",
134 curproxy->id);
135 close(fd);
136 err |= ERR_RETRYABLE;
137 pxerr |= 1;
138 continue;
139 }
140
141 /* the socket is ready */
142 listener->fd = fd;
143
144 /* the function for the accept() event */
Willy Tarreau54469402006-07-29 16:59:06 +0200145 fdtab[fd].cb[DIR_RD].f = &event_accept;
146 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
147 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148 fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */
149 fdtab[fd].state = FD_STLISTEN;
Willy Tarreauf161a342007-04-08 16:59:42 +0200150 EV_FD_SET(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 fd_insert(fd);
152 listeners++;
153 }
154
155 if (!pxerr) {
156 curproxy->state = PR_STRUN;
157 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
158 }
159 }
160
161 return err;
162}
163
164
165/*
166 * this function enables proxies when there are enough free sessions,
167 * or stops them when the table is full. It is designed to be called from the
168 * select_loop(). It returns the time left before next expiration event
169 * during stop time, TIME_ETERNITY otherwise.
170 */
171int maintain_proxies(void)
172{
173 struct proxy *p;
174 struct listener *l;
175 int tleft; /* time left */
176
177 p = proxy;
178 tleft = TIME_ETERNITY; /* infinite time */
179
180 /* if there are enough free sessions, we'll activate proxies */
181 if (actconn < global.maxconn) {
182 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100183 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 if (p->state == PR_STIDLE) {
185 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200186 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 }
188 p->state = PR_STRUN;
189 }
190 }
191 else {
192 if (p->state == PR_STRUN) {
193 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200194 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 }
196 p->state = PR_STIDLE;
197 }
198 }
199 p = p->next;
200 }
201 }
202 else { /* block all proxies */
203 while (p) {
204 if (p->state == PR_STRUN) {
205 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200206 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207 }
208 p->state = PR_STIDLE;
209 }
210 p = p->next;
211 }
212 }
213
214 if (stopping) {
215 p = proxy;
216 while (p) {
217 if (p->state != PR_STSTOPPED) {
218 int t;
219 t = tv_remain2(&now, &p->stop_time);
220 if (t == 0) {
221 Warning("Proxy %s stopped.\n", p->id);
222 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
223
224 for (l = p->listen; l != NULL; l = l->next) {
225 fd_delete(l->fd);
226 listeners--;
227 }
228 p->state = PR_STSTOPPED;
229 }
230 else {
231 tleft = MINTIME(t, tleft);
232 }
233 }
234 p = p->next;
235 }
236 }
237 return tleft;
238}
239
240
241/*
242 * this function disables health-check servers so that the process will quickly be ignored
243 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
244 * time will not be used since it would already not listen anymore to the socket.
245 */
246void soft_stop(void)
247{
248 struct proxy *p;
249
250 stopping = 1;
251 p = proxy;
252 tv_now(&now); /* else, the old time before select will be used */
253 while (p) {
254 if (p->state != PR_STSTOPPED) {
255 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
256 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
257 tv_delayfrom(&p->stop_time, &now, p->grace);
258 }
259 p = p->next;
260 }
261}
262
263
264/*
265 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
266 * Solaris refuses either shutdown().
267 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
268 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
269 * If disabling at least one listener returns an error, then the proxy
270 * state is set to PR_STERROR because we don't know how to resume from this.
271 */
272void pause_proxy(struct proxy *p)
273{
274 struct listener *l;
275 for (l = p->listen; l != NULL; l = l->next) {
276 if (shutdown(l->fd, SHUT_WR) == 0 &&
277 listen(l->fd, p->maxconn) == 0 &&
278 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200279 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 if (p->state != PR_STERROR)
281 p->state = PR_STPAUSED;
282 }
283 else
284 p->state = PR_STERROR;
285 }
286}
287
288/*
289 * This function temporarily disables listening so that another new instance
290 * can start listening. It is designed to be called upon reception of a
291 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
292 * the proxy, or a SIGTTIN can be sent to listen again.
293 */
294void pause_proxies(void)
295{
296 int err;
297 struct proxy *p;
298
299 err = 0;
300 p = proxy;
301 tv_now(&now); /* else, the old time before select will be used */
302 while (p) {
303 if (p->state != PR_STERROR &&
304 p->state != PR_STSTOPPED &&
305 p->state != PR_STPAUSED) {
306 Warning("Pausing proxy %s.\n", p->id);
307 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
308 pause_proxy(p);
309 if (p->state != PR_STPAUSED) {
310 err |= 1;
311 Warning("Proxy %s failed to enter pause mode.\n", p->id);
312 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
313 }
314 }
315 p = p->next;
316 }
317 if (err) {
318 Warning("Some proxies refused to pause, performing soft stop now.\n");
319 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
320 soft_stop();
321 }
322}
323
324
325/*
326 * This function reactivates listening. This can be used after a call to
327 * sig_pause(), for example when a new instance has failed starting up.
328 * It is designed to be called upon reception of a SIGTTIN.
329 */
330void listen_proxies(void)
331{
332 struct proxy *p;
333 struct listener *l;
334
335 p = proxy;
336 tv_now(&now); /* else, the old time before select will be used */
337 while (p) {
338 if (p->state == PR_STPAUSED) {
339 Warning("Enabling proxy %s.\n", p->id);
340 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
341
342 for (l = p->listen; l != NULL; l = l->next) {
343 if (listen(l->fd, p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100344 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200345 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346 p->state = PR_STRUN;
347 }
348 else
349 p->state = PR_STIDLE;
350 } else {
351 int port;
352
353 if (l->addr.ss_family == AF_INET6)
354 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
355 else
356 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
357
358 Warning("Port %d busy while trying to enable proxy %s.\n",
359 port, p->id);
360 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
361 port, p->id);
362 /* Another port might have been enabled. Let's stop everything. */
363 pause_proxy(p);
364 break;
365 }
366 }
367 }
368 p = p->next;
369 }
370}
371
372
373/*
374 * Local variables:
375 * c-indent-level: 8
376 * c-basic-offset: 8
377 * End:
378 */