blob: 58d2471a8bef5c98516c854da660fb9254d72de1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
Willy Tarreauac68c5d2009-10-04 23:12:44 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020013/* Short explanation on the locking, which is far from being trivial : a
14 * pendconn is a list element which necessarily is associated with an existing
15 * stream. It has pendconn->strm always valid. A pendconn may only be in one of
16 * these three states :
17 * - unlinked : in this case it is an empty list head ;
18 * - linked into the server's queue ;
19 * - linked into the proxy's queue.
20 *
21 * A stream does not necessarily have such a pendconn. Thus the pendconn is
22 * designated by the stream->pend_pos pointer. This results in some properties :
23 * - pendconn->strm->pend_pos is never NULL for any valid pendconn
Patrick Hemmer0355dab2018-05-11 12:52:31 -040024 * - if p->node.node.leaf_p is NULL, the element is unlinked,
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020025 * otherwise it necessarily belongs to one of the other lists ; this may
26 * not be atomically checked under threads though ;
27 * - pendconn->px is never NULL if pendconn->list is not empty
Willy Tarreau88930dd2018-07-26 07:38:54 +020028 * - pendconn->srv is never NULL if pendconn->list is in the server's queue,
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020029 * and is always NULL if pendconn->list is in the backend's queue or empty.
Willy Tarreau88930dd2018-07-26 07:38:54 +020030 * - pendconn->target is NULL while the element is queued, and points to the
31 * assigned server when the pendconn is picked.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020032 *
33 * Threads complicate the design a little bit but rules remain simple :
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020034 * - the server's queue lock must be held at least when manipulating the
35 * server's queue, which is when adding a pendconn to the queue and when
36 * removing a pendconn from the queue. It protects the queue's integrity.
37 *
38 * - the proxy's queue lock must be held at least when manipulating the
39 * proxy's queue, which is when adding a pendconn to the queue and when
40 * removing a pendconn from the queue. It protects the queue's integrity.
41 *
Willy Tarreau3201e4e2018-07-26 08:23:24 +020042 * - both locks are compatible and may be held at the same time.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020043 *
44 * - a pendconn_add() is only performed by the stream which will own the
45 * pendconn ; the pendconn is allocated at this moment and returned ; it is
46 * added to either the server or the proxy's queue while holding this
Willy Tarreau3201e4e2018-07-26 08:23:24 +020047 * queue's lock.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020048 *
49 * - the pendconn is then met by a thread walking over the proxy or server's
50 * queue with the respective lock held. This lock is exclusive and the
51 * pendconn can only appear in one queue so by definition a single thread
52 * may find this pendconn at a time.
53 *
54 * - the pendconn is unlinked either by its own stream upon success/abort/
55 * free, or by another one offering it its server slot. This is achieved by
56 * pendconn_process_next_strm() under either the server or proxy's lock,
57 * pendconn_redistribute() under the server's lock, pendconn_grab_from_px()
58 * under the proxy's lock, or pendconn_unlink() under either the proxy's or
59 * the server's lock depending on the queue the pendconn is attached to.
60 *
61 * - no single operation except the pendconn initialisation prior to the
Willy Tarreau3201e4e2018-07-26 08:23:24 +020062 * insertion are performed without eithre a queue lock held or the element
63 * being unlinked and visible exclusively to its stream.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020064 *
Willy Tarreau88930dd2018-07-26 07:38:54 +020065 * - pendconn_grab_from_px() and pendconn_process_next_strm() assign ->target
66 * so that the stream knows what server to work with (via
67 * pendconn_dequeue() which sets it on strm->target).
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020068 *
69 * - a pendconn doesn't switch between queues, it stays where it is.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020070 */
71
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020072#include <common/config.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010073#include <common/initcall.h>
Willy Tarreaue4d7e552007-05-13 20:19:55 +020074#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020075#include <common/time.h>
Christopher Faulet8ba59142017-06-27 15:43:53 +020076#include <common/hathreads.h>
Patrick Hemmer0355dab2018-05-11 12:52:31 -040077#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Willy Tarreau61c112a2018-10-02 16:43:32 +020079#include <proto/http_rules.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040080#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020081#include <proto/queue.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040082#include <proto/sample.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020084#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010085#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086#include <proto/task.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040087#include <proto/tcp_rules.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
89
Patrick Hemmer248cb4c2018-05-11 12:52:31 -040090#define NOW_OFFSET_BOUNDARY() ((now_ms - (TIMER_LOOK_BACK >> 12)) & 0xfffff)
91#define KEY_CLASS(key) ((u32)key & 0xfff00000)
92#define KEY_OFFSET(key) ((u32)key & 0x000fffff)
93#define KEY_CLASS_OFFSET_BOUNDARY(key) (KEY_CLASS(key) | NOW_OFFSET_BOUNDARY())
94#define MAKE_KEY(class, offset) (((u32)(class + 0x7ff) << 20) | ((u32)(now_ms + offset) & 0xfffff))
95
Willy Tarreau8ceae722018-11-26 11:58:30 +010096DECLARE_POOL(pool_head_pendconn, "pendconn", sizeof(struct pendconn));
Willy Tarreaubaaee002006-06-26 02:48:02 +020097
98/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010099 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +0100100 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
101 * server is currently warming up, the slowstart is also applied to the
102 * resulting value, which can be lower than minconn in this case, but never
103 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200105unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106{
Willy Tarreau9909fc12007-11-30 17:42:05 +0100107 unsigned int max;
108
Willy Tarreau86034312006-12-29 00:10:33 +0100109 if (s->proxy->beconn >= s->proxy->fullconn)
110 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100111 max = s->maxconn;
112 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +0100113 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100114 max = s->maxconn;
115 else max = MAX(s->minconn,
116 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +0100117
Emeric Brun52a91d32017-08-31 14:41:55 +0200118 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +0100119 now.tv_sec < s->last_change + s->slowstart &&
120 now.tv_sec >= s->last_change) {
121 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +0200122 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
123 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +0100124 }
125 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126}
127
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100128/* Remove the pendconn from the server/proxy queue. At this stage, the
129 * connection is not really dequeued. It will be done during the
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200130 * process_stream. It also decreases the pending count.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100131 *
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200132 * The caller must own the lock on the queue containing the pendconn. The
133 * pendconn must still be queued.
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200134 */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200135static void __pendconn_unlink(struct pendconn *p)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100136{
Patrick Hemmerda282f42018-05-11 12:52:31 -0400137 if (p->srv) {
138 p->strm->logs.srv_queue_pos += p->srv->queue_idx - p->queue_idx;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100139 p->srv->nbpend--;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400140 } else {
141 p->strm->logs.prx_queue_pos += p->px->queue_idx - p->queue_idx;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100142 p->px->nbpend--;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400143 }
Olivier Houchardb4df4922019-03-08 18:54:16 +0100144 _HA_ATOMIC_SUB(&p->px->totpend, 1);
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400145 eb32_delete(&p->node);
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200146}
147
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200148/* Locks the queue the pendconn element belongs to. This relies on both p->px
149 * and p->srv to be properly initialized (which is always the case once the
150 * element has been added).
151 */
152static inline void pendconn_queue_lock(struct pendconn *p)
153{
154 if (p->srv)
155 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
156 else
157 HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock);
158}
159
160/* Unlocks the queue the pendconn element belongs to. This relies on both p->px
161 * and p->srv to be properly initialized (which is always the case once the
162 * element has been added).
163 */
164static inline void pendconn_queue_unlock(struct pendconn *p)
165{
166 if (p->srv)
167 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
168 else
169 HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock);
170}
171
Willy Tarreau9624fae2018-07-25 08:04:20 +0200172/* Removes the pendconn from the server/proxy queue. At this stage, the
173 * connection is not really dequeued. It will be done during process_stream().
Willy Tarreau9410b922019-11-14 14:58:39 +0100174 * This function takes all the required locks for the operation. The pendconn
175 * must be valid, though it doesn't matter if it was already unlinked. Prefer
176 * pendconn_cond_unlink() to first check <p>. When the locks are already held,
177 * please use __pendconn_unlink() instead.
Willy Tarreau9624fae2018-07-25 08:04:20 +0200178 */
179void pendconn_unlink(struct pendconn *p)
180{
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200181 pendconn_queue_lock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200182
Willy Tarreau9410b922019-11-14 14:58:39 +0100183 if (p->node.node.leaf_p)
184 __pendconn_unlink(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200185
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200186 pendconn_queue_unlock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200187}
188
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400189/* Retrieve the first pendconn from tree <pendconns>. Classes are always
190 * considered first, then the time offset. The time does wrap, so the
191 * lookup is performed twice, one to retrieve the first class and a second
192 * time to retrieve the earliest time in this class.
193 */
194static struct pendconn *pendconn_first(struct eb_root *pendconns)
195{
196 struct eb32_node *node, *node2 = NULL;
197 u32 key;
198
199 node = eb32_first(pendconns);
200 if (!node)
201 return NULL;
202
203 key = KEY_CLASS_OFFSET_BOUNDARY(node->key);
204 node2 = eb32_lookup_ge(pendconns, key);
205
206 if (!node2 ||
207 KEY_CLASS(node2->key) != KEY_CLASS(node->key)) {
208 /* no other key in the tree, or in this class */
209 return eb32_entry(node, struct pendconn, node);
210 }
211
212 /* found a better key */
213 return eb32_entry(node2, struct pendconn, node);
214}
215
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100216/* Process the next pending connection from either a server or a proxy, and
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100217 * returns a strictly positive value on success (see below). If no pending
218 * connection is found, 0 is returned. Note that neither <srv> nor <px> may be
219 * NULL. Priority is given to the oldest request in the queue if both <srv> and
220 * <px> have pending requests. This ensures that no request will be left
221 * unserved. The <px> queue is not considered if the server (or a tracked
222 * server) is not RUNNING, is disabled, or has a null weight (server going
223 * down). The <srv> queue is still considered in this case, because if some
224 * connections remain there, it means that some requests have been forced there
225 * after it was seen down (eg: due to option persist). The stream is
226 * immediately marked as "assigned", and both its <srv> and <srv_conn> are set
227 * to <srv>.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100228 *
229 * This function must only be called if the server queue _AND_ the proxy queue
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100230 * are locked. Today it is only called by process_srv_queue. When a pending
231 * connection is dequeued, this function returns 1 if the pending connection can
232 * be handled by the current thread, else it returns 2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100234static int pendconn_process_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100236 struct pendconn *p = NULL;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400237 struct pendconn *pp = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100238 struct server *rsrv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400239 u32 pkey, ppkey;
Willy Tarreaud132f742010-08-06 10:08:23 +0200240
Willy Tarreau44267702011-10-28 15:35:33 +0200241 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200242 if (!rsrv)
243 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200245 p = NULL;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400246 if (srv->nbpend)
247 p = pendconn_first(&srv->pendconns);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200248
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400249 pp = NULL;
Willy Tarreaua8694652018-08-07 10:44:58 +0200250 if (srv_currently_usable(rsrv) && px->nbpend &&
251 (!(srv->flags & SRV_F_BACKUP) ||
252 (!px->srv_act &&
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400253 (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK)))))
254 pp = pendconn_first(&px->pendconns);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100255
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400256 if (!p && !pp)
257 return 0;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200258
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400259 if (p && !pp)
260 goto use_p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100261
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400262 if (pp && !p)
263 goto use_pp;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100264
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400265 if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key))
266 goto use_p;
267
268 if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key))
269 goto use_pp;
270
271 pkey = KEY_OFFSET(p->node.key);
272 ppkey = KEY_OFFSET(pp->node.key);
273
274 if (pkey < NOW_OFFSET_BOUNDARY())
275 pkey += 0x100000; // key in the future
276
277 if (ppkey < NOW_OFFSET_BOUNDARY())
278 ppkey += 0x100000; // key in the future
279
280 if (pkey <= ppkey)
281 goto use_p;
282
283 use_pp:
284 /* Let's switch from the server pendconn to the proxy pendconn */
285 p = pp;
286 use_p:
Willy Tarreau9624fae2018-07-25 08:04:20 +0200287 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100288 p->strm_flags |= SF_ASSIGNED;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200289 p->target = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100290
Patrick Hemmerda282f42018-05-11 12:52:31 -0400291 if (p != pp)
292 srv->queue_idx++;
293 else
294 px->queue_idx++;
295
Olivier Houchardb4df4922019-03-08 18:54:16 +0100296 _HA_ATOMIC_ADD(&srv->served, 1);
297 _HA_ATOMIC_ADD(&srv->proxy->served, 1);
298 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +0200299 if (px->lbprm.server_take_conn)
300 px->lbprm.server_take_conn(srv);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100301 __stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200302
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100303 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100304
Olivier Houchardecfe6732018-07-26 18:47:27 +0200305 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306}
307
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100308/* Manages a server's connection queue. This function will try to dequeue as
Amaury Denoyelleb0fbda52021-06-18 11:11:36 +0200309 * many pending streams as possible, and wake them up. <server_locked> must
310 * only be set if the caller already hold the server lock.
Christopher Faulet87566c92017-06-06 10:34:51 +0200311 */
Amaury Denoyelleb0fbda52021-06-18 11:11:36 +0200312void process_srv_queue(struct server *s, int server_locked)
Christopher Faulet87566c92017-06-06 10:34:51 +0200313{
314 struct proxy *p = s->proxy;
Olivier Houchardecfe6732018-07-26 18:47:27 +0200315 int maxconn;
Christopher Faulet87566c92017-06-06 10:34:51 +0200316
Amaury Denoyelleb0fbda52021-06-18 11:11:36 +0200317 if (!server_locked)
318 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200319 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200320 maxconn = srv_dynamic_maxconn(s);
321 while (s->served < maxconn) {
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100322 int ret = pendconn_process_next_strm(s, p);
323 if (!ret)
Christopher Faulet87566c92017-06-06 10:34:51 +0200324 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200325 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Amaury Denoyelleb0fbda52021-06-18 11:11:36 +0200327 if (!server_locked)
328 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200329}
330
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400331/* Adds the stream <strm> to the pending connection queue of server <strm>->srv
Willy Tarreau87b09662015-04-03 00:22:06 +0200332 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200334 * pendconn itself. If the stream was already marked as served, its flag is
335 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Patrick Hemmerda282f42018-05-11 12:52:31 -0400336 * The stream's queue position is counted with an offset of -1 because we want
337 * to make sure that being at the first position in the queue reports 1.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100338 *
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400339 * The queue is sorted by the composition of the priority_class, and the current
340 * timestamp offset by strm->priority_offset. The timestamp is in milliseconds
341 * and truncated to 20 bits, so will wrap every 17m28s575ms.
342 * The offset can be positive or negative, and an offset of 0 puts it in the
343 * middle of this range (~ 8 min). Note that this also means if the adjusted
344 * timestamp wraps around, the request will be misinterpreted as being of
Joseph Herlantd8499ec2018-11-25 11:26:48 -0800345 * the highest priority for that priority class.
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400346 *
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100347 * This function must be called by the stream itself, so in the context of
348 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200350struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351{
352 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100353 struct proxy *px;
354 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355
Willy Tarreaubafbe012017-11-24 17:34:44 +0100356 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 if (!p)
358 return NULL;
359
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200360 if (strm->flags & SF_ASSIGNED)
361 srv = objt_server(strm->target);
362 else
363 srv = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100364
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200365 px = strm->be;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200366 p->target = NULL;
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200367 p->srv = srv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400368 p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100369 p->px = px;
370 p->strm = strm;
371 p->strm_flags = strm->flags;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200372
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200373 pendconn_queue_lock(p);
374
375 if (srv) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100376 srv->nbpend++;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100377 if (srv->nbpend > srv->counters.nbpend_max)
378 srv->counters.nbpend_max = srv->nbpend;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400379 p->queue_idx = srv->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400380 eb32_insert(&srv->pendconns, &p->node);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100381 }
382 else {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100383 px->nbpend++;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100384 if (px->nbpend > px->be_counters.nbpend_max)
385 px->be_counters.nbpend_max = px->nbpend;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400386 p->queue_idx = px->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400387 eb32_insert(&px->pendconns, &p->node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388 }
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200389 strm->pend_pos = p;
390
391 pendconn_queue_unlock(p);
392
Olivier Houchardb4df4922019-03-08 18:54:16 +0100393 _HA_ATOMIC_ADD(&px->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394 return p;
395}
396
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200397/* Redistribute pending connections when a server goes down. The number of
Willy Tarreaudeca26c2018-08-21 18:11:03 +0200398 * connections redistributed is returned. It must be called with the server
399 * lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200400 */
401int pendconn_redistribute(struct server *s)
402{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400403 struct pendconn *p;
Willy Tarreaubff005a2019-05-27 08:10:11 +0200404 struct eb32_node *node, *nodeb;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200405 int xferred = 0;
406
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100407 /* The REDISP option was specified. We will ignore cookie and force to
408 * balance or use the dispatcher. */
409 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
410 return 0;
411
Willy Tarreaubff005a2019-05-27 08:10:11 +0200412 for (node = eb32_first(&s->pendconns); node; node = nodeb) {
413 nodeb = eb32_next(node);
414
415 p = eb32_entry(node, struct pendconn, node);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100416 if (p->strm_flags & SF_FORCE_PRST)
417 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200418
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100419 /* it's left to the dispatcher to choose a server */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200420 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100421 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200422
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100423 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Willy Tarreau440839b2020-10-21 11:54:38 +0200424 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200425 }
426 return xferred;
427}
428
429/* Check for pending connections at the backend, and assign some of them to
430 * the server coming up. The server's weight is checked before being assigned
431 * connections it may not be able to handle. The total number of transferred
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200432 * connections is returned. It must be called with the server lock held, and
433 * will take the proxy's lock.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200434 */
435int pendconn_grab_from_px(struct server *s)
436{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400437 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100438 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200439
Emeric Brun52a91d32017-08-31 14:41:55 +0200440 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200441 return 0;
442
Willy Tarreaua8694652018-08-07 10:44:58 +0200443 /* if this is a backup server and there are active servers or at
444 * least another backup server was elected, then this one must
445 * not dequeue requests from the proxy.
446 */
447 if ((s->flags & SRV_F_BACKUP) &&
448 (s->proxy->srv_act ||
449 ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK))))
450 return 0;
451
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100452 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100453 maxconn = srv_dynamic_maxconn(s);
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400454 while ((p = pendconn_first(&s->proxy->pendconns))) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100455 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200456 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100457
Willy Tarreau9624fae2018-07-25 08:04:20 +0200458 __pendconn_unlink(p);
Willy Tarreau88930dd2018-07-26 07:38:54 +0200459 p->target = s;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100460
461 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100462 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200463 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100464 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200465 return xferred;
466}
467
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100468/* Try to dequeue pending connection attached to the stream <strm>. It must
469 * always exists here. If the pendconn is still linked to the server or the
470 * proxy queue, nothing is done and the function returns 1. Otherwise,
471 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
472 * is returned.
473 *
474 * This function must be called by the stream itself, so in the context of
475 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100477int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100479 struct pendconn *p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200480 int is_unlinked;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100481
482 if (unlikely(!strm->pend_pos)) {
483 /* unexpected case because it is called by the stream itself and
484 * only the stream can release a pendconn. So it is only
485 * possible if a pendconn is released by someone else or if the
486 * stream is supposed to be queued but without its associated
487 * pendconn. In both cases it is a bug! */
488 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200489 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100490 p = strm->pend_pos;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200491
492 /* note below : we need to grab the queue's lock to check for emptiness
493 * because we don't want a partial _grab_from_px() or _redistribute()
494 * to be called in parallel and show an empty list without having the
495 * time to finish. With this we know that if we see the element
496 * unlinked, these functions were completely done.
497 */
498 pendconn_queue_lock(p);
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400499 is_unlinked = !p->node.node.leaf_p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200500 pendconn_queue_unlock(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100501
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200502 if (!is_unlinked)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100503 return 1;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100504
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200505 /* the pendconn is not queued anymore and will not be so we're safe
506 * to proceed.
507 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100508 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
509 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau9c27f362021-06-16 08:42:23 +0200510
511 if (p->target) {
512 /* a server picked this pendconn, it must skip LB */
513 strm->target = &p->target->obj_type;
514 strm->flags |= SF_ASSIGNED;
515 }
516
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100517 strm->pend_pos = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100518 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100519 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520}
521
Patrick Hemmer268a7072018-05-11 12:52:31 -0400522static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px,
523 struct session *sess, struct stream *s, int flags)
524{
525 struct sample *smp;
526
527 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
528 if (!smp)
529 return ACT_RET_CONT;
530
531 s->priority_class = queue_limit_class(smp->data.u.sint);
532 return ACT_RET_CONT;
533}
534
535static enum act_return action_set_priority_offset(struct act_rule *rule, struct proxy *px,
536 struct session *sess, struct stream *s, int flags)
537{
538 struct sample *smp;
539
540 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
541 if (!smp)
542 return ACT_RET_CONT;
543
544 s->priority_offset = queue_limit_offset(smp->data.u.sint);
545
546 return ACT_RET_CONT;
547}
548
549static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px,
550 struct act_rule *rule, char **err)
551{
552 unsigned int where = 0;
553
554 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
555 px->conf.args.line, err, &px->conf.args);
556 if (!rule->arg.expr)
557 return ACT_RET_PRS_ERR;
558
559 if (px->cap & PR_CAP_FE)
560 where |= SMP_VAL_FE_HRQ_HDR;
561 if (px->cap & PR_CAP_BE)
562 where |= SMP_VAL_BE_HRQ_HDR;
563
564 if (!(rule->arg.expr->fetch->val & where)) {
565 memprintf(err,
566 "fetch method '%s' extracts information from '%s', none of which is available here",
567 args[0], sample_src_names(rule->arg.expr->fetch->use));
568 free(rule->arg.expr);
569 return ACT_RET_PRS_ERR;
570 }
571
572 rule->action = ACT_CUSTOM;
573 rule->action_ptr = action_set_priority_class;
574 return ACT_RET_PRS_OK;
575}
576
577static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px,
578 struct act_rule *rule, char **err)
579{
580 unsigned int where = 0;
581
582 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
583 px->conf.args.line, err, &px->conf.args);
584 if (!rule->arg.expr)
585 return ACT_RET_PRS_ERR;
586
587 if (px->cap & PR_CAP_FE)
588 where |= SMP_VAL_FE_HRQ_HDR;
589 if (px->cap & PR_CAP_BE)
590 where |= SMP_VAL_BE_HRQ_HDR;
591
592 if (!(rule->arg.expr->fetch->val & where)) {
593 memprintf(err,
594 "fetch method '%s' extracts information from '%s', none of which is available here",
595 args[0], sample_src_names(rule->arg.expr->fetch->use));
596 free(rule->arg.expr);
597 return ACT_RET_PRS_ERR;
598 }
599
600 rule->action = ACT_CUSTOM;
601 rule->action_ptr = action_set_priority_offset;
602 return ACT_RET_PRS_OK;
603}
604
605static struct action_kw_list tcp_cont_kws = {ILH, {
606 { "set-priority-class", parse_set_priority_class },
607 { "set-priority-offset", parse_set_priority_offset },
608 { /* END */ }
609}};
610
Willy Tarreau0108d902018-11-25 19:14:37 +0100611INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_cont_kws);
612
Patrick Hemmer268a7072018-05-11 12:52:31 -0400613static struct action_kw_list http_req_kws = {ILH, {
614 { "set-priority-class", parse_set_priority_class },
615 { "set-priority-offset", parse_set_priority_offset },
616 { /* END */ }
617}};
618
Willy Tarreau0108d902018-11-25 19:14:37 +0100619INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
620
Patrick Hemmer268a7072018-05-11 12:52:31 -0400621static int
622smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private)
623{
624 if (!smp->strm)
625 return 0;
626
627 smp->data.type = SMP_T_SINT;
628 smp->data.u.sint = smp->strm->priority_class;
629
630 return 1;
631}
632
633static int
634smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private)
635{
636 if (!smp->strm)
637 return 0;
638
639 smp->data.type = SMP_T_SINT;
640 smp->data.u.sint = smp->strm->priority_offset;
641
642 return 1;
643}
644
645
646static struct sample_fetch_kw_list smp_kws = {ILH, {
647 { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
648 { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
649 { /* END */},
650}};
651
Willy Tarreau0108d902018-11-25 19:14:37 +0100652INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Patrick Hemmer268a7072018-05-11 12:52:31 -0400653
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654/*
655 * Local variables:
656 * c-indent-level: 8
657 * c-basic-offset: 8
658 * End:
659 */