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