Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Queue management functions. |
| 3 | * |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 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 | |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 13 | /* 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 Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 24 | * - if p->node.node.leaf_p is NULL, the element is unlinked, |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 25 | * 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 Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 28 | * - pendconn->srv is never NULL if pendconn->list is in the server's queue, |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 29 | * and is always NULL if pendconn->list is in the backend's queue or empty. |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 30 | * - pendconn->target is NULL while the element is queued, and points to the |
| 31 | * assigned server when the pendconn is picked. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 32 | * |
| 33 | * Threads complicate the design a little bit but rules remain simple : |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 34 | * - 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 Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 42 | * - both locks are compatible and may be held at the same time. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 43 | * |
| 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 Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 47 | * queue's lock. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 48 | * |
| 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 Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 62 | * insertion are performed without eithre a queue lock held or the element |
| 63 | * being unlinked and visible exclusively to its stream. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 64 | * |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 65 | * - 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 Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 68 | * |
| 69 | * - a pendconn doesn't switch between queues, it stays where it is. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 70 | */ |
| 71 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 72 | #include <common/config.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 73 | #include <common/initcall.h> |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 74 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 75 | #include <common/time.h> |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 76 | #include <common/hathreads.h> |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 77 | #include <eb32tree.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 79 | #include <proto/http_rules.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 80 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | #include <proto/queue.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 82 | #include <proto/sample.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 84 | #include <proto/stream.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 85 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | #include <proto/task.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 87 | #include <proto/tcp_rules.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | |
| 89 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 90 | #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 Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 96 | DECLARE_POOL(pool_head_pendconn, "pendconn", sizeof(struct pendconn)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | |
| 98 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 99 | * and the proxy's usage relative to its dynamic connections limit. It is |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 100 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 105 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 107 | unsigned int max; |
| 108 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 109 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 110 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 111 | max = s->maxconn; |
| 112 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 113 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 114 | max = s->maxconn; |
| 115 | else max = MAX(s->minconn, |
| 116 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 117 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 118 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 119 | now.tv_sec < s->last_change + s->slowstart && |
| 120 | now.tv_sec >= s->last_change) { |
| 121 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 122 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 123 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 124 | } |
| 125 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 128 | /* 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 Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 130 | * process_stream. It also decreases the pending count. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 131 | * |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 132 | * The caller must own the lock on the queue containing the pendconn. The |
| 133 | * pendconn must still be queued. |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 134 | */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 135 | static void __pendconn_unlink(struct pendconn *p) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 136 | { |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 137 | if (p->srv) { |
| 138 | p->strm->logs.srv_queue_pos += p->srv->queue_idx - p->queue_idx; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 139 | p->srv->nbpend--; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 140 | } else { |
| 141 | p->strm->logs.prx_queue_pos += p->px->queue_idx - p->queue_idx; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 142 | p->px->nbpend--; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 143 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 144 | HA_ATOMIC_SUB(&p->px->totpend, 1); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 145 | eb32_delete(&p->node); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 148 | /* 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 | */ |
| 152 | static 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 | */ |
| 164 | static 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 Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 172 | /* 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(). |
| 174 | * This function takes all the required locks for the operation. The caller is |
| 175 | * responsible for ensuring that <p> is valid and still in the queue. Use |
| 176 | * pendconn_cond_unlink() if unsure. When the locks are already held, please |
| 177 | * use __pendconn_unlink() instead. |
| 178 | */ |
| 179 | void pendconn_unlink(struct pendconn *p) |
| 180 | { |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 181 | pendconn_queue_lock(p); |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 182 | |
| 183 | __pendconn_unlink(p); |
| 184 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 185 | pendconn_queue_unlock(p); |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 188 | /* Retrieve the first pendconn from tree <pendconns>. Classes are always |
| 189 | * considered first, then the time offset. The time does wrap, so the |
| 190 | * lookup is performed twice, one to retrieve the first class and a second |
| 191 | * time to retrieve the earliest time in this class. |
| 192 | */ |
| 193 | static struct pendconn *pendconn_first(struct eb_root *pendconns) |
| 194 | { |
| 195 | struct eb32_node *node, *node2 = NULL; |
| 196 | u32 key; |
| 197 | |
| 198 | node = eb32_first(pendconns); |
| 199 | if (!node) |
| 200 | return NULL; |
| 201 | |
| 202 | key = KEY_CLASS_OFFSET_BOUNDARY(node->key); |
| 203 | node2 = eb32_lookup_ge(pendconns, key); |
| 204 | |
| 205 | if (!node2 || |
| 206 | KEY_CLASS(node2->key) != KEY_CLASS(node->key)) { |
| 207 | /* no other key in the tree, or in this class */ |
| 208 | return eb32_entry(node, struct pendconn, node); |
| 209 | } |
| 210 | |
| 211 | /* found a better key */ |
| 212 | return eb32_entry(node2, struct pendconn, node); |
| 213 | } |
| 214 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 215 | /* Process the next pending connection from either a server or a proxy, and |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 216 | * returns a strictly positive value on success (see below). If no pending |
| 217 | * connection is found, 0 is returned. Note that neither <srv> nor <px> may be |
| 218 | * NULL. Priority is given to the oldest request in the queue if both <srv> and |
| 219 | * <px> have pending requests. This ensures that no request will be left |
| 220 | * unserved. The <px> queue is not considered if the server (or a tracked |
| 221 | * server) is not RUNNING, is disabled, or has a null weight (server going |
| 222 | * down). The <srv> queue is still considered in this case, because if some |
| 223 | * connections remain there, it means that some requests have been forced there |
| 224 | * after it was seen down (eg: due to option persist). The stream is |
| 225 | * immediately marked as "assigned", and both its <srv> and <srv_conn> are set |
| 226 | * to <srv>. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 227 | * |
| 228 | * This function must only be called if the server queue _AND_ the proxy queue |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 229 | * are locked. Today it is only called by process_srv_queue. When a pending |
| 230 | * connection is dequeued, this function returns 1 if the pending connection can |
| 231 | * be handled by the current thread, else it returns 2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 232 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 233 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 234 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 235 | struct pendconn *p = NULL; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 236 | struct pendconn *pp = NULL; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 237 | struct server *rsrv; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 238 | u32 pkey, ppkey; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 239 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 240 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 241 | if (!rsrv) |
| 242 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 243 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 244 | p = NULL; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 245 | if (srv->nbpend) |
| 246 | p = pendconn_first(&srv->pendconns); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 247 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 248 | pp = NULL; |
Willy Tarreau | a869465 | 2018-08-07 10:44:58 +0200 | [diff] [blame] | 249 | if (srv_currently_usable(rsrv) && px->nbpend && |
| 250 | (!(srv->flags & SRV_F_BACKUP) || |
| 251 | (!px->srv_act && |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 252 | (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK))))) |
| 253 | pp = pendconn_first(&px->pendconns); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 254 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 255 | if (!p && !pp) |
| 256 | return 0; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 257 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 258 | if (p && !pp) |
| 259 | goto use_p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 260 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 261 | if (pp && !p) |
| 262 | goto use_pp; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 263 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 264 | if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key)) |
| 265 | goto use_p; |
| 266 | |
| 267 | if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key)) |
| 268 | goto use_pp; |
| 269 | |
| 270 | pkey = KEY_OFFSET(p->node.key); |
| 271 | ppkey = KEY_OFFSET(pp->node.key); |
| 272 | |
| 273 | if (pkey < NOW_OFFSET_BOUNDARY()) |
| 274 | pkey += 0x100000; // key in the future |
| 275 | |
| 276 | if (ppkey < NOW_OFFSET_BOUNDARY()) |
| 277 | ppkey += 0x100000; // key in the future |
| 278 | |
| 279 | if (pkey <= ppkey) |
| 280 | goto use_p; |
| 281 | |
| 282 | use_pp: |
| 283 | /* Let's switch from the server pendconn to the proxy pendconn */ |
| 284 | p = pp; |
| 285 | use_p: |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 286 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 287 | p->strm_flags |= SF_ASSIGNED; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 288 | p->target = srv; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 289 | |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 290 | if (p != pp) |
| 291 | srv->queue_idx++; |
| 292 | else |
| 293 | px->queue_idx++; |
| 294 | |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 295 | HA_ATOMIC_ADD(&srv->served, 1); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 296 | HA_ATOMIC_ADD(&srv->proxy->served, 1); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 297 | if (px->lbprm.server_take_conn) |
| 298 | px->lbprm.server_take_conn(srv); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 299 | __stream_add_srv_conn(p->strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 300 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 301 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 302 | |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 303 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 306 | /* Manages a server's connection queue. This function will try to dequeue as |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 307 | * many pending streams as possible, and wake them up. |
| 308 | */ |
| 309 | void process_srv_queue(struct server *s) |
| 310 | { |
| 311 | struct proxy *p = s->proxy; |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 312 | int maxconn; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 313 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 314 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 315 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 316 | maxconn = srv_dynamic_maxconn(s); |
| 317 | while (s->served < maxconn) { |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 318 | int ret = pendconn_process_next_strm(s, p); |
| 319 | if (!ret) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 320 | break; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 321 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 322 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 323 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 326 | /* Adds the stream <strm> to the pending connection queue of server <strm>->srv |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 327 | * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 328 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 329 | * pendconn itself. If the stream was already marked as served, its flag is |
| 330 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 331 | * The stream's queue position is counted with an offset of -1 because we want |
| 332 | * to make sure that being at the first position in the queue reports 1. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 333 | * |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 334 | * The queue is sorted by the composition of the priority_class, and the current |
| 335 | * timestamp offset by strm->priority_offset. The timestamp is in milliseconds |
| 336 | * and truncated to 20 bits, so will wrap every 17m28s575ms. |
| 337 | * The offset can be positive or negative, and an offset of 0 puts it in the |
| 338 | * middle of this range (~ 8 min). Note that this also means if the adjusted |
| 339 | * timestamp wraps around, the request will be misinterpreted as being of |
Joseph Herlant | d8499ec | 2018-11-25 11:26:48 -0800 | [diff] [blame] | 340 | * the highest priority for that priority class. |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 341 | * |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 342 | * This function must be called by the stream itself, so in the context of |
| 343 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 344 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 345 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | { |
| 347 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 348 | struct proxy *px; |
| 349 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 350 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 351 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | if (!p) |
| 353 | return NULL; |
| 354 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 355 | if (strm->flags & SF_ASSIGNED) |
| 356 | srv = objt_server(strm->target); |
| 357 | else |
| 358 | srv = NULL; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 359 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 360 | px = strm->be; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 361 | p->target = NULL; |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 362 | p->srv = srv; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 363 | p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 364 | p->px = px; |
| 365 | p->strm = strm; |
| 366 | p->strm_flags = strm->flags; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 367 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 368 | pendconn_queue_lock(p); |
| 369 | |
| 370 | if (srv) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 371 | srv->nbpend++; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 372 | if (srv->nbpend > srv->counters.nbpend_max) |
| 373 | srv->counters.nbpend_max = srv->nbpend; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 374 | p->queue_idx = srv->queue_idx - 1; // for increment |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 375 | eb32_insert(&srv->pendconns, &p->node); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 376 | } |
| 377 | else { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 378 | px->nbpend++; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 379 | if (px->nbpend > px->be_counters.nbpend_max) |
| 380 | px->be_counters.nbpend_max = px->nbpend; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 381 | p->queue_idx = px->queue_idx - 1; // for increment |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 382 | eb32_insert(&px->pendconns, &p->node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 383 | } |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 384 | strm->pend_pos = p; |
| 385 | |
| 386 | pendconn_queue_unlock(p); |
| 387 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 388 | HA_ATOMIC_ADD(&px->totpend, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | return p; |
| 390 | } |
| 391 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 392 | /* Redistribute pending connections when a server goes down. The number of |
Willy Tarreau | deca26c | 2018-08-21 18:11:03 +0200 | [diff] [blame] | 393 | * connections redistributed is returned. It must be called with the server |
| 394 | * lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 395 | */ |
| 396 | int pendconn_redistribute(struct server *s) |
| 397 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 398 | struct pendconn *p; |
| 399 | struct eb32_node *node; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 400 | int xferred = 0; |
| 401 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 402 | /* The REDISP option was specified. We will ignore cookie and force to |
| 403 | * balance or use the dispatcher. */ |
| 404 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 405 | return 0; |
| 406 | |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 407 | for (node = eb32_first(&s->pendconns); node; node = eb32_next(node)) { |
| 408 | p = eb32_entry(&node, struct pendconn, node); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 409 | if (p->strm_flags & SF_FORCE_PRST) |
| 410 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 411 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 412 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 413 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 414 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 415 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 416 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 417 | } |
| 418 | return xferred; |
| 419 | } |
| 420 | |
| 421 | /* Check for pending connections at the backend, and assign some of them to |
| 422 | * the server coming up. The server's weight is checked before being assigned |
| 423 | * connections it may not be able to handle. The total number of transferred |
| 424 | * connections is returned. |
| 425 | */ |
| 426 | int pendconn_grab_from_px(struct server *s) |
| 427 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 428 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 429 | int maxconn, xferred = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 430 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 431 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | |
Willy Tarreau | a869465 | 2018-08-07 10:44:58 +0200 | [diff] [blame] | 434 | /* if this is a backup server and there are active servers or at |
| 435 | * least another backup server was elected, then this one must |
| 436 | * not dequeue requests from the proxy. |
| 437 | */ |
| 438 | if ((s->flags & SRV_F_BACKUP) && |
| 439 | (s->proxy->srv_act || |
| 440 | ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK)))) |
| 441 | return 0; |
| 442 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 443 | HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 444 | maxconn = srv_dynamic_maxconn(s); |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 445 | while ((p = pendconn_first(&s->proxy->pendconns))) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 446 | if (s->maxconn && s->served + xferred >= maxconn) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 447 | break; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 448 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 449 | __pendconn_unlink(p); |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 450 | p->target = s; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 451 | |
| 452 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 453 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 454 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 455 | HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 456 | return xferred; |
| 457 | } |
| 458 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 459 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 460 | * always exists here. If the pendconn is still linked to the server or the |
| 461 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 462 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 463 | * is returned. |
| 464 | * |
| 465 | * This function must be called by the stream itself, so in the context of |
| 466 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 467 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 468 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 469 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 470 | struct pendconn *p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 471 | int is_unlinked; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 472 | |
| 473 | if (unlikely(!strm->pend_pos)) { |
| 474 | /* unexpected case because it is called by the stream itself and |
| 475 | * only the stream can release a pendconn. So it is only |
| 476 | * possible if a pendconn is released by someone else or if the |
| 477 | * stream is supposed to be queued but without its associated |
| 478 | * pendconn. In both cases it is a bug! */ |
| 479 | abort(); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 480 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 481 | p = strm->pend_pos; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 482 | |
| 483 | /* note below : we need to grab the queue's lock to check for emptiness |
| 484 | * because we don't want a partial _grab_from_px() or _redistribute() |
| 485 | * to be called in parallel and show an empty list without having the |
| 486 | * time to finish. With this we know that if we see the element |
| 487 | * unlinked, these functions were completely done. |
| 488 | */ |
| 489 | pendconn_queue_lock(p); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 490 | is_unlinked = !p->node.node.leaf_p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 491 | pendconn_queue_unlock(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 492 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 493 | if (!is_unlinked) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 494 | return 1; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 495 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 496 | /* the pendconn is not queued anymore and will not be so we're safe |
| 497 | * to proceed. |
| 498 | */ |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 499 | if (p->target) |
| 500 | strm->target = &p->target->obj_type; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 501 | |
| 502 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 503 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 504 | strm->pend_pos = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 505 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 506 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 509 | static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px, |
| 510 | struct session *sess, struct stream *s, int flags) |
| 511 | { |
| 512 | struct sample *smp; |
| 513 | |
| 514 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 515 | if (!smp) |
| 516 | return ACT_RET_CONT; |
| 517 | |
| 518 | s->priority_class = queue_limit_class(smp->data.u.sint); |
| 519 | return ACT_RET_CONT; |
| 520 | } |
| 521 | |
| 522 | static enum act_return action_set_priority_offset(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_offset = queue_limit_offset(smp->data.u.sint); |
| 532 | |
| 533 | return ACT_RET_CONT; |
| 534 | } |
| 535 | |
| 536 | static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px, |
| 537 | struct act_rule *rule, char **err) |
| 538 | { |
| 539 | unsigned int where = 0; |
| 540 | |
| 541 | rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
| 542 | px->conf.args.line, err, &px->conf.args); |
| 543 | if (!rule->arg.expr) |
| 544 | return ACT_RET_PRS_ERR; |
| 545 | |
| 546 | if (px->cap & PR_CAP_FE) |
| 547 | where |= SMP_VAL_FE_HRQ_HDR; |
| 548 | if (px->cap & PR_CAP_BE) |
| 549 | where |= SMP_VAL_BE_HRQ_HDR; |
| 550 | |
| 551 | if (!(rule->arg.expr->fetch->val & where)) { |
| 552 | memprintf(err, |
| 553 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 554 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 555 | free(rule->arg.expr); |
| 556 | return ACT_RET_PRS_ERR; |
| 557 | } |
| 558 | |
| 559 | rule->action = ACT_CUSTOM; |
| 560 | rule->action_ptr = action_set_priority_class; |
| 561 | return ACT_RET_PRS_OK; |
| 562 | } |
| 563 | |
| 564 | static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px, |
| 565 | struct act_rule *rule, char **err) |
| 566 | { |
| 567 | unsigned int where = 0; |
| 568 | |
| 569 | rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
| 570 | px->conf.args.line, err, &px->conf.args); |
| 571 | if (!rule->arg.expr) |
| 572 | return ACT_RET_PRS_ERR; |
| 573 | |
| 574 | if (px->cap & PR_CAP_FE) |
| 575 | where |= SMP_VAL_FE_HRQ_HDR; |
| 576 | if (px->cap & PR_CAP_BE) |
| 577 | where |= SMP_VAL_BE_HRQ_HDR; |
| 578 | |
| 579 | if (!(rule->arg.expr->fetch->val & where)) { |
| 580 | memprintf(err, |
| 581 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 582 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 583 | free(rule->arg.expr); |
| 584 | return ACT_RET_PRS_ERR; |
| 585 | } |
| 586 | |
| 587 | rule->action = ACT_CUSTOM; |
| 588 | rule->action_ptr = action_set_priority_offset; |
| 589 | return ACT_RET_PRS_OK; |
| 590 | } |
| 591 | |
| 592 | static struct action_kw_list tcp_cont_kws = {ILH, { |
| 593 | { "set-priority-class", parse_set_priority_class }, |
| 594 | { "set-priority-offset", parse_set_priority_offset }, |
| 595 | { /* END */ } |
| 596 | }}; |
| 597 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 598 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_cont_kws); |
| 599 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 600 | static struct action_kw_list http_req_kws = {ILH, { |
| 601 | { "set-priority-class", parse_set_priority_class }, |
| 602 | { "set-priority-offset", parse_set_priority_offset }, |
| 603 | { /* END */ } |
| 604 | }}; |
| 605 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 606 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 607 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 608 | static int |
| 609 | smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 610 | { |
| 611 | if (!smp->strm) |
| 612 | return 0; |
| 613 | |
| 614 | smp->data.type = SMP_T_SINT; |
| 615 | smp->data.u.sint = smp->strm->priority_class; |
| 616 | |
| 617 | return 1; |
| 618 | } |
| 619 | |
| 620 | static int |
| 621 | smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 622 | { |
| 623 | if (!smp->strm) |
| 624 | return 0; |
| 625 | |
| 626 | smp->data.type = SMP_T_SINT; |
| 627 | smp->data.u.sint = smp->strm->priority_offset; |
| 628 | |
| 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 634 | { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 635 | { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 636 | { /* END */}, |
| 637 | }}; |
| 638 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 639 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 640 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Local variables: |
| 643 | * c-indent-level: 8 |
| 644 | * c-basic-offset: 8 |
| 645 | * End: |
| 646 | */ |