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