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