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