Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Client-side 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 <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 27 | #include <types/backend.h> |
| 28 | #include <types/buffers.h> |
| 29 | #include <types/global.h> |
| 30 | #include <types/httperr.h> |
| 31 | #include <types/polling.h> |
| 32 | #include <types/proxy.h> |
| 33 | #include <types/server.h> |
| 34 | #include <types/session.h> |
| 35 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 36 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/client.h> |
| 38 | #include <proto/fd.h> |
| 39 | #include <proto/log.h> |
| 40 | #include <proto/proto_http.h> |
| 41 | #include <proto/stream_sock.h> |
| 42 | #include <proto/task.h> |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP. |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * this function is called on a read event from a listen socket, corresponding |
| 52 | * to an accept. It tries to accept as many connections as possible. |
| 53 | * It returns 0. |
| 54 | */ |
| 55 | int event_accept(int fd) { |
| 56 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
| 57 | struct session *s; |
| 58 | struct task *t; |
| 59 | int cfd; |
| 60 | int max_accept; |
| 61 | |
| 62 | if (global.nbproc > 1) |
| 63 | max_accept = 8; /* let other processes catch some connections too */ |
| 64 | else |
| 65 | max_accept = -1; |
| 66 | |
| 67 | while (p->nbconn < p->maxconn && max_accept--) { |
| 68 | struct sockaddr_storage addr; |
| 69 | socklen_t laddr = sizeof(addr); |
| 70 | |
| 71 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 72 | switch (errno) { |
| 73 | case EAGAIN: |
| 74 | case EINTR: |
| 75 | case ECONNABORTED: |
| 76 | return 0; /* nothing more to accept */ |
| 77 | case ENFILE: |
| 78 | send_log(p, LOG_EMERG, |
| 79 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 80 | p->id, maxfd); |
| 81 | return 0; |
| 82 | case EMFILE: |
| 83 | send_log(p, LOG_EMERG, |
| 84 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 85 | p->id, maxfd); |
| 86 | return 0; |
| 87 | case ENOBUFS: |
| 88 | case ENOMEM: |
| 89 | send_log(p, LOG_EMERG, |
| 90 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 91 | p->id, maxfd); |
| 92 | return 0; |
| 93 | default: |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 99 | Alert("out of memory in event_accept().\n"); |
| 100 | FD_CLR(fd, StaticReadEvent); |
| 101 | p->state = PR_STIDLE; |
| 102 | close(cfd); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* if this session comes from a known monitoring system, we want to ignore |
| 107 | * it as soon as possible, which means closing it immediately for TCP. |
| 108 | */ |
| 109 | s->flags = 0; |
| 110 | if (addr.ss_family == AF_INET && |
| 111 | p->mon_mask.s_addr && |
| 112 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 113 | if (p->mode == PR_MODE_TCP) { |
| 114 | close(cfd); |
| 115 | pool_free(session, s); |
| 116 | continue; |
| 117 | } |
| 118 | s->flags |= SN_MONITOR; |
| 119 | } |
| 120 | |
| 121 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 122 | Alert("out of memory in event_accept().\n"); |
| 123 | FD_CLR(fd, StaticReadEvent); |
| 124 | p->state = PR_STIDLE; |
| 125 | close(cfd); |
| 126 | pool_free(session, s); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | s->cli_addr = addr; |
| 131 | if (cfd >= global.maxsock) { |
| 132 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 133 | close(cfd); |
| 134 | pool_free(task, t); |
| 135 | pool_free(session, s); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 140 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 141 | (char *) &one, sizeof(one)) == -1)) { |
| 142 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 143 | close(cfd); |
| 144 | pool_free(task, t); |
| 145 | pool_free(session, s); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | if (p->options & PR_O_TCP_CLI_KA) |
| 150 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 151 | |
| 152 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 153 | t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */ |
| 154 | t->state = TASK_IDLE; |
| 155 | t->process = process_session; |
| 156 | t->context = s; |
| 157 | |
| 158 | s->task = t; |
| 159 | s->proxy = p; |
| 160 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 161 | s->srv_state = SV_STIDLE; |
| 162 | s->req = s->rep = NULL; /* will be allocated later */ |
| 163 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 164 | s->cli_fd = cfd; |
| 165 | s->srv_fd = -1; |
| 166 | s->req_line.len = -1; |
| 167 | s->auth_hdr.len = -1; |
| 168 | s->srv = NULL; |
| 169 | s->pend_pos = NULL; |
| 170 | s->conn_retries = p->conn_retries; |
| 171 | |
| 172 | if (s->flags & SN_MONITOR) |
| 173 | s->logs.logwait = 0; |
| 174 | else |
| 175 | s->logs.logwait = p->to_log; |
| 176 | |
| 177 | s->logs.tv_accept = now; |
| 178 | s->logs.t_request = -1; |
| 179 | s->logs.t_queue = -1; |
| 180 | s->logs.t_connect = -1; |
| 181 | s->logs.t_data = -1; |
| 182 | s->logs.t_close = 0; |
| 183 | s->logs.uri = NULL; |
| 184 | s->logs.cli_cookie = NULL; |
| 185 | s->logs.srv_cookie = NULL; |
| 186 | s->logs.status = -1; |
| 187 | s->logs.bytes = 0; |
| 188 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 189 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 190 | |
| 191 | s->data_source = DATA_SRC_NONE; |
| 192 | |
| 193 | s->uniq_id = totalconn; |
| 194 | p->cum_conn++; |
| 195 | |
| 196 | if (p->nb_req_cap > 0) { |
| 197 | if ((s->req_cap = |
| 198 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 199 | == NULL) { /* no memory */ |
| 200 | close(cfd); /* nothing can be done for this fd without memory */ |
| 201 | pool_free(task, t); |
| 202 | pool_free(session, s); |
| 203 | return 0; |
| 204 | } |
| 205 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 206 | } |
| 207 | else |
| 208 | s->req_cap = NULL; |
| 209 | |
| 210 | if (p->nb_rsp_cap > 0) { |
| 211 | if ((s->rsp_cap = |
| 212 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 213 | == NULL) { /* no memory */ |
| 214 | if (s->req_cap != NULL) |
| 215 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 216 | close(cfd); /* nothing can be done for this fd without memory */ |
| 217 | pool_free(task, t); |
| 218 | pool_free(session, s); |
| 219 | return 0; |
| 220 | } |
| 221 | memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 222 | } |
| 223 | else |
| 224 | s->rsp_cap = NULL; |
| 225 | |
| 226 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 227 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
| 228 | struct sockaddr_storage sockname; |
| 229 | socklen_t namelen = sizeof(sockname); |
| 230 | |
| 231 | if (addr.ss_family != AF_INET || |
| 232 | !(s->proxy->options & PR_O_TRANSP) || |
| 233 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 234 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 235 | |
| 236 | if (p->to_log) { |
| 237 | /* we have the client ip */ |
| 238 | if (s->logs.logwait & LW_CLIP) |
| 239 | if (!(s->logs.logwait &= ~LW_CLIP)) |
| 240 | sess_log(s); |
| 241 | } |
| 242 | else if (s->cli_addr.ss_family == AF_INET) { |
| 243 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 244 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 245 | sn, sizeof(sn)) && |
| 246 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 247 | pn, sizeof(pn))) { |
| 248 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 249 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 250 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 251 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 252 | } |
| 253 | } |
| 254 | else { |
| 255 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 256 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 257 | sn, sizeof(sn)) && |
| 258 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 259 | pn, sizeof(pn))) { |
| 260 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 261 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 262 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 263 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 269 | struct sockaddr_in sockname; |
| 270 | socklen_t namelen = sizeof(sockname); |
| 271 | int len; |
| 272 | if (addr.ss_family != AF_INET || |
| 273 | !(s->proxy->options & PR_O_TRANSP) || |
| 274 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 275 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 276 | |
| 277 | if (s->cli_addr.ss_family == AF_INET) { |
| 278 | char pn[INET_ADDRSTRLEN]; |
| 279 | inet_ntop(AF_INET, |
| 280 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 281 | pn, sizeof(pn)); |
| 282 | |
| 283 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 284 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 285 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 286 | } |
| 287 | else { |
| 288 | char pn[INET6_ADDRSTRLEN]; |
| 289 | inet_ntop(AF_INET6, |
| 290 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 291 | pn, sizeof(pn)); |
| 292 | |
| 293 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 294 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 295 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 296 | } |
| 297 | |
| 298 | write(1, trash, len); |
| 299 | } |
| 300 | |
| 301 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 302 | if (s->rsp_cap != NULL) |
| 303 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 304 | if (s->req_cap != NULL) |
| 305 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 306 | close(cfd); /* nothing can be done for this fd without memory */ |
| 307 | pool_free(task, t); |
| 308 | pool_free(session, s); |
| 309 | return 0; |
| 310 | } |
| 311 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 312 | buffer_init(s->req); |
| 313 | s->req->rlim += BUFSIZE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 314 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
| 315 | s->req->rlim -= MAXREWRITE; |
| 316 | |
| 317 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 318 | pool_free(buffer, s->req); |
| 319 | if (s->rsp_cap != NULL) |
| 320 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 321 | if (s->req_cap != NULL) |
| 322 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 323 | close(cfd); /* nothing can be done for this fd without memory */ |
| 324 | pool_free(task, t); |
| 325 | pool_free(session, s); |
| 326 | return 0; |
| 327 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 328 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 329 | buffer_init(s->rep); |
| 330 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 331 | fdtab[cfd].owner = t; |
| 332 | fdtab[cfd].state = FD_STREADY; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 333 | fdtab[cfd].cb[DIR_RD].f = &event_cli_read; |
| 334 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 335 | fdtab[cfd].cb[DIR_WR].f = &event_cli_write; |
| 336 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | |
| 338 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 339 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 340 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 341 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 342 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 343 | */ |
| 344 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 345 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 346 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
| 347 | } |
| 348 | else { |
| 349 | FD_SET(cfd, StaticReadEvent); |
| 350 | } |
| 351 | |
| 352 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 353 | if (PrevReadEvent) { |
| 354 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 355 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 356 | } |
| 357 | #endif |
| 358 | fd_insert(cfd); |
| 359 | |
| 360 | tv_eternity(&s->cnexpire); |
| 361 | tv_eternity(&s->srexpire); |
| 362 | tv_eternity(&s->swexpire); |
| 363 | tv_eternity(&s->crexpire); |
| 364 | tv_eternity(&s->cwexpire); |
| 365 | |
| 366 | if (s->proxy->clitimeout) { |
| 367 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 368 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 369 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 370 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 371 | } |
| 372 | |
| 373 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
| 374 | |
| 375 | task_queue(t); |
| 376 | |
| 377 | if (p->mode != PR_MODE_HEALTH) |
| 378 | task_wakeup(&rq, t); |
| 379 | |
| 380 | p->nbconn++; |
| 381 | if (p->nbconn > p->nbconn_max) |
| 382 | p->nbconn_max = p->nbconn; |
| 383 | actconn++; |
| 384 | totalconn++; |
| 385 | |
| 386 | // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t); |
| 387 | } /* end of while (p->nbconn < p->maxconn) */ |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | /* |
| 394 | * Local variables: |
| 395 | * c-indent-level: 8 |
| 396 | * c-basic-offset: 8 |
| 397 | * End: |
| 398 | */ |