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