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