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