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