blob: 640a2c1d82fb3df265d8e4af38bcf9fb1e8bed73 [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 Tarreaud740bab2007-10-28 11:14:07 +010022#include <common/errors.h>
Willy Tarreau4d2d0982007-05-14 00:39:29 +020023#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020024#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
26#include <types/global.h>
27#include <types/polling.h>
28
29#include <proto/client.h>
Alexandre Cassen87ea5482007-10-11 20:48:58 +020030#include <proto/backend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/fd.h>
32#include <proto/log.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010033#include <proto/protocols.h>
34#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/proxy.h>
36
37
Willy Tarreaue6b98942007-10-29 01:09:36 +010038int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +020039struct proxy *proxy = NULL; /* list of all existing proxies */
Willy Tarreaudcd47712007-11-04 23:35:08 +010040int next_pxid = 1; /* UUID assigned to next new proxy, 0 reserved */
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau977b8e42006-12-29 14:19:17 +010042/*
Willy Tarreau816eb542007-11-04 07:04:43 +010043 * This function returns a string containing a name describing capabilities to
44 * report comprehensible error messages. Specifically, it will return the words
45 * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other
46 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +010047 */
Willy Tarreau816eb542007-11-04 07:04:43 +010048const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +010049{
Willy Tarreau816eb542007-11-04 07:04:43 +010050 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
51 if (cap & PR_CAP_FE)
52 return "frontend";
53 else if (cap & PR_CAP_BE)
54 return "backend";
55 else if (cap & PR_CAP_RS)
56 return "ruleset";
57 }
58 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +010059}
60
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010061/*
62 * This function returns a string containing the mode of the proxy in a format
63 * suitable for error messages.
64 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +010065const char *proxy_mode_str(int mode) {
66
67 if (mode == PR_MODE_TCP)
68 return "tcp";
69 else if (mode == PR_MODE_HTTP)
70 return "http";
71 else if (mode == PR_MODE_HEALTH)
72 return "health";
73 else
74 return "unknown";
75}
76
77/*
78 * This function finds a proxy with matching name, mode and with satisfying
79 * capabilities. It also checks if there are more matching proxies with
80 * requested name as this often leads into unexpected situations.
81 */
82
83struct proxy *findproxy(const char *name, int mode, int cap) {
84
85 struct proxy *curproxy, *target=NULL;
86
87 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
88 if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name))
89 continue;
90
91 if (curproxy->mode != mode) {
92 Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n",
93 name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode));
94 Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode));
95 return NULL;
96 }
97
98 if (!target) {
99 target = curproxy;
100 continue;
101 }
102
Willy Tarreau816eb542007-11-04 07:04:43 +0100103 Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n",
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100104 name, proxy_type_str(curproxy), proxy_type_str(target));
105
106 return NULL;
107 }
108
109 return target;
110}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
112/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200113 * This function creates all proxy sockets. It should be done very early,
114 * typically before privileges are dropped. The sockets will be registered
115 * but not added to any fd_set, in order not to loose them across the fork().
116 * The proxies also start in IDLE state, meaning that it will be
117 * maintain_proxies that will finally complete their loading.
118 *
119 * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
120 * Retryable errors will only be printed if <verbose> is not zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 */
122int start_proxies(int verbose)
123{
124 struct proxy *curproxy;
125 struct listener *listener;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100126 int lerr, err = ERR_NONE;
127 int pxerr;
128 char msg[100];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129
130 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
131 if (curproxy->state != PR_STNEW)
132 continue; /* already initialized */
133
134 pxerr = 0;
135 for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100136 if (listener->state != LI_ASSIGNED)
137 continue; /* already started */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138
Willy Tarreaue6b98942007-10-29 01:09:36 +0100139 lerr = tcp_bind_listener(listener, msg, sizeof(msg));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140
Willy Tarreaue6b98942007-10-29 01:09:36 +0100141 /* errors are reported if <verbose> is set or if they are fatal */
142 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
143 if (lerr & ERR_ALERT)
144 Alert("Starting %s %s: %s\n",
145 proxy_type_str(curproxy), curproxy->id, msg);
146 else if (lerr & ERR_WARN)
147 Warning("Starting %s %s: %s\n",
148 proxy_type_str(curproxy), curproxy->id, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 }
150
Willy Tarreaue6b98942007-10-29 01:09:36 +0100151 err |= lerr;
152 if (lerr & (ERR_ABORT | ERR_FATAL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 pxerr |= 1;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100154 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100156 else if (lerr & ERR_CODE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157 pxerr |= 1;
158 continue;
159 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160 }
161
162 if (!pxerr) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200163 curproxy->state = PR_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
165 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100166
167 if (err & ERR_ABORT)
168 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 }
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) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100194 for (l = p->listen; l != NULL; l = l->next)
195 enable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 p->state = PR_STRUN;
197 }
198 }
199 else {
200 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100201 for (l = p->listen; l != NULL; l = l->next)
202 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203 p->state = PR_STIDLE;
204 }
205 }
206 p = p->next;
207 }
208 }
209 else { /* block all proxies */
210 while (p) {
211 if (p->state == PR_STRUN) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100212 for (l = p->listen; l != NULL; l = l->next)
213 disable_listener(l);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214 p->state = PR_STIDLE;
215 }
216 p = p->next;
217 }
218 }
219
220 if (stopping) {
221 p = proxy;
222 while (p) {
223 if (p->state != PR_STSTOPPED) {
224 int t;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200225 t = tv_ms_remain2(&now, &p->stop_time);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226 if (t == 0) {
227 Warning("Proxy %s stopped.\n", p->id);
228 send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
229
230 for (l = p->listen; l != NULL; l = l->next) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100231 unbind_listener(l);
232 if (l->state >= LI_ASSIGNED) {
233 delete_listener(l);
234 listeners--;
235 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236 }
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 */