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