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