blob: 06424c62c13411dbd9897f16ae1534e2a4e045ee [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>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010015#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/stat.h>
19
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/defaults.h>
21#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020022#include <common/config.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010023#include <common/errors.h>
Willy Tarreau4d2d0982007-05-14 00:39:29 +020024#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/global.h>
28#include <types/polling.h>
29
30#include <proto/client.h>
Alexandre Cassen87ea5482007-10-11 20:48:58 +020031#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/fd.h>
33#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <proto/protocols.h>
35#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/proxy.h>
37
38
Willy Tarreaue6b98942007-10-29 01:09:36 +010039int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +020040struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreaudcd47712007-11-04 23:35:08 +010041int next_pxid = 1; /* UUID assigned to next new proxy, 0 reserved */
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreau977b8e42006-12-29 14:19:17 +010043/*
Willy Tarreau816eb542007-11-04 07:04:43 +010044 * This function returns a string containing a name describing capabilities to
45 * report comprehensible error messages. Specifically, it will return the words
46 * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other
47 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010048 */
Willy Tarreau816eb542007-11-04 07:04:43 +010049const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010050{
Willy Tarreau816eb542007-11-04 07:04:43 +010051 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
52 if (cap & PR_CAP_FE)
53 return "frontend";
54 else if (cap & PR_CAP_BE)
55 return "backend";
56 else if (cap & PR_CAP_RS)
57 return "ruleset";
58 }
59 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010060}
61
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010062/*
63 * This function returns a string containing the mode of the proxy in a format
64 * suitable for error messages.
65 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010066const char *proxy_mode_str(int mode) {
67
68 if (mode == PR_MODE_TCP)
69 return "tcp";
70 else if (mode == PR_MODE_HTTP)
71 return "http";
72 else if (mode == PR_MODE_HEALTH)
73 return "health";
74 else
75 return "unknown";
76}
77
78/*
79 * This function finds a proxy with matching name, mode and with satisfying
80 * capabilities. It also checks if there are more matching proxies with
81 * requested name as this often leads into unexpected situations.
82 */
83
84struct proxy *findproxy(const char *name, int mode, int cap) {
85
86 struct proxy *curproxy, *target=NULL;
87
88 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
89 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
90 continue;
91
92 if (curproxy->mode != mode) {
93 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
94 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
95 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
96 return NULL;
97 }
98
99 if (!target) {
100 target = curproxy;
101 continue;
102 }
103
Willy Tarreau816eb542007-11-04 07:04:43 +0100104 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100105 name, proxy_type_str(curproxy), proxy_type_str(target));
106
107 return NULL;
108 }
109
110 return target;
111}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112
113/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200114 * This function creates all proxy sockets. It should be done very early,
115 * typically before privileges are dropped. The sockets will be registered
116 * but not added to any fd_set, in order not to loose them across the fork().
117 * The proxies also start in IDLE state, meaning that it will be
118 * maintain_proxies that will finally complete their loading.
119 *
120 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
121 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 */
123int start_proxies(int verbose)
124{
125 struct proxy *curproxy;
126 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100127 int lerr, err = ERR_NONE;
128 int pxerr;
129 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
131 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
132 if (curproxy->state != PR_STNEW)
133 continue; /* already initialized */
134
135 pxerr = 0;
136 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100137 if (listener->state != LI_ASSIGNED)
138 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139
Willy Tarreaue6b98942007-10-29 01:09:36 +0100140 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
Willy Tarreaue6b98942007-10-29 01:09:36 +0100142 /* errors are reported if <verbose> is set or if they are fatal */
143 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
144 if (lerr & ERR_ALERT)
145 Alert("Starting %s %s: %s\n",
146 proxy_type_str(curproxy), curproxy->id, msg);
147 else if (lerr & ERR_WARN)
148 Warning("Starting %s %s: %s\n",
149 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150 }
151
Willy Tarreaue6b98942007-10-29 01:09:36 +0100152 err |= lerr;
153 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100155 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100157 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 pxerr |= 1;
159 continue;
160 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161 }
162
163 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200164 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
166 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100167
168 if (err & ERR_ABORT)
169 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 }
171
172 return err;
173}
174
175
176/*
177 * this function enables proxies when there are enough free sessions,
178 * or stops them when the table is full. It is designed to be called from the
Willy Tarreaud825eef2007-05-12 22:35:00 +0200179 * select_loop(). It returns the date of next expiration event during stop
180 * time, ETERNITY otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200182void maintain_proxies(struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183{
184 struct proxy *p;
185 struct listener *l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186
187 p = proxy;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200188 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
190 /* if there are enough free sessions, we'll activate proxies */
191 if (actconn < global.maxconn) {
192 while (p) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100193 if (p->feconn < p->maxconn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194 if (p->state == PR_STIDLE) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100195 for (l = p->listen; l != NULL; l = l->next)
196 enable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 p->state = PR_STRUN;
198 }
199 }
200 else {
201 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100202 for (l = p->listen; l != NULL; l = l->next)
203 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204 p->state = PR_STIDLE;
205 }
206 }
207 p = p->next;
208 }
209 }
210 else { /* block all proxies */
211 while (p) {
212 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100213 for (l = p->listen; l != NULL; l = l->next)
214 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 p->state = PR_STIDLE;
216 }
217 p = p->next;
218 }
219 }
220
221 if (stopping) {
222 p = proxy;
223 while (p) {
224 if (p->state != PR_STSTOPPED) {
225 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200226 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227 if (t == 0) {
228 Warning("Proxy %s stopped.\n", p->id);
229 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
230
231 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100232 unbind_listener(l);
233 if (l->state >= LI_ASSIGNED) {
234 delete_listener(l);
235 listeners--;
236 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 }
238 p->state = PR_STSTOPPED;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200239 /* try to free more memory */
240 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 }
242 else {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200243 tv_bound(next, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244 }
245 }
246 p = p->next;
247 }
248 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200249 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250}
251
252
253/*
254 * this function disables health-check servers so that the process will quickly be ignored
255 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
256 * time will not be used since it would already not listen anymore to the socket.
257 */
258void soft_stop(void)
259{
260 struct proxy *p;
261
262 stopping = 1;
263 p = proxy;
264 tv_now(&now); /* else, the old time before select will be used */
265 while (p) {
266 if (p->state != PR_STSTOPPED) {
267 Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace);
268 send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200269 tv_ms_add(&p->stop_time, &now, p->grace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 }
271 p = p->next;
272 }
273}
274
275
276/*
277 * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR.
278 * Solaris refuses either shutdown().
279 * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind.
280 * So a common validation path involves SHUT_WR && listen && SHUT_RD.
281 * If disabling at least one listener returns an error, then the proxy
282 * state is set to PR_STERROR because we don't know how to resume from this.
283 */
284void pause_proxy(struct proxy *p)
285{
286 struct listener *l;
287 for (l = p->listen; l != NULL; l = l->next) {
288 if (shutdown(l->fd, SHUT_WR) == 0 &&
289 listen(l->fd, p->maxconn) == 0 &&
290 shutdown(l->fd, SHUT_RD) == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200291 EV_FD_CLR(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292 if (p->state != PR_STERROR)
293 p->state = PR_STPAUSED;
294 }
295 else
296 p->state = PR_STERROR;
297 }
298}
299
300/*
301 * This function temporarily disables listening so that another new instance
302 * can start listening. It is designed to be called upon reception of a
303 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
304 * the proxy, or a SIGTTIN can be sent to listen again.
305 */
306void pause_proxies(void)
307{
308 int err;
309 struct proxy *p;
310
311 err = 0;
312 p = proxy;
313 tv_now(&now); /* else, the old time before select will be used */
314 while (p) {
315 if (p->state != PR_STERROR &&
316 p->state != PR_STSTOPPED &&
317 p->state != PR_STPAUSED) {
318 Warning("Pausing proxy %s.\n", p->id);
319 send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id);
320 pause_proxy(p);
321 if (p->state != PR_STPAUSED) {
322 err |= 1;
323 Warning("Proxy %s failed to enter pause mode.\n", p->id);
324 send_log(p, LOG_WARNING, "Proxy %s failed to enter pause mode.\n", p->id);
325 }
326 }
327 p = p->next;
328 }
329 if (err) {
330 Warning("Some proxies refused to pause, performing soft stop now.\n");
331 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
332 soft_stop();
333 }
334}
335
336
337/*
338 * This function reactivates listening. This can be used after a call to
339 * sig_pause(), for example when a new instance has failed starting up.
340 * It is designed to be called upon reception of a SIGTTIN.
341 */
342void listen_proxies(void)
343{
344 struct proxy *p;
345 struct listener *l;
346
347 p = proxy;
348 tv_now(&now); /* else, the old time before select will be used */
349 while (p) {
350 if (p->state == PR_STPAUSED) {
351 Warning("Enabling proxy %s.\n", p->id);
352 send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id);
353
354 for (l = p->listen; l != NULL; l = l->next) {
355 if (listen(l->fd, p->maxconn) == 0) {
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100356 if (actconn < global.maxconn && p->feconn < p->maxconn) {
Willy Tarreauf161a342007-04-08 16:59:42 +0200357 EV_FD_SET(l->fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 p->state = PR_STRUN;
359 }
360 else
361 p->state = PR_STIDLE;
362 } else {
363 int port;
364
365 if (l->addr.ss_family == AF_INET6)
366 port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
367 else
368 port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
369
370 Warning("Port %d busy while trying to enable proxy %s.\n",
371 port, p->id);
372 send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n",
373 port, p->id);
374 /* Another port might have been enabled. Let's stop everything. */
375 pause_proxy(p);
376 break;
377 }
378 }
379 }
380 p = p->next;
381 }
382}
383
384
385/*
386 * Local variables:
387 * c-indent-level: 8
388 * c-basic-offset: 8
389 * End:
390 */