Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 2 | * Session management functions. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 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 <stdlib.h> |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | #include <fcntl.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 18 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <types/capture.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 22 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 24 | #include <proto/acl.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 25 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 26 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 27 | #include <proto/checks.h> |
Willy Tarreau | 5ca791d | 2009-08-16 19:06:42 +0200 | [diff] [blame] | 28 | #include <proto/dumpstats.h> |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 29 | #include <proto/freq_ctr.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 30 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 31 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 33 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 34 | #include <proto/proto_http.h> |
| 35 | #include <proto/proto_tcp.h> |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 36 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 38 | #include <proto/server.h> |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 39 | #include <proto/stick_table.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 40 | #include <proto/stream_interface.h> |
| 41 | #include <proto/stream_sock.h> |
| 42 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 44 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 45 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 47 | /* This function is called from the protocol layer accept() in order to instanciate |
| 48 | * a new session on behalf of a given listener and frontend. It returns a positive |
| 49 | * value upon success, 0 if the connection needs to be closed and ignored, or a |
| 50 | * negative value upon critical failure. |
| 51 | */ |
| 52 | int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr) |
| 53 | { |
| 54 | struct proxy *p = l->frontend; |
| 55 | struct session *s; |
| 56 | struct http_txn *txn; |
| 57 | struct task *t; |
| 58 | |
| 59 | if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) { |
| 60 | Alert("out of memory in event_accept().\n"); |
| 61 | goto out_close; |
| 62 | } |
| 63 | |
| 64 | /* minimum session initialization required for monitor mode below */ |
| 65 | s->flags = 0; |
| 66 | s->logs.logwait = p->to_log; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 67 | s->tracked_counters = NULL; |
| 68 | s->tracked_table = NULL; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 69 | |
| 70 | /* if this session comes from a known monitoring system, we want to ignore |
| 71 | * it as soon as possible, which means closing it immediately for TCP, but |
| 72 | * cleanly. |
| 73 | */ |
| 74 | if (unlikely((l->options & LI_O_CHK_MONNET) && |
| 75 | addr->ss_family == AF_INET && |
| 76 | (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) { |
| 77 | if (p->mode == PR_MODE_TCP) { |
| 78 | pool_free2(pool2_session, s); |
| 79 | return 0; |
| 80 | } |
| 81 | s->flags |= SN_MONITOR; |
| 82 | s->logs.logwait = 0; |
| 83 | } |
| 84 | |
| 85 | /* OK, we're keeping the session, so let's properly initialize the session */ |
| 86 | LIST_ADDQ(&sessions, &s->list); |
| 87 | LIST_INIT(&s->back_refs); |
| 88 | |
| 89 | if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */ |
| 90 | Alert("out of memory in event_accept().\n"); |
| 91 | goto out_free_session; |
| 92 | } |
| 93 | |
| 94 | s->term_trace = 0; |
| 95 | s->cli_addr = *addr; |
| 96 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 97 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 98 | s->uniq_id = totalconn; |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 99 | p->feconn++; /* beconn will be increased once assigned */ |
| 100 | |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 101 | proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */ |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 102 | |
| 103 | t->process = l->handler; |
| 104 | t->context = s; |
| 105 | t->nice = l->nice; |
| 106 | t->expire = TICK_ETERNITY; |
| 107 | |
| 108 | s->task = t; |
| 109 | s->listener = l; |
| 110 | |
| 111 | /* Note: initially, the session's backend points to the frontend. |
| 112 | * This changes later when switching rules are executed or |
| 113 | * when the default backend is assigned. |
| 114 | */ |
| 115 | s->be = s->fe = p; |
| 116 | s->req = s->rep = NULL; /* will be allocated later */ |
| 117 | |
| 118 | /* now evaluate the tcp-request layer4 rules. Since we expect to be able |
| 119 | * to abort right here as soon as possible, we check the rules before |
| 120 | * even initializing the stream interfaces. |
| 121 | */ |
| 122 | if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) { |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 123 | if (s->tracked_counters) |
| 124 | session_store_counters(s); |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 125 | task_free(t); |
| 126 | LIST_DEL(&s->list); |
| 127 | pool_free2(pool2_session, s); |
| 128 | /* let's do a no-linger now to close with a single RST. */ |
| 129 | setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 130 | p->feconn--; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 134 | /* This session was accepted, count it now */ |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 135 | if (p->feconn > p->counters.feconn_max) |
| 136 | p->counters.feconn_max = p->feconn; |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 137 | proxy_inc_fe_sess_ctr(l, p); |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 138 | if (s->tracked_counters) { |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 139 | void *ptr; |
| 140 | |
| 141 | ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_CNT); |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 142 | if (ptr) |
| 143 | stktable_data_cast(ptr, sess_cnt)++; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 144 | |
| 145 | ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_RATE); |
| 146 | if (ptr) |
| 147 | update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate), |
| 148 | s->tracked_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1); |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 149 | } |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 150 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 151 | /* this part should be common with other protocols */ |
| 152 | s->si[0].fd = cfd; |
| 153 | s->si[0].owner = t; |
| 154 | s->si[0].state = s->si[0].prev_state = SI_ST_EST; |
| 155 | s->si[0].err_type = SI_ET_NONE; |
| 156 | s->si[0].err_loc = NULL; |
| 157 | s->si[0].connect = NULL; |
| 158 | s->si[0].iohandler = NULL; |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 159 | s->si[0].release = NULL; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 160 | s->si[0].exp = TICK_ETERNITY; |
| 161 | s->si[0].flags = SI_FL_NONE; |
| 162 | |
| 163 | if (likely(s->fe->options2 & PR_O2_INDEPSTR)) |
| 164 | s->si[0].flags |= SI_FL_INDEP_STR; |
| 165 | |
| 166 | if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6) |
| 167 | s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */ |
| 168 | |
| 169 | /* add the various callbacks */ |
| 170 | stream_sock_prepare_interface(&s->si[0]); |
| 171 | |
| 172 | /* pre-initialize the other side's stream interface to an INIT state. The |
| 173 | * callbacks will be initialized before attempting to connect. |
| 174 | */ |
| 175 | s->si[1].fd = -1; /* just to help with debugging */ |
| 176 | s->si[1].owner = t; |
| 177 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 178 | s->si[1].err_type = SI_ET_NONE; |
| 179 | s->si[1].err_loc = NULL; |
| 180 | s->si[1].connect = NULL; |
| 181 | s->si[1].iohandler = NULL; |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 182 | s->si[1].release = NULL; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 183 | s->si[1].shutr = stream_int_shutr; |
| 184 | s->si[1].shutw = stream_int_shutw; |
| 185 | s->si[1].exp = TICK_ETERNITY; |
| 186 | s->si[1].flags = SI_FL_NONE; |
| 187 | |
| 188 | if (likely(s->fe->options2 & PR_O2_INDEPSTR)) |
| 189 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 190 | |
| 191 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 192 | s->pend_pos = NULL; |
| 193 | |
| 194 | /* init store persistence */ |
| 195 | s->store_count = 0; |
| 196 | |
| 197 | /* Adjust some socket options */ |
| 198 | if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) { |
| 199 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 200 | goto out_free_task; |
| 201 | } |
| 202 | |
| 203 | txn = &s->txn; |
| 204 | /* Those variables will be checked and freed if non-NULL in |
| 205 | * session.c:session_free(). It is important that they are |
| 206 | * properly initialized. |
| 207 | */ |
| 208 | txn->sessid = NULL; |
| 209 | txn->srv_cookie = NULL; |
| 210 | txn->cli_cookie = NULL; |
| 211 | txn->uri = NULL; |
| 212 | txn->req.cap = NULL; |
| 213 | txn->rsp.cap = NULL; |
| 214 | txn->hdr_idx.v = NULL; |
| 215 | txn->hdr_idx.size = txn->hdr_idx.used = 0; |
| 216 | |
| 217 | if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL)) |
| 218 | goto out_free_task; /* no memory */ |
| 219 | |
| 220 | if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL)) |
| 221 | goto out_free_req; /* no memory */ |
| 222 | |
| 223 | /* initialize the request buffer */ |
| 224 | s->req->size = global.tune.bufsize; |
| 225 | buffer_init(s->req); |
| 226 | s->req->prod = &s->si[0]; |
| 227 | s->req->cons = &s->si[1]; |
| 228 | s->si[0].ib = s->si[1].ob = s->req; |
| 229 | s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */ |
| 230 | |
| 231 | /* activate default analysers enabled for this listener */ |
| 232 | s->req->analysers = l->analysers; |
| 233 | |
| 234 | s->req->wto = TICK_ETERNITY; |
| 235 | s->req->rto = TICK_ETERNITY; |
| 236 | s->req->rex = TICK_ETERNITY; |
| 237 | s->req->wex = TICK_ETERNITY; |
| 238 | s->req->analyse_exp = TICK_ETERNITY; |
| 239 | |
| 240 | /* initialize response buffer */ |
| 241 | s->rep->size = global.tune.bufsize; |
| 242 | buffer_init(s->rep); |
| 243 | s->rep->prod = &s->si[1]; |
| 244 | s->rep->cons = &s->si[0]; |
| 245 | s->si[0].ob = s->si[1].ib = s->rep; |
| 246 | s->rep->analysers = 0; |
| 247 | |
| 248 | s->rep->rto = TICK_ETERNITY; |
| 249 | s->rep->wto = TICK_ETERNITY; |
| 250 | s->rep->rex = TICK_ETERNITY; |
| 251 | s->rep->wex = TICK_ETERNITY; |
| 252 | s->rep->analyse_exp = TICK_ETERNITY; |
| 253 | |
| 254 | /* finish initialization of the accepted file descriptor */ |
| 255 | fd_insert(cfd); |
| 256 | fdtab[cfd].owner = &s->si[0]; |
| 257 | fdtab[cfd].state = FD_STREADY; |
| 258 | fdtab[cfd].flags = 0; |
| 259 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 260 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 261 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 262 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 263 | fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 264 | fdinfo[cfd].peerlen = sizeof(s->cli_addr); |
| 265 | EV_FD_SET(cfd, DIR_RD); |
| 266 | |
| 267 | if (p->accept) { |
| 268 | int ret = p->accept(s); |
| 269 | if (unlikely(ret < 0)) |
| 270 | goto out_free_rep; |
| 271 | |
| 272 | if (unlikely(ret == 0)) { |
| 273 | /* work is finished, we can release everything (eg: monitoring) */ |
| 274 | pool_free2(pool2_buffer, s->rep); |
| 275 | pool_free2(pool2_buffer, s->req); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 276 | if (s->tracked_counters) |
| 277 | session_store_counters(s); |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 278 | task_free(t); |
| 279 | LIST_DEL(&s->list); |
| 280 | pool_free2(pool2_session, s); |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 281 | p->feconn--; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* it is important not to call the wakeup function directly but to |
| 287 | * pass through task_wakeup(), because this one knows how to apply |
| 288 | * priorities to tasks. |
| 289 | */ |
| 290 | task_wakeup(t, TASK_WOKEN_INIT); |
| 291 | return 1; |
| 292 | |
| 293 | /* Error unrolling */ |
| 294 | out_free_rep: |
| 295 | pool_free2(pool2_buffer, s->rep); |
| 296 | out_free_req: |
| 297 | pool_free2(pool2_buffer, s->req); |
| 298 | out_free_task: |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 299 | p->feconn--; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 300 | if (s->tracked_counters) |
| 301 | session_store_counters(s); |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 302 | task_free(t); |
| 303 | out_free_session: |
| 304 | LIST_DEL(&s->list); |
| 305 | pool_free2(pool2_session, s); |
| 306 | out_close: |
| 307 | return -1; |
| 308 | } |
| 309 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 310 | /* |
| 311 | * frees the context associated to a session. It must have been removed first. |
| 312 | */ |
| 313 | void session_free(struct session *s) |
| 314 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 315 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 316 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 317 | struct bref *bref, *back; |
Willy Tarreau | a4cda67 | 2010-06-06 18:28:49 +0200 | [diff] [blame] | 318 | int i; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 319 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 320 | if (s->pend_pos) |
| 321 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 322 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 323 | if (s->srv) { /* there may be requests left pending in queue */ |
| 324 | if (s->flags & SN_CURR_SESS) { |
| 325 | s->flags &= ~SN_CURR_SESS; |
| 326 | s->srv->cur_sess--; |
| 327 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 328 | if (may_dequeue_tasks(s->srv, s->be)) |
| 329 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 330 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 331 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 332 | if (unlikely(s->srv_conn)) { |
| 333 | /* the session still has a reserved slot on a server, but |
| 334 | * it should normally be only the same as the one above, |
| 335 | * so this should not happen in fact. |
| 336 | */ |
| 337 | sess_change_server(s, NULL); |
| 338 | } |
| 339 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 340 | if (s->req->pipe) |
| 341 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 342 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 343 | if (s->rep->pipe) |
| 344 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 345 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 346 | pool_free2(pool2_buffer, s->req); |
| 347 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 348 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 349 | http_end_txn(s); |
| 350 | |
Willy Tarreau | a4cda67 | 2010-06-06 18:28:49 +0200 | [diff] [blame] | 351 | for (i = 0; i < s->store_count; i++) { |
| 352 | if (!s->store[i].ts) |
| 353 | continue; |
| 354 | stksess_free(s->store[i].table, s->store[i].ts); |
| 355 | s->store[i].ts = NULL; |
| 356 | } |
| 357 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 358 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 359 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 360 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
| 361 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | } |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 363 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 364 | if (s->tracked_counters) |
| 365 | session_store_counters(s); |
| 366 | |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 367 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 368 | /* we have to unlink all watchers. We must not relink them if |
| 369 | * this session was the last one in the list. |
| 370 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 371 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 372 | LIST_INIT(&bref->users); |
| 373 | if (s->list.n != &sessions) |
| 374 | LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 375 | bref->ref = s->list.n; |
| 376 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 377 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 378 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 379 | |
| 380 | /* We may want to free the maximum amount of pools if the proxy is stopping */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 381 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 382 | pool_flush2(pool2_buffer); |
| 383 | pool_flush2(fe->hdr_idx_pool); |
| 384 | pool_flush2(pool2_requri); |
| 385 | pool_flush2(pool2_capture); |
| 386 | pool_flush2(pool2_session); |
| 387 | pool_flush2(fe->req_cap_pool); |
| 388 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 389 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 394 | int init_session() |
| 395 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 396 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 397 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 398 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 401 | void session_process_counters(struct session *s) |
| 402 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 403 | unsigned long long bytes; |
| 404 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 405 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 406 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 407 | s->logs.bytes_in = s->req->total; |
| 408 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 409 | s->fe->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 410 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 411 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 412 | s->be->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 413 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 414 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 415 | s->srv->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 416 | |
| 417 | if (s->listener->counters) |
| 418 | s->listener->counters->bytes_in += bytes; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 419 | |
| 420 | if (s->tracked_counters) { |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 421 | void *ptr; |
| 422 | |
| 423 | ptr = stktable_data_ptr(s->tracked_table, |
| 424 | s->tracked_counters, |
| 425 | STKTABLE_DT_BYTES_IN_CNT); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 426 | if (ptr) |
| 427 | stktable_data_cast(ptr, bytes_in_cnt) += bytes; |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 428 | |
| 429 | ptr = stktable_data_ptr(s->tracked_table, |
| 430 | s->tracked_counters, |
| 431 | STKTABLE_DT_BYTES_IN_RATE); |
| 432 | if (ptr) |
| 433 | update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
| 434 | s->tracked_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 435 | } |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 436 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 439 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 440 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 441 | s->logs.bytes_out = s->rep->total; |
| 442 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 443 | s->fe->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 444 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 445 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 446 | s->be->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 448 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 449 | s->srv->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 450 | |
| 451 | if (s->listener->counters) |
| 452 | s->listener->counters->bytes_out += bytes; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 453 | |
| 454 | if (s->tracked_counters) { |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 455 | void *ptr; |
| 456 | |
| 457 | ptr = stktable_data_ptr(s->tracked_table, |
| 458 | s->tracked_counters, |
| 459 | STKTABLE_DT_BYTES_OUT_CNT); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 460 | if (ptr) |
| 461 | stktable_data_cast(ptr, bytes_out_cnt) += bytes; |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 462 | |
| 463 | ptr = stktable_data_ptr(s->tracked_table, |
| 464 | s->tracked_counters, |
| 465 | STKTABLE_DT_BYTES_OUT_RATE); |
| 466 | if (ptr) |
| 467 | update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate), |
| 468 | s->tracked_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 469 | } |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 470 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 474 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 475 | * connection was attempted and that the file descriptor is already allocated. |
| 476 | * We must check for establishment, error and abort. Possible output states |
| 477 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 478 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 479 | * otherwise 1. |
| 480 | */ |
| 481 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 482 | { |
| 483 | struct buffer *req = si->ob; |
| 484 | struct buffer *rep = si->ib; |
| 485 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 486 | /* If we got an error, or if nothing happened and the connection timed |
| 487 | * out, we must give up. The CER state handler will take care of retry |
| 488 | * attempts and error reports. |
| 489 | */ |
| 490 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 491 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 492 | si->state = SI_ST_CER; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 493 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 494 | fd_delete(si->fd); |
| 495 | |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 496 | if (si->release) |
| 497 | si->release(si); |
| 498 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 499 | if (si->err_type) |
| 500 | return 0; |
| 501 | |
| 502 | si->err_loc = s->srv; |
| 503 | if (si->flags & SI_FL_ERR) |
| 504 | si->err_type = SI_ET_CONN_ERR; |
| 505 | else |
| 506 | si->err_type = SI_ET_CONN_TO; |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* OK, maybe we want to abort */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 511 | if (unlikely((rep->flags & BF_SHUTW) || |
| 512 | ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 513 | (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 514 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 515 | /* give up */ |
| 516 | si->shutw(si); |
| 517 | si->err_type |= SI_ET_CONN_ABRT; |
| 518 | si->err_loc = s->srv; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 519 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 520 | if (s->srv_error) |
| 521 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | /* we need to wait a bit more if there was no activity either */ |
| 526 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 527 | return 1; |
| 528 | |
| 529 | /* OK, this means that a connection succeeded. The caller will be |
| 530 | * responsible for handling the transition from CON to EST. |
| 531 | */ |
| 532 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 533 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 534 | si->state = SI_ST_EST; |
| 535 | si->err_type = SI_ET_NONE; |
| 536 | si->err_loc = NULL; |
| 537 | return 1; |
| 538 | } |
| 539 | |
| 540 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 541 | * previous connection attempt has failed and that the file descriptor |
| 542 | * has already been released. Possible causes include asynchronous error |
| 543 | * notification and time out. Possible output states are SI_ST_CLO when |
| 544 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 545 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 546 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 547 | * marked as in error state. It returns 0. |
| 548 | */ |
| 549 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 550 | { |
| 551 | /* we probably have to release last session from the server */ |
| 552 | if (s->srv) { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 553 | health_adjust(s->srv, HANA_STATUS_L4_ERR); |
| 554 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 555 | if (s->flags & SN_CURR_SESS) { |
| 556 | s->flags &= ~SN_CURR_SESS; |
| 557 | s->srv->cur_sess--; |
| 558 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* ensure that we have enough retries left */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 562 | si->conn_retries--; |
| 563 | if (si->conn_retries < 0) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 564 | if (!si->err_type) { |
| 565 | si->err_type = SI_ET_CONN_ERR; |
| 566 | si->err_loc = s->srv; |
| 567 | } |
| 568 | |
| 569 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 570 | s->srv->counters.failed_conns++; |
| 571 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 572 | if (may_dequeue_tasks(s->srv, s->be)) |
| 573 | process_srv_queue(s->srv); |
| 574 | |
| 575 | /* shutw is enough so stop a connecting socket */ |
| 576 | si->shutw(si); |
| 577 | si->ob->flags |= BF_WRITE_ERROR; |
| 578 | si->ib->flags |= BF_READ_ERROR; |
| 579 | |
| 580 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 581 | if (s->srv_error) |
| 582 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 587 | * retry on another server for the last retry. In order to achieve this, |
| 588 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 589 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 590 | * redispatch yet, because this will depend on what server is selected. |
| 591 | */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 592 | if (s->srv && si->conn_retries == 0 && |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 593 | s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 594 | if (may_dequeue_tasks(s->srv, s->be)) |
| 595 | process_srv_queue(s->srv); |
| 596 | |
| 597 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 598 | s->prev_srv = s->srv; |
| 599 | si->state = SI_ST_REQ; |
| 600 | } else { |
| 601 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 602 | s->srv->counters.retries++; |
| 603 | s->be->counters.retries++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 604 | si->state = SI_ST_ASS; |
| 605 | } |
| 606 | |
| 607 | if (si->flags & SI_FL_ERR) { |
| 608 | /* The error was an asynchronous connection error, and we will |
| 609 | * likely have to retry connecting to the same server, most |
| 610 | * likely leading to the same result. To avoid this, we wait |
| 611 | * one second before retrying. |
| 612 | */ |
| 613 | |
| 614 | if (!si->err_type) |
| 615 | si->err_type = SI_ET_CONN_ERR; |
| 616 | |
| 617 | si->state = SI_ST_TAR; |
| 618 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 619 | return 0; |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * This function handles the transition between the SI_ST_CON state and the |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 626 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or |
| 627 | * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined. |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 628 | */ |
| 629 | void sess_establish(struct session *s, struct stream_interface *si) |
| 630 | { |
| 631 | struct buffer *req = si->ob; |
| 632 | struct buffer *rep = si->ib; |
| 633 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 634 | if (s->srv) |
| 635 | health_adjust(s->srv, HANA_STATUS_L4_OK); |
| 636 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 637 | if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 638 | /* if the user wants to log as soon as possible, without counting |
| 639 | * bytes from the server, then this is the right moment. */ |
| 640 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 641 | s->logs.t_close = s->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 642 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 643 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 644 | } |
| 645 | else { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 646 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 647 | /* reset hdr_idx which was already initialized by the request. |
| 648 | * right now, the http parser does it. |
| 649 | * hdr_idx_init(&s->txn.hdr_idx); |
| 650 | */ |
| 651 | } |
| 652 | |
Willy Tarreau | 4e5b828 | 2009-08-16 22:57:50 +0200 | [diff] [blame] | 653 | rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 654 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 655 | if (si->connect) { |
| 656 | /* real connections have timeouts */ |
| 657 | req->wto = s->be->timeout.server; |
| 658 | rep->rto = s->be->timeout.server; |
| 659 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 660 | req->wex = TICK_ETERNITY; |
| 661 | } |
| 662 | |
| 663 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 664 | * Other input states are simply ignored. |
| 665 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 666 | * Flags must have previously been updated for timeouts and other conditions. |
| 667 | */ |
| 668 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 669 | { |
| 670 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 671 | now_ms, __FUNCTION__, |
| 672 | s, |
| 673 | s->req, s->rep, |
| 674 | s->req->rex, s->rep->wex, |
| 675 | s->req->flags, s->rep->flags, |
| 676 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 677 | |
| 678 | if (si->state == SI_ST_ASS) { |
| 679 | /* Server assigned to connection request, we have to try to connect now */ |
| 680 | int conn_err; |
| 681 | |
| 682 | conn_err = connect_server(s); |
| 683 | if (conn_err == SN_ERR_NONE) { |
| 684 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 685 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 686 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | |
| 690 | /* We have received a synchronous error. We might have to |
| 691 | * abort, retry immediately or redispatch. |
| 692 | */ |
| 693 | if (conn_err == SN_ERR_INTERNAL) { |
| 694 | if (!si->err_type) { |
| 695 | si->err_type = SI_ET_CONN_OTHER; |
| 696 | si->err_loc = s->srv; |
| 697 | } |
| 698 | |
| 699 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 700 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 701 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 702 | s->srv->counters.failed_conns++; |
| 703 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 704 | |
| 705 | /* release other sessions waiting for this server */ |
| 706 | if (may_dequeue_tasks(s->srv, s->be)) |
| 707 | process_srv_queue(s->srv); |
| 708 | |
| 709 | /* Failed and not retryable. */ |
| 710 | si->shutr(si); |
| 711 | si->shutw(si); |
| 712 | si->ob->flags |= BF_WRITE_ERROR; |
| 713 | |
| 714 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 715 | |
| 716 | /* no session was ever accounted for this server */ |
| 717 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 718 | if (s->srv_error) |
| 719 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
| 723 | /* We are facing a retryable error, but we don't want to run a |
| 724 | * turn-around now, as the problem is likely a source port |
| 725 | * allocation problem, so we want to retry now. |
| 726 | */ |
| 727 | si->state = SI_ST_CER; |
| 728 | si->flags &= ~SI_FL_ERR; |
| 729 | sess_update_st_cer(s, si); |
| 730 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 731 | return; |
| 732 | } |
| 733 | else if (si->state == SI_ST_QUE) { |
| 734 | /* connection request was queued, check for any update */ |
| 735 | if (!s->pend_pos) { |
| 736 | /* The connection is not in the queue anymore. Either |
| 737 | * we have a server connection slot available and we |
| 738 | * go directly to the assigned state, or we need to |
| 739 | * load-balance first and go to the INI state. |
| 740 | */ |
| 741 | si->exp = TICK_ETERNITY; |
| 742 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 743 | si->state = SI_ST_REQ; |
| 744 | else { |
| 745 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 746 | si->state = SI_ST_ASS; |
| 747 | } |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | /* Connection request still in queue... */ |
| 752 | if (si->flags & SI_FL_EXP) { |
| 753 | /* ... and timeout expired */ |
| 754 | si->exp = TICK_ETERNITY; |
| 755 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 756 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 757 | s->srv->counters.failed_conns++; |
| 758 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 759 | si->shutr(si); |
| 760 | si->shutw(si); |
| 761 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 762 | if (!si->err_type) |
| 763 | si->err_type = SI_ET_QUEUE_TO; |
| 764 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 765 | if (s->srv_error) |
| 766 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
| 770 | /* Connection remains in queue, check if we have to abort it */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 771 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 772 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 773 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 774 | /* give up */ |
| 775 | si->exp = TICK_ETERNITY; |
| 776 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 777 | si->shutr(si); |
| 778 | si->shutw(si); |
| 779 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 780 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 781 | if (s->srv_error) |
| 782 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
| 786 | /* Nothing changed */ |
| 787 | return; |
| 788 | } |
| 789 | else if (si->state == SI_ST_TAR) { |
| 790 | /* Connection request might be aborted */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 791 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 792 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 793 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 794 | /* give up */ |
| 795 | si->exp = TICK_ETERNITY; |
| 796 | si->shutr(si); |
| 797 | si->shutw(si); |
| 798 | si->err_type |= SI_ET_CONN_ABRT; |
| 799 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 800 | if (s->srv_error) |
| 801 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 802 | return; |
| 803 | } |
| 804 | |
| 805 | if (!(si->flags & SI_FL_EXP)) |
| 806 | return; /* still in turn-around */ |
| 807 | |
| 808 | si->exp = TICK_ETERNITY; |
| 809 | |
| 810 | /* we keep trying on the same server as long as the session is |
| 811 | * marked "assigned". |
| 812 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 813 | */ |
| 814 | if (s->flags & SN_ASSIGNED) |
| 815 | si->state = SI_ST_ASS; |
| 816 | else |
| 817 | si->state = SI_ST_REQ; |
| 818 | return; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | /* This function initiates a server connection request on a stream interface |
| 823 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 824 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 825 | * or SI_ST_CLO upon error. |
| 826 | */ |
| 827 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 828 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 829 | now_ms, __FUNCTION__, |
| 830 | s, |
| 831 | s->req, s->rep, |
| 832 | s->req->rex, s->rep->wex, |
| 833 | s->req->flags, s->rep->flags, |
| 834 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 835 | |
| 836 | if (si->state != SI_ST_REQ) |
| 837 | return; |
| 838 | |
| 839 | /* Try to assign a server */ |
| 840 | if (srv_redispatch_connect(s) != 0) { |
| 841 | /* We did not get a server. Either we queued the |
| 842 | * connection request, or we encountered an error. |
| 843 | */ |
| 844 | if (si->state == SI_ST_QUE) |
| 845 | return; |
| 846 | |
| 847 | /* we did not get any server, let's check the cause */ |
| 848 | si->shutr(si); |
| 849 | si->shutw(si); |
| 850 | si->ob->flags |= BF_WRITE_ERROR; |
| 851 | if (!si->err_type) |
| 852 | si->err_type = SI_ET_CONN_OTHER; |
| 853 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 854 | if (s->srv_error) |
| 855 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
| 859 | /* The server is assigned */ |
| 860 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 861 | si->state = SI_ST_ASS; |
| 862 | } |
| 863 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 864 | /* This stream analyser checks the switching rules and changes the backend |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 865 | * if appropriate. The default_backend rule is also considered, then the |
| 866 | * target backend's forced persistence rules are also evaluated last if any. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 867 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 868 | * either needs more data or wants to immediately abort the request. |
| 869 | */ |
| 870 | int process_switching_rules(struct session *s, struct buffer *req, int an_bit) |
| 871 | { |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 872 | struct persist_rule *prst_rule; |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 873 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 874 | req->analysers &= ~an_bit; |
| 875 | req->analyse_exp = TICK_ETERNITY; |
| 876 | |
| 877 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 878 | now_ms, __FUNCTION__, |
| 879 | s, |
| 880 | req, |
| 881 | req->rex, req->wex, |
| 882 | req->flags, |
| 883 | req->l, |
| 884 | req->analysers); |
| 885 | |
| 886 | /* now check whether we have some switching rules for this request */ |
| 887 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 888 | struct switching_rule *rule; |
| 889 | |
| 890 | list_for_each_entry(rule, &s->fe->switching_rules, list) { |
| 891 | int ret; |
| 892 | |
| 893 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ); |
| 894 | ret = acl_pass(ret); |
| 895 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 896 | ret = !ret; |
| 897 | |
| 898 | if (ret) { |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 899 | if (!session_set_backend(s, rule->be.backend)) |
| 900 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 901 | break; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /* To ensure correct connection accounting on the backend, we |
| 906 | * have to assign one if it was not set (eg: a listen). This |
| 907 | * measure also takes care of correctly setting the default |
| 908 | * backend if any. |
| 909 | */ |
| 910 | if (!(s->flags & SN_BE_ASSIGNED)) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 911 | if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be)) |
| 912 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 915 | /* we don't want to run the TCP or HTTP filters again if the backend has not changed */ |
| 916 | if (s->fe == s->be) { |
| 917 | s->req->analysers &= ~AN_REQ_INSPECT_BE; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 918 | s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE; |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 919 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 920 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 921 | /* as soon as we know the backend, we must check if we have a matching forced or ignored |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 922 | * persistence rule, and report that in the session. |
| 923 | */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 924 | list_for_each_entry(prst_rule, &s->be->persist_rules, list) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 925 | int ret = 1; |
| 926 | |
| 927 | if (prst_rule->cond) { |
| 928 | ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 929 | ret = acl_pass(ret); |
| 930 | if (prst_rule->cond->pol == ACL_COND_UNLESS) |
| 931 | ret = !ret; |
| 932 | } |
| 933 | |
| 934 | if (ret) { |
| 935 | /* no rule, or the rule matches */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 936 | if (prst_rule->type == PERSIST_TYPE_FORCE) { |
| 937 | s->flags |= SN_FORCE_PRST; |
| 938 | } else { |
| 939 | s->flags |= SN_IGNORE_PRST; |
| 940 | } |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 945 | return 1; |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 946 | |
| 947 | sw_failed: |
| 948 | /* immediately abort this request in case of allocation failure */ |
| 949 | buffer_abort(s->req); |
| 950 | buffer_abort(s->rep); |
| 951 | |
| 952 | if (!(s->flags & SN_ERR_MASK)) |
| 953 | s->flags |= SN_ERR_RESOURCE; |
| 954 | if (!(s->flags & SN_FINST_MASK)) |
| 955 | s->flags |= SN_FINST_R; |
| 956 | |
| 957 | s->txn.status = 500; |
| 958 | s->req->analysers = 0; |
| 959 | s->req->analyse_exp = TICK_ETERNITY; |
| 960 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 963 | /* This stream analyser works on a request. It applies all sticking rules on |
| 964 | * it then returns 1. The data must already be present in the buffer otherwise |
| 965 | * they won't match. It always returns 1. |
| 966 | */ |
| 967 | int process_sticking_rules(struct session *s, struct buffer *req, int an_bit) |
| 968 | { |
| 969 | struct proxy *px = s->be; |
| 970 | struct sticking_rule *rule; |
| 971 | |
| 972 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 973 | now_ms, __FUNCTION__, |
| 974 | s, |
| 975 | req, |
| 976 | req->rex, req->wex, |
| 977 | req->flags, |
| 978 | req->l, |
| 979 | req->analysers); |
| 980 | |
| 981 | list_for_each_entry(rule, &px->sticking_rules, list) { |
| 982 | int ret = 1 ; |
| 983 | int i; |
| 984 | |
| 985 | for (i = 0; i < s->store_count; i++) { |
| 986 | if (rule->table.t == s->store[i].table) |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | if (i != s->store_count) |
| 991 | continue; |
| 992 | |
| 993 | if (rule->cond) { |
| 994 | ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ); |
| 995 | ret = acl_pass(ret); |
| 996 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 997 | ret = !ret; |
| 998 | } |
| 999 | |
| 1000 | if (ret) { |
| 1001 | struct stktable_key *key; |
| 1002 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1003 | key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1004 | if (!key) |
| 1005 | continue; |
| 1006 | |
| 1007 | if (rule->flags & STK_IS_MATCH) { |
| 1008 | struct stksess *ts; |
| 1009 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 1010 | if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) { |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1011 | if (!(s->flags & SN_ASSIGNED)) { |
| 1012 | struct eb32_node *node; |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 1013 | void *ptr; |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1014 | |
| 1015 | /* srv found in table */ |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 1016 | ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID); |
| 1017 | node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id)); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1018 | if (node) { |
| 1019 | struct server *srv; |
| 1020 | |
| 1021 | srv = container_of(node, struct server, conf.id); |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 1022 | if ((srv->state & SRV_RUNNING) || |
| 1023 | (px->options & PR_O_PERSIST) || |
| 1024 | (s->flags & SN_FORCE_PRST)) { |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1025 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 1026 | s->srv = srv; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
Willy Tarreau | e8f6338 | 2010-07-13 15:20:24 +0200 | [diff] [blame] | 1030 | stktable_touch(rule->table.t, ts); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | if (rule->flags & STK_IS_STORE) { |
| 1034 | if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) { |
| 1035 | struct stksess *ts; |
| 1036 | |
| 1037 | ts = stksess_new(rule->table.t, key); |
| 1038 | if (ts) { |
| 1039 | s->store[s->store_count].table = rule->table.t; |
| 1040 | s->store[s->store_count++].ts = ts; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | req->analysers &= ~an_bit; |
| 1048 | req->analyse_exp = TICK_ETERNITY; |
| 1049 | return 1; |
| 1050 | } |
| 1051 | |
| 1052 | /* This stream analyser works on a response. It applies all store rules on it |
| 1053 | * then returns 1. The data must already be present in the buffer otherwise |
| 1054 | * they won't match. It always returns 1. |
| 1055 | */ |
| 1056 | int process_store_rules(struct session *s, struct buffer *rep, int an_bit) |
| 1057 | { |
| 1058 | struct proxy *px = s->be; |
| 1059 | struct sticking_rule *rule; |
| 1060 | int i; |
| 1061 | |
| 1062 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 1063 | now_ms, __FUNCTION__, |
| 1064 | s, |
Willy Tarreau | 2e2b3eb | 2010-02-09 20:55:44 +0100 | [diff] [blame] | 1065 | rep, |
| 1066 | rep->rex, rep->wex, |
| 1067 | rep->flags, |
| 1068 | rep->l, |
| 1069 | rep->analysers); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1070 | |
| 1071 | list_for_each_entry(rule, &px->storersp_rules, list) { |
| 1072 | int ret = 1 ; |
| 1073 | int storereqidx = -1; |
| 1074 | |
| 1075 | for (i = 0; i < s->store_count; i++) { |
| 1076 | if (rule->table.t == s->store[i].table) { |
| 1077 | if (!(s->store[i].flags)) |
| 1078 | storereqidx = i; |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if ((i != s->store_count) && (storereqidx == -1)) |
| 1084 | continue; |
| 1085 | |
| 1086 | if (rule->cond) { |
| 1087 | ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR); |
| 1088 | ret = acl_pass(ret); |
| 1089 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1090 | ret = !ret; |
| 1091 | } |
| 1092 | |
| 1093 | if (ret) { |
| 1094 | struct stktable_key *key; |
| 1095 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1096 | key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1097 | if (!key) |
| 1098 | continue; |
| 1099 | |
| 1100 | if (storereqidx != -1) { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 1101 | stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1102 | s->store[storereqidx].flags = 1; |
| 1103 | } |
| 1104 | else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) { |
| 1105 | struct stksess *ts; |
| 1106 | |
| 1107 | ts = stksess_new(rule->table.t, key); |
| 1108 | if (ts) { |
| 1109 | s->store[s->store_count].table = rule->table.t; |
| 1110 | s->store[s->store_count].flags = 1; |
| 1111 | s->store[s->store_count++].ts = ts; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /* process store request and store response */ |
| 1118 | for (i = 0; i < s->store_count; i++) { |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 1119 | struct stksess *ts; |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 1120 | void *ptr; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 1121 | |
| 1122 | ts = stktable_lookup(s->store[i].table, s->store[i].ts); |
| 1123 | if (ts) { |
| 1124 | /* the entry already existed, we can free ours */ |
Willy Tarreau | e8f6338 | 2010-07-13 15:20:24 +0200 | [diff] [blame] | 1125 | stktable_touch(s->store[i].table, ts); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1126 | stksess_free(s->store[i].table, s->store[i].ts); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1127 | } |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 1128 | else |
| 1129 | ts = stktable_store(s->store[i].table, s->store[i].ts); |
| 1130 | |
| 1131 | s->store[i].ts = NULL; |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 1132 | ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID); |
| 1133 | stktable_data_cast(ptr, server_id) = s->srv->puid; |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1134 | } |
Willy Tarreau | 2a164ee | 2010-06-18 09:57:45 +0200 | [diff] [blame] | 1135 | s->store_count = 0; /* everything is stored */ |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1136 | |
| 1137 | rep->analysers &= ~an_bit; |
| 1138 | rep->analyse_exp = TICK_ETERNITY; |
| 1139 | return 1; |
| 1140 | } |
| 1141 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1142 | /* This macro is very specific to the function below. See the comments in |
| 1143 | * process_session() below to understand the logic and the tests. |
| 1144 | */ |
| 1145 | #define UPDATE_ANALYSERS(real, list, back, flag) { \ |
| 1146 | list = (((list) & ~(flag)) | ~(back)) & (real); \ |
| 1147 | back = real; \ |
| 1148 | if (!(list)) \ |
| 1149 | break; \ |
| 1150 | if (((list) ^ ((list) & ((list) - 1))) < (flag)) \ |
| 1151 | continue; \ |
| 1152 | } |
| 1153 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1154 | /* Processes the client, server, request and response jobs of a session task, |
| 1155 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 1156 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 1157 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 1158 | * nothing too many times, the request and response buffers flags are monitored |
| 1159 | * and each function is called only if at least another function has changed at |
| 1160 | * least one flag it is interested in. |
| 1161 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1162 | struct task *process_session(struct task *t) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1163 | { |
| 1164 | struct session *s = t->context; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1165 | unsigned int rqf_last, rpf_last; |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1166 | unsigned int rq_prod_last, rq_cons_last; |
| 1167 | unsigned int rp_cons_last, rp_prod_last; |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1168 | unsigned int req_ana_back; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1169 | |
| 1170 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 1171 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 1172 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1173 | /* this data may be no longer valid, clear it */ |
| 1174 | memset(&s->txn.auth, 0, sizeof(s->txn.auth)); |
| 1175 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1176 | /* This flag must explicitly be set every time */ |
| 1177 | s->req->flags &= ~BF_READ_NOEXP; |
| 1178 | |
| 1179 | /* Keep a copy of req/rep flags so that we can detect shutdowns */ |
| 1180 | rqf_last = s->req->flags; |
| 1181 | rpf_last = s->rep->flags; |
| 1182 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1183 | /* we don't want the stream interface functions to recursively wake us up */ |
| 1184 | if (s->req->prod->owner == t) |
| 1185 | s->req->prod->flags |= SI_FL_DONT_WAKE; |
| 1186 | if (s->req->cons->owner == t) |
| 1187 | s->req->cons->flags |= SI_FL_DONT_WAKE; |
| 1188 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1189 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 1190 | * stream interfaces when their timeouts have expired. |
| 1191 | */ |
| 1192 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 1193 | stream_int_check_timeouts(&s->si[0]); |
| 1194 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1195 | |
| 1196 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 1197 | * for future reads or writes. Note: this will also concern upper layers |
| 1198 | * but we do not touch any other flag. We must be careful and correctly |
| 1199 | * detect state changes when calling them. |
| 1200 | */ |
| 1201 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1202 | buffer_check_timeouts(s->req); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1203 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 1204 | if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 1205 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 1206 | s->req->cons->shutw(s->req->cons); |
| 1207 | } |
| 1208 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1209 | if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 1210 | s->req->prod->shutr(s->req->prod); |
| 1211 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1212 | buffer_check_timeouts(s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1213 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 1214 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 1215 | s->rep->cons->flags |= SI_FL_NOLINGER; |
| 1216 | s->rep->cons->shutw(s->rep->cons); |
| 1217 | } |
| 1218 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1219 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 1220 | s->rep->prod->shutr(s->rep->prod); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1221 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1222 | |
| 1223 | /* 1b: check for low-level errors reported at the stream interface. |
| 1224 | * First we check if it's a retryable error (in which case we don't |
| 1225 | * want to tell the buffer). Otherwise we report the error one level |
| 1226 | * upper by setting flags into the buffers. Note that the side towards |
| 1227 | * the client cannot have connect (hence retryable) errors. Also, the |
| 1228 | * connection setup code must be able to deal with any type of abort. |
| 1229 | */ |
| 1230 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 1231 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 1232 | s->si[0].shutr(&s->si[0]); |
| 1233 | s->si[0].shutw(&s->si[0]); |
| 1234 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1235 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1236 | s->be->counters.cli_aborts++; |
| 1237 | if (s->srv) |
| 1238 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1239 | if (!(s->flags & SN_ERR_MASK)) |
| 1240 | s->flags |= SN_ERR_CLICL; |
| 1241 | if (!(s->flags & SN_FINST_MASK)) |
| 1242 | s->flags |= SN_FINST_D; |
| 1243 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 1248 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 1249 | s->si[1].shutr(&s->si[1]); |
| 1250 | s->si[1].shutw(&s->si[1]); |
| 1251 | stream_int_report_error(&s->si[1]); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1252 | s->be->counters.failed_resp++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1253 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1254 | s->srv->counters.failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1255 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1256 | s->be->counters.srv_aborts++; |
| 1257 | if (s->srv) |
| 1258 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1259 | if (!(s->flags & SN_ERR_MASK)) |
| 1260 | s->flags |= SN_ERR_SRVCL; |
| 1261 | if (!(s->flags & SN_FINST_MASK)) |
| 1262 | s->flags |= SN_FINST_D; |
| 1263 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1264 | } |
| 1265 | /* note: maybe we should process connection errors here ? */ |
| 1266 | } |
| 1267 | |
| 1268 | if (s->si[1].state == SI_ST_CON) { |
| 1269 | /* we were trying to establish a connection on the server side, |
| 1270 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 1271 | */ |
| 1272 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 1273 | sess_update_st_cer(s, &s->si[1]); |
| 1274 | else if (s->si[1].state == SI_ST_EST) |
| 1275 | sess_establish(s, &s->si[1]); |
| 1276 | |
| 1277 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 1278 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 1279 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 1280 | */ |
| 1281 | } |
| 1282 | |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1283 | rq_prod_last = s->si[0].state; |
| 1284 | rq_cons_last = s->si[1].state; |
| 1285 | rp_cons_last = s->si[0].state; |
| 1286 | rp_prod_last = s->si[1].state; |
| 1287 | |
| 1288 | resync_stream_interface: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1289 | /* Check for connection closure */ |
| 1290 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1291 | DPRINTF(stderr, |
| 1292 | "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n", |
| 1293 | now_ms, __FUNCTION__, __LINE__, |
| 1294 | t, |
| 1295 | s, s->flags, |
| 1296 | s->req, s->rep, |
| 1297 | s->req->rex, s->rep->wex, |
| 1298 | s->req->flags, s->rep->flags, |
| 1299 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 1300 | s->rep->cons->err_type, s->req->cons->err_type, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1301 | s->req->cons->conn_retries); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* nothing special to be done on client side */ |
| 1304 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 1305 | s->req->prod->state = SI_ST_CLO; |
| 1306 | |
| 1307 | /* When a server-side connection is released, we have to count it and |
| 1308 | * check for pending connections on this server. |
| 1309 | */ |
| 1310 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 1311 | s->req->cons->state = SI_ST_CLO; |
| 1312 | if (s->srv) { |
| 1313 | if (s->flags & SN_CURR_SESS) { |
| 1314 | s->flags &= ~SN_CURR_SESS; |
| 1315 | s->srv->cur_sess--; |
| 1316 | } |
| 1317 | sess_change_server(s, NULL); |
| 1318 | if (may_dequeue_tasks(s->srv, s->be)) |
| 1319 | process_srv_queue(s->srv); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 1325 | * at this point. |
| 1326 | */ |
| 1327 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1328 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1329 | /* Analyse request */ |
| 1330 | if ((s->req->flags & BF_MASK_ANALYSER) || |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1331 | ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) || |
| 1332 | s->si[0].state != rq_prod_last || |
| 1333 | s->si[1].state != rq_cons_last) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1334 | unsigned int flags = s->req->flags; |
| 1335 | |
| 1336 | if (s->req->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1337 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1338 | unsigned int ana_list; |
| 1339 | unsigned int ana_back; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1340 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1341 | /* it's up to the analysers to stop new connections, |
| 1342 | * disable reading or closing. Note: if an analyser |
| 1343 | * disables any of these bits, it is responsible for |
| 1344 | * enabling them again when it disables itself, so |
| 1345 | * that other analysers are called in similar conditions. |
| 1346 | */ |
| 1347 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1348 | buffer_auto_connect(s->req); |
| 1349 | buffer_auto_close(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1350 | |
| 1351 | /* We will call all analysers for which a bit is set in |
| 1352 | * s->req->analysers, following the bit order from LSB |
| 1353 | * to MSB. The analysers must remove themselves from |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1354 | * the list when not needed. Any analyser may return 0 |
| 1355 | * to break out of the loop, either because of missing |
| 1356 | * data to take a decision, or because it decides to |
| 1357 | * kill the session. We loop at least once through each |
| 1358 | * analyser, and we may loop again if other analysers |
| 1359 | * are added in the middle. |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1360 | * |
| 1361 | * We build a list of analysers to run. We evaluate all |
| 1362 | * of these analysers in the order of the lower bit to |
| 1363 | * the higher bit. This ordering is very important. |
| 1364 | * An analyser will often add/remove other analysers, |
| 1365 | * including itself. Any changes to itself have no effect |
| 1366 | * on the loop. If it removes any other analysers, we |
| 1367 | * want those analysers not to be called anymore during |
| 1368 | * this loop. If it adds an analyser that is located |
| 1369 | * after itself, we want it to be scheduled for being |
| 1370 | * processed during the loop. If it adds an analyser |
| 1371 | * which is located before it, we want it to switch to |
| 1372 | * it immediately, even if it has already been called |
| 1373 | * once but removed since. |
| 1374 | * |
| 1375 | * In order to achieve this, we compare the analyser |
| 1376 | * list after the call with a copy of it before the |
| 1377 | * call. The work list is fed with analyser bits that |
| 1378 | * appeared during the call. Then we compare previous |
| 1379 | * work list with the new one, and check the bits that |
| 1380 | * appeared. If the lowest of these bits is lower than |
| 1381 | * the current bit, it means we have enabled a previous |
| 1382 | * analyser and must immediately loop again. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1383 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1384 | |
| 1385 | ana_list = ana_back = s->req->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1386 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1387 | /* Warning! ensure that analysers are always placed in ascending order! */ |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1388 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 1389 | if (ana_list & AN_REQ_INSPECT_FE) { |
| 1390 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1391 | break; |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 1392 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1393 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1395 | if (ana_list & AN_REQ_WAIT_HTTP) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1396 | if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP)) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1397 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1398 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1401 | if (ana_list & AN_REQ_HTTP_PROCESS_FE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1402 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe)) |
| 1403 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1404 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1407 | if (ana_list & AN_REQ_SWITCHING_RULES) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1408 | if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES)) |
| 1409 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1410 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 1413 | if (ana_list & AN_REQ_INSPECT_BE) { |
| 1414 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE)) |
| 1415 | break; |
| 1416 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE); |
| 1417 | } |
| 1418 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1419 | if (ana_list & AN_REQ_HTTP_PROCESS_BE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1420 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be)) |
| 1421 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1422 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1425 | if (ana_list & AN_REQ_HTTP_TARPIT) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1426 | if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT)) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 1427 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1428 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1429 | } |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1431 | if (ana_list & AN_REQ_HTTP_INNER) { |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 1432 | if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER)) |
| 1433 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1434 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1437 | if (ana_list & AN_REQ_HTTP_BODY) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1438 | if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY)) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 1439 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1440 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1441 | } |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1442 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1443 | if (ana_list & AN_REQ_PRST_RDP_COOKIE) { |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1444 | if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE)) |
| 1445 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1446 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE); |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1447 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1448 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1449 | if (ana_list & AN_REQ_STICKING_RULES) { |
| 1450 | if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES)) |
| 1451 | break; |
| 1452 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES); |
| 1453 | } |
| 1454 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1455 | if (ana_list & AN_REQ_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1456 | if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY)) |
| 1457 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1458 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1459 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1460 | break; |
| 1461 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1462 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1464 | rq_prod_last = s->si[0].state; |
| 1465 | rq_cons_last = s->si[1].state; |
| 1466 | rqf_last = s->req->flags; |
| 1467 | |
| 1468 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1469 | goto resync_request; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1472 | /* we'll monitor the request analysers while parsing the response, |
| 1473 | * because some response analysers may indirectly enable new request |
| 1474 | * analysers (eg: HTTP keep-alive). |
| 1475 | */ |
| 1476 | req_ana_back = s->req->analysers; |
| 1477 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1478 | resync_response: |
| 1479 | /* Analyse response */ |
| 1480 | |
| 1481 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 1482 | /* In inject mode, we wake up everytime something has |
| 1483 | * happened on the write side of the buffer. |
| 1484 | */ |
| 1485 | unsigned int flags = s->rep->flags; |
| 1486 | |
| 1487 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 1488 | !(s->rep->flags & BF_FULL)) { |
| 1489 | s->rep->hijacker(s, s->rep); |
| 1490 | } |
| 1491 | |
| 1492 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 1493 | rpf_last = s->rep->flags; |
| 1494 | goto resync_response; |
| 1495 | } |
| 1496 | } |
| 1497 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1498 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC || |
| 1499 | s->si[0].state != rp_cons_last || |
| 1500 | s->si[1].state != rp_prod_last) { |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1501 | unsigned int flags = s->rep->flags; |
| 1502 | |
| 1503 | if (s->rep->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1504 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1505 | unsigned int ana_list; |
| 1506 | unsigned int ana_back; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1507 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1508 | /* it's up to the analysers to stop disable reading or |
| 1509 | * closing. Note: if an analyser disables any of these |
| 1510 | * bits, it is responsible for enabling them again when |
| 1511 | * it disables itself, so that other analysers are called |
| 1512 | * in similar conditions. |
| 1513 | */ |
| 1514 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1515 | buffer_auto_close(s->rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1516 | |
| 1517 | /* We will call all analysers for which a bit is set in |
| 1518 | * s->rep->analysers, following the bit order from LSB |
| 1519 | * to MSB. The analysers must remove themselves from |
| 1520 | * the list when not needed. Any analyser may return 0 |
| 1521 | * to break out of the loop, either because of missing |
| 1522 | * data to take a decision, or because it decides to |
| 1523 | * kill the session. We loop at least once through each |
| 1524 | * analyser, and we may loop again if other analysers |
| 1525 | * are added in the middle. |
| 1526 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1527 | |
| 1528 | ana_list = ana_back = s->rep->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1529 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1530 | /* Warning! ensure that analysers are always placed in ascending order! */ |
| 1531 | |
| 1532 | if (ana_list & AN_RES_WAIT_HTTP) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1533 | if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP)) |
| 1534 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1535 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1538 | if (ana_list & AN_RES_STORE_RULES) { |
| 1539 | if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES)) |
| 1540 | break; |
| 1541 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES); |
| 1542 | } |
| 1543 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1544 | if (ana_list & AN_RES_HTTP_PROCESS_BE) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1545 | if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be)) |
| 1546 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1547 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1548 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1550 | if (ana_list & AN_RES_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1551 | if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY)) |
| 1552 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1553 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1554 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1555 | break; |
| 1556 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1557 | } |
| 1558 | |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1559 | rp_cons_last = s->si[0].state; |
| 1560 | rp_prod_last = s->si[1].state; |
| 1561 | rpf_last = s->rep->flags; |
| 1562 | |
| 1563 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1564 | goto resync_response; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1567 | /* maybe someone has added some request analysers, so we must check and loop */ |
| 1568 | if (s->req->analysers & ~req_ana_back) |
| 1569 | goto resync_request; |
| 1570 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1571 | /* FIXME: here we should call protocol handlers which rely on |
| 1572 | * both buffers. |
| 1573 | */ |
| 1574 | |
| 1575 | |
| 1576 | /* |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1577 | * Now we propagate unhandled errors to the session. Normally |
| 1578 | * we're just in a data phase here since it means we have not |
| 1579 | * seen any analyser who could set an error status. |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1580 | */ |
| 1581 | if (!(s->flags & SN_ERR_MASK)) { |
| 1582 | if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1583 | /* Report it if the client got an error or a read timeout expired */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1584 | s->req->analysers = 0; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1585 | if (s->req->flags & BF_READ_ERROR) { |
| 1586 | s->be->counters.cli_aborts++; |
| 1587 | if (s->srv) |
| 1588 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1589 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1590 | } |
| 1591 | else if (s->req->flags & BF_READ_TIMEOUT) { |
| 1592 | s->be->counters.cli_aborts++; |
| 1593 | if (s->srv) |
| 1594 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1595 | s->flags |= SN_ERR_CLITO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1596 | } |
| 1597 | else if (s->req->flags & BF_WRITE_ERROR) { |
| 1598 | s->be->counters.srv_aborts++; |
| 1599 | if (s->srv) |
| 1600 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1601 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1602 | } |
| 1603 | else { |
| 1604 | s->be->counters.srv_aborts++; |
| 1605 | if (s->srv) |
| 1606 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1607 | s->flags |= SN_ERR_SRVTO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1608 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1609 | sess_set_term_flags(s); |
| 1610 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1611 | else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1612 | /* Report it if the server got an error or a read timeout expired */ |
| 1613 | s->rep->analysers = 0; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1614 | if (s->rep->flags & BF_READ_ERROR) { |
| 1615 | s->be->counters.srv_aborts++; |
| 1616 | if (s->srv) |
| 1617 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1618 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1619 | } |
| 1620 | else if (s->rep->flags & BF_READ_TIMEOUT) { |
| 1621 | s->be->counters.srv_aborts++; |
| 1622 | if (s->srv) |
| 1623 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1624 | s->flags |= SN_ERR_SRVTO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1625 | } |
| 1626 | else if (s->rep->flags & BF_WRITE_ERROR) { |
| 1627 | s->be->counters.cli_aborts++; |
| 1628 | if (s->srv) |
| 1629 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1630 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1631 | } |
| 1632 | else { |
| 1633 | s->be->counters.cli_aborts++; |
| 1634 | if (s->srv) |
| 1635 | s->srv->counters.cli_aborts++; |
| 1636 | s->flags |= SN_ERR_CLITO; |
| 1637 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1638 | sess_set_term_flags(s); |
| 1639 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1642 | /* |
| 1643 | * Here we take care of forwarding unhandled data. This also includes |
| 1644 | * connection establishments and shutdown requests. |
| 1645 | */ |
| 1646 | |
| 1647 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1648 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1649 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1650 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1651 | if (!s->req->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1652 | !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1653 | (s->req->prod->state >= SI_ST_EST) && |
| 1654 | (s->req->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1655 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1656 | * attached to it. If any data are left in, we'll permit them to |
| 1657 | * move. |
| 1658 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1659 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1660 | buffer_auto_connect(s->req); |
| 1661 | buffer_auto_close(s->req); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1662 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 1663 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1664 | /* If the producer is still connected, we'll enable data to flow |
| 1665 | * from the producer to the consumer (which might possibly not be |
| 1666 | * connected yet). |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1667 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1668 | if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1669 | buffer_forward(s->req, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1670 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1671 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1672 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 1673 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1674 | s->req->to_forward && |
| 1675 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1676 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1677 | (pipes_used < global.maxpipes) && |
| 1678 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 1679 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1680 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 1681 | s->req->flags |= BF_KERN_SPLICING; |
| 1682 | } |
| 1683 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1684 | /* reflect what the L7 analysers have seen last */ |
| 1685 | rqf_last = s->req->flags; |
| 1686 | |
| 1687 | /* |
| 1688 | * Now forward all shutdown requests between both sides of the buffer |
| 1689 | */ |
| 1690 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1691 | /* first, let's check if the request buffer needs to shutdown(write), which may |
| 1692 | * happen either because the input is closed or because we want to force a close |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1693 | * once the server has begun to respond. |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1694 | */ |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1695 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1696 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1697 | buffer_shutw_now(s->req); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1698 | |
| 1699 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1700 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1701 | s->req->cons->shutw(s->req->cons); |
| 1702 | |
| 1703 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1704 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 1705 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1706 | buffer_shutr_now(s->req); |
| 1707 | |
| 1708 | /* shutdown(read) pending */ |
| 1709 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1710 | s->req->prod->shutr(s->req->prod); |
| 1711 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1712 | /* it's possible that an upper layer has requested a connection setup or abort. |
| 1713 | * There are 2 situations where we decide to establish a new connection : |
| 1714 | * - there are data scheduled for emission in the buffer |
| 1715 | * - the BF_AUTO_CONNECT flag is set (active connection) |
| 1716 | */ |
| 1717 | if (s->req->cons->state == SI_ST_INI) { |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1718 | if (!(s->req->flags & BF_SHUTW)) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1719 | if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) { |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1720 | /* If we have an iohandler without a connect method, we immediately |
| 1721 | * switch to the connected state, otherwise we perform a connection |
| 1722 | * request. |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1723 | */ |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1724 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
Willy Tarreau | 070ceb6 | 2010-06-01 10:36:43 +0200 | [diff] [blame] | 1725 | s->req->cons->conn_retries = s->be->conn_retries; |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1726 | if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1727 | s->req->cons->state = SI_ST_EST; /* connection established */ |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1728 | s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 1729 | s->req->wex = TICK_ETERNITY; |
| 1730 | } |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1731 | } |
Willy Tarreau | 7320122 | 2009-08-16 18:27:24 +0200 | [diff] [blame] | 1732 | } |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1733 | else { |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1734 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1735 | buffer_shutw_now(s->req); /* fix buffer flags upon abort */ |
| 1736 | buffer_shutr_now(s->rep); |
| 1737 | } |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1738 | } |
| 1739 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1740 | |
| 1741 | /* we may have a pending connection request, or a connection waiting |
| 1742 | * for completion. |
| 1743 | */ |
| 1744 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 1745 | do { |
| 1746 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 1747 | * to give a chance to step 2 to perform a redirect if needed. |
| 1748 | */ |
| 1749 | if (s->si[1].state != SI_ST_REQ) |
| 1750 | sess_update_stream_int(s, &s->si[1]); |
| 1751 | if (s->si[1].state == SI_ST_REQ) |
| 1752 | sess_prepare_conn_req(s, &s->si[1]); |
| 1753 | |
| 1754 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1755 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1756 | perform_http_redirect(s, &s->si[1]); |
| 1757 | } while (s->si[1].state == SI_ST_ASS); |
| 1758 | } |
| 1759 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1760 | /* Benchmarks have shown that it's optimal to do a full resync now */ |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1761 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1762 | goto resync_stream_interface; |
| 1763 | |
Willy Tarreau | 815a9b2 | 2010-07-27 17:15:12 +0200 | [diff] [blame] | 1764 | /* otherwise we want to check if we need to resync the req buffer or not */ |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1765 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1766 | goto resync_request; |
| 1767 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1768 | /* perform output updates to the response buffer */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1770 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1771 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1772 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1773 | if (!s->rep->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1774 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1775 | (s->rep->prod->state >= SI_ST_EST) && |
| 1776 | (s->rep->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1777 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1778 | * attached to it. If any data are left in, we'll permit them to |
| 1779 | * move. |
| 1780 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1781 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1782 | buffer_auto_close(s->rep); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1783 | buffer_flush(s->rep); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1784 | if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1785 | buffer_forward(s->rep, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1786 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1788 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1789 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1790 | s->rep->to_forward && |
| 1791 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1792 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1793 | (pipes_used < global.maxpipes) && |
| 1794 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1795 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1796 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1797 | s->rep->flags |= BF_KERN_SPLICING; |
| 1798 | } |
| 1799 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1800 | /* reflect what the L7 analysers have seen last */ |
| 1801 | rpf_last = s->rep->flags; |
| 1802 | |
| 1803 | /* |
| 1804 | * Now forward all shutdown requests between both sides of the buffer |
| 1805 | */ |
| 1806 | |
| 1807 | /* |
| 1808 | * FIXME: this is probably where we should produce error responses. |
| 1809 | */ |
| 1810 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1811 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1812 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1813 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1814 | buffer_shutw_now(s->rep); |
| 1815 | |
| 1816 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1817 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1818 | s->rep->cons->shutw(s->rep->cons); |
| 1819 | |
| 1820 | /* shutdown(write) done on the client side, we must stop the server too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1821 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1822 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1823 | buffer_shutr_now(s->rep); |
| 1824 | |
| 1825 | /* shutdown(read) pending */ |
| 1826 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1827 | s->rep->prod->shutr(s->rep->prod); |
| 1828 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1829 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1830 | goto resync_stream_interface; |
| 1831 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1832 | if (s->req->flags != rqf_last) |
| 1833 | goto resync_request; |
| 1834 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1835 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1836 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1837 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1838 | /* we're interested in getting wakeups again */ |
| 1839 | s->req->prod->flags &= ~SI_FL_DONT_WAKE; |
| 1840 | s->req->cons->flags &= ~SI_FL_DONT_WAKE; |
| 1841 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1842 | /* This is needed only when debugging is enabled, to indicate |
| 1843 | * client-side or server-side close. Please note that in the unlikely |
| 1844 | * event where both sides would close at once, the sequence is reported |
| 1845 | * on the server side first. |
| 1846 | */ |
| 1847 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1848 | (!(global.mode & MODE_QUIET) || |
| 1849 | (global.mode & MODE_VERBOSE)))) { |
| 1850 | int len; |
| 1851 | |
| 1852 | if (s->si[1].state == SI_ST_CLO && |
| 1853 | s->si[1].prev_state == SI_ST_EST) { |
| 1854 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1855 | s->uniq_id, s->be->id, |
| 1856 | (unsigned short)s->si[0].fd, |
| 1857 | (unsigned short)s->si[1].fd); |
| 1858 | write(1, trash, len); |
| 1859 | } |
| 1860 | |
| 1861 | if (s->si[0].state == SI_ST_CLO && |
| 1862 | s->si[0].prev_state == SI_ST_EST) { |
| 1863 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1864 | s->uniq_id, s->be->id, |
| 1865 | (unsigned short)s->si[0].fd, |
| 1866 | (unsigned short)s->si[1].fd); |
| 1867 | write(1, trash, len); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1872 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1873 | |
| 1874 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1875 | session_process_counters(s); |
| 1876 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1877 | if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1878 | s->rep->cons->update(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1879 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1880 | if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1881 | s->req->cons->update(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1882 | |
Willy Tarreau | a6eebb3 | 2010-06-04 11:40:20 +0200 | [diff] [blame] | 1883 | s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED); |
| 1884 | s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1885 | s->si[0].prev_state = s->si[0].state; |
| 1886 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1887 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1888 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1889 | |
| 1890 | /* Trick: if a request is being waiting for the server to respond, |
| 1891 | * and if we know the server can timeout, we don't want the timeout |
| 1892 | * to expire on the client side first, but we're still interested |
| 1893 | * in passing data from the client to the server (eg: POST). Thus, |
| 1894 | * we can cancel the client's request timeout if the server's |
| 1895 | * request timeout is set and the server has not yet sent a response. |
| 1896 | */ |
| 1897 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1898 | if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1899 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1900 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1901 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1902 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1903 | |
Willy Tarreau | 7a20aa6 | 2010-07-13 16:30:45 +0200 | [diff] [blame] | 1904 | /* Call the stream interfaces' I/O handlers when embedded. |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1905 | * Note that this one may wake the task up again. |
| 1906 | */ |
Willy Tarreau | 7a20aa6 | 2010-07-13 16:30:45 +0200 | [diff] [blame] | 1907 | if (s->req->cons->iohandler || s->rep->cons->iohandler) { |
| 1908 | if (s->req->cons->iohandler) |
| 1909 | s->req->cons->iohandler(s->req->cons); |
| 1910 | if (s->rep->cons->iohandler) |
| 1911 | s->rep->cons->iohandler(s->rep->cons); |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1912 | if (task_in_rq(t)) { |
| 1913 | /* If we woke up, we don't want to requeue the |
| 1914 | * task to the wait queue, but rather requeue |
| 1915 | * it into the runqueue ASAP. |
| 1916 | */ |
| 1917 | t->expire = TICK_ETERNITY; |
| 1918 | return t; |
| 1919 | } |
| 1920 | } |
| 1921 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1922 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1923 | tick_first(s->rep->rex, s->rep->wex)); |
| 1924 | if (s->req->analysers) |
| 1925 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1926 | |
| 1927 | if (s->si[0].exp) |
| 1928 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1929 | |
| 1930 | if (s->si[1].exp) |
| 1931 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1932 | |
| 1933 | #ifdef DEBUG_FULL |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 1934 | fprintf(stderr, |
| 1935 | "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u" |
| 1936 | " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n", |
| 1937 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, |
| 1938 | s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1939 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1940 | |
| 1941 | #ifdef DEBUG_DEV |
| 1942 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
Willy Tarreau | d0a201b | 2009-03-08 15:53:06 +0100 | [diff] [blame] | 1943 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1944 | ABORT_NOW(); |
| 1945 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1946 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | s->fe->feconn--; |
| 1950 | if (s->flags & SN_BE_ASSIGNED) |
| 1951 | s->be->beconn--; |
| 1952 | actconn--; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 1953 | s->listener->nbconn--; |
| 1954 | if (s->listener->state == LI_FULL && |
| 1955 | s->listener->nbconn < s->listener->maxconn) { |
| 1956 | /* we should reactivate the listener */ |
| 1957 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1958 | s->listener->state = LI_READY; |
| 1959 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1960 | |
| 1961 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1962 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1963 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1964 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1965 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1966 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1967 | write(1, trash, len); |
| 1968 | } |
| 1969 | |
| 1970 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1971 | session_process_counters(s); |
| 1972 | |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1973 | if (s->txn.status) { |
| 1974 | int n; |
| 1975 | |
| 1976 | n = s->txn.status / 100; |
| 1977 | if (n < 1 || n > 5) |
| 1978 | n = 0; |
| 1979 | |
| 1980 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1981 | s->fe->counters.fe.http.rsp[n]++; |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1983 | if ((s->flags & SN_BE_ASSIGNED) && |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1984 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1985 | s->be->counters.be.http.rsp[n]++; |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1988 | /* let's do a final log if we need it */ |
| 1989 | if (s->logs.logwait && |
| 1990 | !(s->flags & SN_MONITOR) && |
| 1991 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1992 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1996 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1997 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1998 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1999 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2002 | /* |
| 2003 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 2004 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 2005 | * current connection slot. This function also notifies any LB algo which might |
| 2006 | * expect to be informed about any change in the number of active sessions on a |
| 2007 | * server. |
| 2008 | */ |
| 2009 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 2010 | { |
| 2011 | if (sess->srv_conn == newsrv) |
| 2012 | return; |
| 2013 | |
| 2014 | if (sess->srv_conn) { |
| 2015 | sess->srv_conn->served--; |
| 2016 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 2017 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 2018 | sess->srv_conn = NULL; |
| 2019 | } |
| 2020 | |
| 2021 | if (newsrv) { |
| 2022 | newsrv->served++; |
| 2023 | if (newsrv->proxy->lbprm.server_take_conn) |
| 2024 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 2025 | sess->srv_conn = newsrv; |
| 2026 | } |
| 2027 | } |
| 2028 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 2029 | /* Set correct session termination flags in case no analyser has done it. It |
| 2030 | * also counts a failed request if the server state has not reached the request |
| 2031 | * stage. |
| 2032 | */ |
| 2033 | void sess_set_term_flags(struct session *s) |
| 2034 | { |
| 2035 | if (!(s->flags & SN_FINST_MASK)) { |
| 2036 | if (s->si[1].state < SI_ST_REQ) { |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2037 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2038 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2039 | if (s->listener->counters) |
| 2040 | s->listener->counters->failed_req++; |
| 2041 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 2042 | s->flags |= SN_FINST_R; |
| 2043 | } |
| 2044 | else if (s->si[1].state == SI_ST_QUE) |
| 2045 | s->flags |= SN_FINST_Q; |
| 2046 | else if (s->si[1].state < SI_ST_EST) |
| 2047 | s->flags |= SN_FINST_C; |
Willy Tarreau | 033b2db | 2010-03-04 17:54:21 +0100 | [diff] [blame] | 2048 | else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST) |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 2049 | s->flags |= SN_FINST_D; |
| 2050 | else |
| 2051 | s->flags |= SN_FINST_L; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 2056 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 2057 | * session termination flags so that the caller does not have to worry about |
| 2058 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 2059 | */ |
| 2060 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 2061 | { |
| 2062 | int err_type = si->err_type; |
| 2063 | int err = 0, fin = 0; |
| 2064 | |
| 2065 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 2066 | err = SN_ERR_CLICL; |
| 2067 | fin = SN_FINST_Q; |
| 2068 | } |
| 2069 | else if (err_type & SI_ET_CONN_ABRT) { |
| 2070 | err = SN_ERR_CLICL; |
| 2071 | fin = SN_FINST_C; |
| 2072 | } |
| 2073 | else if (err_type & SI_ET_QUEUE_TO) { |
| 2074 | err = SN_ERR_SRVTO; |
| 2075 | fin = SN_FINST_Q; |
| 2076 | } |
| 2077 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 2078 | err = SN_ERR_SRVCL; |
| 2079 | fin = SN_FINST_Q; |
| 2080 | } |
| 2081 | else if (err_type & SI_ET_CONN_TO) { |
| 2082 | err = SN_ERR_SRVTO; |
| 2083 | fin = SN_FINST_C; |
| 2084 | } |
| 2085 | else if (err_type & SI_ET_CONN_ERR) { |
| 2086 | err = SN_ERR_SRVCL; |
| 2087 | fin = SN_FINST_C; |
| 2088 | } |
| 2089 | else /* SI_ET_CONN_OTHER and others */ { |
| 2090 | err = SN_ERR_INTERNAL; |
| 2091 | fin = SN_FINST_C; |
| 2092 | } |
| 2093 | |
| 2094 | if (!(s->flags & SN_ERR_MASK)) |
| 2095 | s->flags |= err; |
| 2096 | if (!(s->flags & SN_FINST_MASK)) |
| 2097 | s->flags |= fin; |
| 2098 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2099 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 2100 | |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2101 | /************************************************************************/ |
| 2102 | /* All supported ACL keywords must be declared here. */ |
| 2103 | /************************************************************************/ |
| 2104 | |
Willy Tarreau | c3bd972 | 2010-06-20 12:47:25 +0200 | [diff] [blame^] | 2105 | /* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */ |
| 2106 | static int |
| 2107 | acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2108 | { |
| 2109 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2110 | test->i = 0; |
| 2111 | if (ts != NULL) { |
| 2112 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0); |
| 2113 | if (!ptr) |
| 2114 | return 0; /* parameter not stored */ |
| 2115 | test->i = stktable_data_cast(ptr, gpc0); |
| 2116 | } |
| 2117 | return 1; |
| 2118 | } |
| 2119 | |
| 2120 | /* set test->i to the General Purpose Counter 0 value from the session's tracked |
| 2121 | * counters. |
| 2122 | */ |
| 2123 | static int |
| 2124 | acl_fetch_trk_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2125 | struct acl_expr *expr, struct acl_test *test) |
| 2126 | { |
| 2127 | if (!l4->tracked_counters) |
| 2128 | return 0; |
| 2129 | return acl_fetch_get_gpc0(l4->tracked_table, test, l4->tracked_counters); |
| 2130 | } |
| 2131 | |
| 2132 | /* set test->i to the General Purpose Counter 0 value from the session's source |
| 2133 | * address in the table pointed to by expr. |
| 2134 | */ |
| 2135 | static int |
| 2136 | acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2137 | struct acl_expr *expr, struct acl_test *test) |
| 2138 | { |
| 2139 | struct stktable_key *key; |
| 2140 | |
| 2141 | key = tcpv4_src_to_stktable_key(l4); |
| 2142 | if (!key) |
| 2143 | return 0; /* only TCPv4 is supported right now */ |
| 2144 | |
| 2145 | if (expr->arg_len) |
| 2146 | px = find_stktable(expr->arg.str); |
| 2147 | |
| 2148 | if (!px) |
| 2149 | return 0; /* table not found */ |
| 2150 | |
| 2151 | return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2152 | } |
| 2153 | |
| 2154 | /* Increment the General Purpose Counter 0 value in the stksess entry <ts> and |
| 2155 | * return it into test->i. |
| 2156 | */ |
| 2157 | static int |
| 2158 | acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2159 | { |
| 2160 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2161 | test->i = 0; |
| 2162 | if (ts != NULL) { |
| 2163 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0); |
| 2164 | if (!ptr) |
| 2165 | return 0; /* parameter not stored */ |
| 2166 | test->i = ++stktable_data_cast(ptr, gpc0); |
| 2167 | } |
| 2168 | return 1; |
| 2169 | } |
| 2170 | |
| 2171 | /* Increment the General Purpose Counter 0 value from the session's tracked |
| 2172 | * counters and return it into test->i. |
| 2173 | */ |
| 2174 | static int |
| 2175 | acl_fetch_trk_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2176 | struct acl_expr *expr, struct acl_test *test) |
| 2177 | { |
| 2178 | if (!l4->tracked_counters) |
| 2179 | return 0; |
| 2180 | return acl_fetch_inc_gpc0(l4->tracked_table, test, l4->tracked_counters); |
| 2181 | } |
| 2182 | |
| 2183 | /* Increment the General Purpose Counter 0 value from the session's source |
| 2184 | * address in the table pointed to by expr, and return it into test->i. |
| 2185 | */ |
| 2186 | static int |
| 2187 | acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2188 | struct acl_expr *expr, struct acl_test *test) |
| 2189 | { |
| 2190 | struct stktable_key *key; |
| 2191 | |
| 2192 | key = tcpv4_src_to_stktable_key(l4); |
| 2193 | if (!key) |
| 2194 | return 0; /* only TCPv4 is supported right now */ |
| 2195 | |
| 2196 | if (expr->arg_len) |
| 2197 | px = find_stktable(expr->arg.str); |
| 2198 | |
| 2199 | if (!px) |
| 2200 | return 0; /* table not found */ |
| 2201 | |
| 2202 | return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key)); |
| 2203 | } |
| 2204 | |
Willy Tarreau | 9a3f849 | 2010-06-18 19:53:25 +0200 | [diff] [blame] | 2205 | /* set test->i to the cumulated number of connections in the stksess entry <ts> */ |
| 2206 | static int |
| 2207 | acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2208 | { |
| 2209 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2210 | test->i = 0; |
| 2211 | if (ts != NULL) { |
| 2212 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT); |
| 2213 | if (!ptr) |
| 2214 | return 0; /* parameter not stored */ |
| 2215 | test->i = stktable_data_cast(ptr, conn_cnt); |
| 2216 | } |
Willy Tarreau | 9a3f849 | 2010-06-18 19:53:25 +0200 | [diff] [blame] | 2217 | return 1; |
| 2218 | } |
| 2219 | |
| 2220 | /* set test->i to the cumulated number of connections from the session's tracked counters */ |
| 2221 | static int |
| 2222 | acl_fetch_trk_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2223 | struct acl_expr *expr, struct acl_test *test) |
| 2224 | { |
| 2225 | if (!l4->tracked_counters) |
| 2226 | return 0; |
| 2227 | |
| 2228 | return acl_fetch_conn_cnt(l4->tracked_table, test, l4->tracked_counters); |
| 2229 | } |
| 2230 | |
| 2231 | /* set test->i to the cumulated number of connections from the session's source |
| 2232 | * address in the table pointed to by expr. |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2233 | */ |
| 2234 | static int |
| 2235 | acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2236 | struct acl_expr *expr, struct acl_test *test) |
| 2237 | { |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2238 | struct stktable_key *key; |
| 2239 | |
| 2240 | key = tcpv4_src_to_stktable_key(l4); |
| 2241 | if (!key) |
| 2242 | return 0; /* only TCPv4 is supported right now */ |
| 2243 | |
| 2244 | if (expr->arg_len) |
| 2245 | px = find_stktable(expr->arg.str); |
| 2246 | |
| 2247 | if (!px) |
| 2248 | return 0; /* table not found */ |
| 2249 | |
Willy Tarreau | 9a3f849 | 2010-06-18 19:53:25 +0200 | [diff] [blame] | 2250 | return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key)); |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 2253 | /* set test->i to the connection rate in the stksess entry <ts> over the configured period */ |
| 2254 | static int |
| 2255 | acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2256 | { |
| 2257 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2258 | test->i = 0; |
| 2259 | if (ts != NULL) { |
| 2260 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE); |
| 2261 | if (!ptr) |
| 2262 | return 0; /* parameter not stored */ |
| 2263 | test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate), |
| 2264 | table->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 2265 | } |
| 2266 | return 1; |
| 2267 | } |
| 2268 | |
| 2269 | /* set test->i to the connection rate from the session's tracked counters over |
| 2270 | * the configured period. |
| 2271 | */ |
| 2272 | static int |
| 2273 | acl_fetch_trk_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2274 | struct acl_expr *expr, struct acl_test *test) |
| 2275 | { |
| 2276 | if (!l4->tracked_counters) |
| 2277 | return 0; |
| 2278 | |
| 2279 | return acl_fetch_conn_rate(l4->tracked_table, test, l4->tracked_counters); |
| 2280 | } |
| 2281 | |
| 2282 | /* set test->i to the connection rate from the session's source address in the |
| 2283 | * table pointed to by expr, over the configured period. |
| 2284 | */ |
| 2285 | static int |
| 2286 | acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2287 | struct acl_expr *expr, struct acl_test *test) |
| 2288 | { |
| 2289 | struct stktable_key *key; |
| 2290 | |
| 2291 | key = tcpv4_src_to_stktable_key(l4); |
| 2292 | if (!key) |
| 2293 | return 0; /* only TCPv4 is supported right now */ |
| 2294 | |
| 2295 | if (expr->arg_len) |
| 2296 | px = find_stktable(expr->arg.str); |
| 2297 | |
| 2298 | if (!px) |
| 2299 | return 0; /* table not found */ |
| 2300 | |
| 2301 | return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2302 | } |
| 2303 | |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2304 | /* set test->i to the number of connections from the session's source address |
| 2305 | * in the table pointed to by expr, after updating it. |
| 2306 | */ |
| 2307 | static int |
| 2308 | acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2309 | struct acl_expr *expr, struct acl_test *test) |
| 2310 | { |
| 2311 | struct stksess *ts; |
| 2312 | struct stktable_key *key; |
| 2313 | void *ptr; |
| 2314 | |
| 2315 | key = tcpv4_src_to_stktable_key(l4); |
| 2316 | if (!key) |
| 2317 | return 0; /* only TCPv4 is supported right now */ |
| 2318 | |
| 2319 | if (expr->arg_len) |
| 2320 | px = find_stktable(expr->arg.str); |
| 2321 | |
| 2322 | if (!px) |
| 2323 | return 0; /* table not found */ |
| 2324 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 2325 | if ((ts = stktable_update_key(&px->table, key)) == NULL) |
| 2326 | /* entry does not exist and could not be created */ |
| 2327 | return 0; |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2328 | |
| 2329 | ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT); |
| 2330 | if (!ptr) |
| 2331 | return 0; /* parameter not stored in this table */ |
| 2332 | |
| 2333 | test->i = ++stktable_data_cast(ptr, conn_cnt); |
| 2334 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2335 | return 1; |
| 2336 | } |
| 2337 | |
Willy Tarreau | 9b0ddcf | 2010-06-18 21:14:36 +0200 | [diff] [blame] | 2338 | /* set test->i to the number of concurrent connections in the stksess entry <ts> */ |
| 2339 | static int |
| 2340 | acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2341 | { |
| 2342 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2343 | test->i = 0; |
| 2344 | |
| 2345 | if (ts != NULL) { |
| 2346 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR); |
| 2347 | if (!ptr) |
| 2348 | return 0; /* parameter not stored */ |
| 2349 | test->i = stktable_data_cast(ptr, conn_cur); |
| 2350 | } |
| 2351 | return 1; |
| 2352 | } |
| 2353 | |
| 2354 | /* set test->i to the number of concurrent connections from the session's tracked counters */ |
| 2355 | static int |
| 2356 | acl_fetch_trk_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2357 | struct acl_expr *expr, struct acl_test *test) |
| 2358 | { |
| 2359 | if (!l4->tracked_counters) |
| 2360 | return 0; |
| 2361 | |
| 2362 | return acl_fetch_conn_cur(l4->tracked_table, test, l4->tracked_counters); |
| 2363 | } |
| 2364 | |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 2365 | /* set test->i to the number of concurrent connections from the session's source |
| 2366 | * address in the table pointed to by expr. |
| 2367 | */ |
| 2368 | static int |
| 2369 | acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2370 | struct acl_expr *expr, struct acl_test *test) |
| 2371 | { |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 2372 | struct stktable_key *key; |
| 2373 | |
| 2374 | key = tcpv4_src_to_stktable_key(l4); |
| 2375 | if (!key) |
| 2376 | return 0; /* only TCPv4 is supported right now */ |
| 2377 | |
| 2378 | if (expr->arg_len) |
| 2379 | px = find_stktable(expr->arg.str); |
| 2380 | |
| 2381 | if (!px) |
| 2382 | return 0; /* table not found */ |
| 2383 | |
Willy Tarreau | 9b0ddcf | 2010-06-18 21:14:36 +0200 | [diff] [blame] | 2384 | return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key)); |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 2385 | } |
| 2386 | |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 2387 | /* set test->i to the cumulated number of sessions in the stksess entry <ts> */ |
| 2388 | static int |
| 2389 | acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2390 | { |
| 2391 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2392 | test->i = 0; |
| 2393 | if (ts != NULL) { |
| 2394 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT); |
| 2395 | if (!ptr) |
| 2396 | return 0; /* parameter not stored */ |
| 2397 | test->i = stktable_data_cast(ptr, sess_cnt); |
| 2398 | } |
| 2399 | return 1; |
| 2400 | } |
| 2401 | |
| 2402 | /* set test->i to the cumulated number of sessions from the session's tracked counters */ |
| 2403 | static int |
| 2404 | acl_fetch_trk_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2405 | struct acl_expr *expr, struct acl_test *test) |
| 2406 | { |
| 2407 | if (!l4->tracked_counters) |
| 2408 | return 0; |
| 2409 | |
| 2410 | return acl_fetch_sess_cnt(l4->tracked_table, test, l4->tracked_counters); |
| 2411 | } |
| 2412 | |
| 2413 | /* set test->i to the cumulated number of session from the session's source |
| 2414 | * address in the table pointed to by expr. |
| 2415 | */ |
| 2416 | static int |
| 2417 | acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2418 | struct acl_expr *expr, struct acl_test *test) |
| 2419 | { |
| 2420 | struct stktable_key *key; |
| 2421 | |
| 2422 | key = tcpv4_src_to_stktable_key(l4); |
| 2423 | if (!key) |
| 2424 | return 0; /* only TCPv4 is supported right now */ |
| 2425 | |
| 2426 | if (expr->arg_len) |
| 2427 | px = find_stktable(expr->arg.str); |
| 2428 | |
| 2429 | if (!px) |
| 2430 | return 0; /* table not found */ |
| 2431 | |
| 2432 | return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2433 | } |
| 2434 | |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 2435 | /* set test->i to the session rate in the stksess entry <ts> over the configured period */ |
| 2436 | static int |
| 2437 | acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2438 | { |
| 2439 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2440 | test->i = 0; |
| 2441 | if (ts != NULL) { |
| 2442 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE); |
| 2443 | if (!ptr) |
| 2444 | return 0; /* parameter not stored */ |
| 2445 | test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate), |
| 2446 | table->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 2447 | } |
| 2448 | return 1; |
| 2449 | } |
| 2450 | |
| 2451 | /* set test->i to the session rate from the session's tracked counters over |
| 2452 | * the configured period. |
| 2453 | */ |
| 2454 | static int |
| 2455 | acl_fetch_trk_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2456 | struct acl_expr *expr, struct acl_test *test) |
| 2457 | { |
| 2458 | if (!l4->tracked_counters) |
| 2459 | return 0; |
| 2460 | |
| 2461 | return acl_fetch_sess_rate(l4->tracked_table, test, l4->tracked_counters); |
| 2462 | } |
| 2463 | |
| 2464 | /* set test->i to the session rate from the session's source address in the |
| 2465 | * table pointed to by expr, over the configured period. |
| 2466 | */ |
| 2467 | static int |
| 2468 | acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2469 | struct acl_expr *expr, struct acl_test *test) |
| 2470 | { |
| 2471 | struct stktable_key *key; |
| 2472 | |
| 2473 | key = tcpv4_src_to_stktable_key(l4); |
| 2474 | if (!key) |
| 2475 | return 0; /* only TCPv4 is supported right now */ |
| 2476 | |
| 2477 | if (expr->arg_len) |
| 2478 | px = find_stktable(expr->arg.str); |
| 2479 | |
| 2480 | if (!px) |
| 2481 | return 0; /* table not found */ |
| 2482 | |
| 2483 | return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2484 | } |
| 2485 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2486 | /* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */ |
| 2487 | static int |
| 2488 | acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2489 | { |
| 2490 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2491 | test->i = 0; |
| 2492 | |
| 2493 | if (ts != NULL) { |
| 2494 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT); |
| 2495 | if (!ptr) |
| 2496 | return 0; /* parameter not stored */ |
| 2497 | test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
| 2498 | } |
| 2499 | return 1; |
| 2500 | } |
| 2501 | |
| 2502 | /* set test->i to the number of kbytes received from clients according to the |
| 2503 | * session's tracked counters. |
| 2504 | */ |
| 2505 | static int |
| 2506 | acl_fetch_trk_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2507 | struct acl_expr *expr, struct acl_test *test) |
| 2508 | { |
| 2509 | if (!l4->tracked_counters) |
| 2510 | return 0; |
| 2511 | |
| 2512 | return acl_fetch_kbytes_in(l4->tracked_table, test, l4->tracked_counters); |
| 2513 | } |
| 2514 | |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2515 | /* set test->i to the number of kbytes received from the session's source |
| 2516 | * address in the table pointed to by expr. |
| 2517 | */ |
| 2518 | static int |
| 2519 | acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2520 | struct acl_expr *expr, struct acl_test *test) |
| 2521 | { |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2522 | struct stktable_key *key; |
| 2523 | |
| 2524 | key = tcpv4_src_to_stktable_key(l4); |
| 2525 | if (!key) |
| 2526 | return 0; /* only TCPv4 is supported right now */ |
| 2527 | |
| 2528 | if (expr->arg_len) |
| 2529 | px = find_stktable(expr->arg.str); |
| 2530 | |
| 2531 | if (!px) |
| 2532 | return 0; /* table not found */ |
| 2533 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2534 | return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2535 | } |
| 2536 | |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 2537 | /* set test->i to the bytes rate from clients in the stksess entry <ts> over the |
| 2538 | * configured period. |
| 2539 | */ |
| 2540 | static int |
| 2541 | acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2542 | { |
| 2543 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2544 | test->i = 0; |
| 2545 | if (ts != NULL) { |
| 2546 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE); |
| 2547 | if (!ptr) |
| 2548 | return 0; /* parameter not stored */ |
| 2549 | test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
| 2550 | table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 2551 | } |
| 2552 | return 1; |
| 2553 | } |
| 2554 | |
| 2555 | /* set test->i to the bytes rate from clients from the session's tracked |
| 2556 | * counters over the configured period. |
| 2557 | */ |
| 2558 | static int |
| 2559 | acl_fetch_trk_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2560 | struct acl_expr *expr, struct acl_test *test) |
| 2561 | { |
| 2562 | if (!l4->tracked_counters) |
| 2563 | return 0; |
| 2564 | |
| 2565 | return acl_fetch_bytes_in_rate(l4->tracked_table, test, l4->tracked_counters); |
| 2566 | } |
| 2567 | |
| 2568 | /* set test->i to the bytes rate from clients from the session's source address |
| 2569 | * in the table pointed to by expr, over the configured period. |
| 2570 | */ |
| 2571 | static int |
| 2572 | acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2573 | struct acl_expr *expr, struct acl_test *test) |
| 2574 | { |
| 2575 | struct stktable_key *key; |
| 2576 | |
| 2577 | key = tcpv4_src_to_stktable_key(l4); |
| 2578 | if (!key) |
| 2579 | return 0; /* only TCPv4 is supported right now */ |
| 2580 | |
| 2581 | if (expr->arg_len) |
| 2582 | px = find_stktable(expr->arg.str); |
| 2583 | |
| 2584 | if (!px) |
| 2585 | return 0; /* table not found */ |
| 2586 | |
| 2587 | return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2588 | } |
| 2589 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2590 | /* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */ |
| 2591 | static int |
| 2592 | acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2593 | { |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2594 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2595 | test->i = 0; |
| 2596 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2597 | if (ts != NULL) { |
| 2598 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2599 | if (!ptr) |
| 2600 | return 0; /* parameter not stored */ |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2601 | test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2602 | } |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2603 | return 1; |
| 2604 | } |
| 2605 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2606 | /* set test->i to the number of kbytes sent to clients according to the session's |
| 2607 | * tracked counters. |
| 2608 | */ |
| 2609 | static int |
| 2610 | acl_fetch_trk_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2611 | struct acl_expr *expr, struct acl_test *test) |
| 2612 | { |
| 2613 | if (!l4->tracked_counters) |
| 2614 | return 0; |
| 2615 | |
| 2616 | return acl_fetch_kbytes_out(l4->tracked_table, test, l4->tracked_counters); |
| 2617 | } |
| 2618 | |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2619 | /* set test->i to the number of kbytes sent to the session's source address in |
| 2620 | * the table pointed to by expr. |
| 2621 | */ |
| 2622 | static int |
| 2623 | acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2624 | struct acl_expr *expr, struct acl_test *test) |
| 2625 | { |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2626 | struct stktable_key *key; |
| 2627 | |
| 2628 | key = tcpv4_src_to_stktable_key(l4); |
| 2629 | if (!key) |
| 2630 | return 0; /* only TCPv4 is supported right now */ |
| 2631 | |
| 2632 | if (expr->arg_len) |
| 2633 | px = find_stktable(expr->arg.str); |
| 2634 | |
| 2635 | if (!px) |
| 2636 | return 0; /* table not found */ |
| 2637 | |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2638 | return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key)); |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 2641 | /* set test->i to the bytes rate to clients in the stksess entry <ts> over the |
| 2642 | * configured period. |
| 2643 | */ |
| 2644 | static int |
| 2645 | acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts) |
| 2646 | { |
| 2647 | test->flags = ACL_TEST_F_VOL_TEST; |
| 2648 | test->i = 0; |
| 2649 | if (ts != NULL) { |
| 2650 | void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE); |
| 2651 | if (!ptr) |
| 2652 | return 0; /* parameter not stored */ |
| 2653 | test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate), |
| 2654 | table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 2655 | } |
| 2656 | return 1; |
| 2657 | } |
| 2658 | |
| 2659 | /* set test->i to the bytes rate to clients from the session's tracked counters |
| 2660 | * over the configured period. |
| 2661 | */ |
| 2662 | static int |
| 2663 | acl_fetch_trk_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2664 | struct acl_expr *expr, struct acl_test *test) |
| 2665 | { |
| 2666 | if (!l4->tracked_counters) |
| 2667 | return 0; |
| 2668 | |
| 2669 | return acl_fetch_bytes_out_rate(l4->tracked_table, test, l4->tracked_counters); |
| 2670 | } |
| 2671 | |
| 2672 | /* set test->i to the bytes rate to client from the session's source address in |
| 2673 | * the table pointed to by expr, over the configured period. |
| 2674 | */ |
| 2675 | static int |
| 2676 | acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 2677 | struct acl_expr *expr, struct acl_test *test) |
| 2678 | { |
| 2679 | struct stktable_key *key; |
| 2680 | |
| 2681 | key = tcpv4_src_to_stktable_key(l4); |
| 2682 | if (!key) |
| 2683 | return 0; /* only TCPv4 is supported right now */ |
| 2684 | |
| 2685 | if (expr->arg_len) |
| 2686 | px = find_stktable(expr->arg.str); |
| 2687 | |
| 2688 | if (!px) |
| 2689 | return 0; /* table not found */ |
| 2690 | |
| 2691 | return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key)); |
| 2692 | } |
| 2693 | |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2694 | |
| 2695 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2696 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | c3bd972 | 2010-06-20 12:47:25 +0200 | [diff] [blame^] | 2697 | { "trk_get_gpc0", acl_parse_int, acl_fetch_trk_get_gpc0, acl_match_int, ACL_USE_NOTHING }, |
| 2698 | { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
| 2699 | { "trk_inc_gpc0", acl_parse_int, acl_fetch_trk_inc_gpc0, acl_match_int, ACL_USE_NOTHING }, |
| 2700 | { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 9a3f849 | 2010-06-18 19:53:25 +0200 | [diff] [blame] | 2701 | { "trk_conn_cnt", acl_parse_int, acl_fetch_trk_conn_cnt, acl_match_int, ACL_USE_NOTHING }, |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2702 | { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 2703 | { "trk_conn_rate", acl_parse_int, acl_fetch_trk_conn_rate, acl_match_int, ACL_USE_NOTHING }, |
| 2704 | { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2705 | { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 9b0ddcf | 2010-06-18 21:14:36 +0200 | [diff] [blame] | 2706 | { "trk_conn_cur", acl_parse_int, acl_fetch_trk_conn_cur, acl_match_int, ACL_USE_NOTHING }, |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 2707 | { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 2708 | { "trk_sess_cnt", acl_parse_int, acl_fetch_trk_sess_cnt, acl_match_int, ACL_USE_NOTHING }, |
| 2709 | { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 2710 | { "trk_sess_rate", acl_parse_int, acl_fetch_trk_sess_rate, acl_match_int, ACL_USE_NOTHING }, |
| 2711 | { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2712 | { "trk_kbytes_in", acl_parse_int, acl_fetch_trk_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2713 | { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 2714 | { "trk_bytes_in_rate", acl_parse_int, acl_fetch_trk_bytes_in_rate, acl_match_int, ACL_USE_NOTHING }, |
| 2715 | { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 1aa006f | 2010-06-18 21:52:52 +0200 | [diff] [blame] | 2716 | { "trk_kbytes_out", acl_parse_int, acl_fetch_trk_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 2717 | { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 2718 | { "trk_bytes_out_rate", acl_parse_int, acl_fetch_trk_bytes_out_rate,acl_match_int, ACL_USE_NOTHING }, |
| 2719 | { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate,acl_match_int, ACL_USE_TCP4_VOLATILE }, |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2720 | { NULL, NULL, NULL, NULL }, |
| 2721 | }}; |
| 2722 | |
| 2723 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 2724 | /* Parse a "track-counters" line starting with "track-counters" in args[arg-1]. |
| 2725 | * Returns the number of warnings emitted, or -1 in case of fatal errors. The |
| 2726 | * <prm> struct is fed with the table name if any. If unspecified, the caller |
| 2727 | * will assume that the current proxy's table is used. |
| 2728 | */ |
| 2729 | int parse_track_counters(char **args, int *arg, |
| 2730 | int section_type, struct proxy *curpx, |
| 2731 | struct track_ctr_prm *prm, |
| 2732 | struct proxy *defpx, char *err, int errlen) |
| 2733 | { |
| 2734 | int pattern_type = 0; |
| 2735 | |
| 2736 | /* parse the arguments of "track-counters" before the condition in the |
| 2737 | * following form : |
| 2738 | * track-counters src [ table xxx ] [ if|unless ... ] |
| 2739 | */ |
| 2740 | while (args[*arg]) { |
| 2741 | if (strcmp(args[*arg], "src") == 0) { |
| 2742 | prm->type = STKTABLE_TYPE_IP; |
| 2743 | pattern_type = 1; |
| 2744 | } |
| 2745 | else if (strcmp(args[*arg], "table") == 0) { |
| 2746 | if (!args[*arg + 1]) { |
| 2747 | snprintf(err, errlen, |
| 2748 | "missing table for track-counter in %s '%s'.", |
| 2749 | proxy_type_str(curpx), curpx->id); |
| 2750 | return -1; |
| 2751 | } |
| 2752 | /* we copy the table name for now, it will be resolved later */ |
| 2753 | prm->table.n = strdup(args[*arg + 1]); |
| 2754 | (*arg)++; |
| 2755 | } |
| 2756 | else { |
| 2757 | /* unhandled keywords are handled by the caller */ |
| 2758 | break; |
| 2759 | } |
| 2760 | (*arg)++; |
| 2761 | } |
| 2762 | |
| 2763 | if (!pattern_type) { |
| 2764 | snprintf(err, errlen, |
| 2765 | "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).", |
| 2766 | proxy_type_str(curpx), curpx->id, quote_arg(args[*arg])); |
| 2767 | return -1; |
| 2768 | } |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
Willy Tarreau | 8b22a71 | 2010-06-18 17:46:06 +0200 | [diff] [blame] | 2773 | __attribute__((constructor)) |
| 2774 | static void __session_init(void) |
| 2775 | { |
| 2776 | acl_register_keywords(&acl_kws); |
| 2777 | } |
| 2778 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2779 | /* |
| 2780 | * Local variables: |
| 2781 | * c-indent-level: 8 |
| 2782 | * c-basic-offset: 8 |
| 2783 | * End: |
| 2784 | */ |