Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/buffer.h |
| 3 | * Buffer management definitions, macros and inline functions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_BUFFER_H |
| 23 | #define _COMMON_BUFFER_H |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 29 | #include <haproxy/api.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame^] | 30 | #include <haproxy/buf.h> |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 31 | #include <common/chunk.h> |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 32 | #include <import/ist.h> |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 33 | #include <common/istbuf.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | a8b2ce0 | 2019-05-28 17:04:16 +0200 | [diff] [blame] | 36 | #include <proto/activity.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 37 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 38 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 39 | * acquire a buffer to continue its process. */ |
| 40 | struct buffer_wait { |
| 41 | void *target; /* The waiting object that should be woken up */ |
| 42 | int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */ |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 43 | struct mt_list list; /* Next element in the <buffer_wq> list */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 46 | extern struct pool_head *pool_head_buffer; |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 47 | extern struct mt_list buffer_wq; |
Willy Tarreau | 53bae85 | 2017-11-26 11:00:37 +0100 | [diff] [blame] | 48 | __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 50 | int init_buffer(); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 51 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 52 | |
| 53 | /*****************************************************************/ |
| 54 | /* These functions are used to compute various buffer area sizes */ |
| 55 | /*****************************************************************/ |
| 56 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 57 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 58 | static inline int buffer_almost_full(const struct buffer *buf) |
| 59 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 60 | if (b_is_null(buf)) |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 61 | return 0; |
| 62 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 63 | return b_almost_full(buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Willy Tarreau | 7b04cc4 | 2018-07-10 10:35:02 +0200 | [diff] [blame] | 66 | /**************************************************/ |
| 67 | /* Functions below are used for buffer allocation */ |
| 68 | /**************************************************/ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 70 | /* Allocates a buffer and assigns it to *buf. If no memory is available, |
| 71 | * ((char *)1) is assigned instead with a zero size. No control is made to |
| 72 | * check if *buf already pointed to another buffer. The allocated buffer is |
| 73 | * returned, or NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 74 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 75 | static inline struct buffer *b_alloc(struct buffer *buf) |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 76 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 77 | char *area; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 79 | *buf = BUF_WANTED; |
| 80 | area = pool_alloc_dirty(pool_head_buffer); |
Willy Tarreau | a8b2ce0 | 2019-05-28 17:04:16 +0200 | [diff] [blame] | 81 | if (unlikely(!area)) { |
| 82 | activity[tid].buf_wait++; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 83 | return NULL; |
Willy Tarreau | a8b2ce0 | 2019-05-28 17:04:16 +0200 | [diff] [blame] | 84 | } |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 85 | |
| 86 | buf->area = area; |
| 87 | buf->size = pool_head_buffer->size; |
| 88 | return buf; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 91 | /* Allocates a buffer and assigns it to *buf. If no memory is available, |
| 92 | * ((char *)1) is assigned instead with a zero size. No control is made to |
| 93 | * check if *buf already pointed to another buffer. The allocated buffer is |
| 94 | * returned, or NULL in case no memory is available. The difference with |
| 95 | * b_alloc() is that this function only picks from the pool and never calls |
| 96 | * malloc(), so it can fail even if some memory is available. |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 97 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 98 | static inline struct buffer *b_alloc_fast(struct buffer *buf) |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 99 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 100 | char *area; |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 101 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 102 | *buf = BUF_WANTED; |
| 103 | area = pool_get_first(pool_head_buffer); |
| 104 | if (unlikely(!area)) |
| 105 | return NULL; |
| 106 | |
| 107 | buf->area = area; |
| 108 | buf->size = pool_head_buffer->size; |
| 109 | return buf; |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Willy Tarreau | 3b091f8 | 2019-08-08 07:53:20 +0200 | [diff] [blame] | 112 | /* Releases buffer <buf> (no check of emptiness). The buffer's head is marked |
| 113 | * empty. |
| 114 | */ |
Willy Tarreau | e0d0b40 | 2019-08-08 08:06:27 +0200 | [diff] [blame] | 115 | static inline void __b_free(struct buffer *buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 116 | { |
Willy Tarreau | 3b091f8 | 2019-08-08 07:53:20 +0200 | [diff] [blame] | 117 | char *area = buf->area; |
| 118 | |
| 119 | /* let's first clear the area to save an occasional "show sess all" |
| 120 | * glancing over our shoulder from getting a dangling pointer. |
| 121 | */ |
| 122 | *buf = BUF_NULL; |
| 123 | __ha_barrier_store(); |
| 124 | pool_free(pool_head_buffer, area); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Willy Tarreau | 3b091f8 | 2019-08-08 07:53:20 +0200 | [diff] [blame] | 127 | /* Releases buffer <buf> if allocated, and marks it empty. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 128 | static inline void b_free(struct buffer *buf) |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 129 | { |
Willy Tarreau | e0d0b40 | 2019-08-08 08:06:27 +0200 | [diff] [blame] | 130 | if (buf->size) |
| 131 | __b_free(buf); |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 134 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 135 | * there are still at least <margin> buffers available in the pool after this |
| 136 | * allocation so that we don't leave the pool in a condition where a session or |
| 137 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 138 | * This means that we sometimes need to try to allocate extra entries even if |
| 139 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 140 | * |
| 141 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 142 | * after the allocation, regardless how many threads that doing it in the same |
| 143 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 144 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 145 | static inline struct buffer *b_alloc_margin(struct buffer *buf, int margin) |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 146 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 147 | char *area; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 148 | ssize_t idx; |
| 149 | unsigned int cached; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 150 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 151 | if (buf->size) |
| 152 | return buf; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 153 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 154 | cached = 0; |
| 155 | idx = pool_get_index(pool_head_buffer); |
| 156 | if (idx >= 0) |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 157 | cached = pool_cache[tid][idx].count; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 159 | *buf = BUF_WANTED; |
| 160 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 161 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 162 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 163 | #endif |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 164 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 165 | /* fast path */ |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 166 | if ((pool_head_buffer->allocated - pool_head_buffer->used + cached) > margin) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 167 | area = __pool_get_first(pool_head_buffer); |
| 168 | if (likely(area)) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 169 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 170 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 171 | #endif |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 172 | goto done; |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 175 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 176 | /* slow path, uses malloc() */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 177 | area = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 178 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 179 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 180 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 181 | #endif |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 182 | |
Willy Tarreau | a8b2ce0 | 2019-05-28 17:04:16 +0200 | [diff] [blame] | 183 | if (unlikely(!area)) { |
| 184 | activity[tid].buf_wait++; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 185 | return NULL; |
Willy Tarreau | a8b2ce0 | 2019-05-28 17:04:16 +0200 | [diff] [blame] | 186 | } |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 187 | |
| 188 | done: |
| 189 | buf->area = area; |
| 190 | buf->size = pool_head_buffer->size; |
| 191 | return buf; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 194 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 195 | /* Offer a buffer currently belonging to target <from> to whoever needs one. |
| 196 | * Any pointer is valid for <from>, including NULL. Its purpose is to avoid |
| 197 | * passing a buffer to oneself in case of failed allocations (e.g. need two |
| 198 | * buffers, get one, fail, release it and wake up self again). In case of |
| 199 | * normal buffer release where it is expected that the caller is not waiting |
| 200 | * for a buffer, NULL is fine. |
| 201 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 202 | void __offer_buffer(void *from, unsigned int threshold); |
| 203 | |
| 204 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 205 | { |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 206 | if (!MT_LIST_ISEMPTY(&buffer_wq)) |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 207 | __offer_buffer(from, threshold); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 211 | #endif /* _COMMON_BUFFER_H */ |
| 212 | |
| 213 | /* |
| 214 | * Local variables: |
| 215 | * c-indent-level: 8 |
| 216 | * c-basic-offset: 8 |
| 217 | * End: |
| 218 | */ |