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 | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 29 | #include <common/buf.h> |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 31 | #include <common/config.h> |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 32 | #include <common/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 | |
| 36 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 37 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 38 | * acquire a buffer to continue its process. */ |
| 39 | struct buffer_wait { |
| 40 | void *target; /* The waiting object that should be woken up */ |
| 41 | int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */ |
| 42 | struct list list; /* Next element in the <buffer_wq> list */ |
| 43 | }; |
| 44 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 45 | extern struct pool_head *pool_head_buffer; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 46 | extern struct list buffer_wq; |
Willy Tarreau | 53bae85 | 2017-11-26 11:00:37 +0100 | [diff] [blame] | 47 | __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 49 | int init_buffer(); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 50 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 51 | |
| 52 | /*****************************************************************/ |
| 53 | /* These functions are used to compute various buffer area sizes */ |
| 54 | /*****************************************************************/ |
| 55 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 56 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 57 | static inline int buffer_almost_full(const struct buffer *buf) |
| 58 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 59 | if (b_is_null(buf)) |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 60 | return 0; |
| 61 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 62 | return b_almost_full(buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Willy Tarreau | 7b04cc4 | 2018-07-10 10:35:02 +0200 | [diff] [blame] | 65 | /**************************************************/ |
| 66 | /* Functions below are used for buffer allocation */ |
| 67 | /**************************************************/ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 69 | /* Allocates a buffer and assigns it to *buf. If no memory is available, |
| 70 | * ((char *)1) is assigned instead with a zero size. No control is made to |
| 71 | * check if *buf already pointed to another buffer. The allocated buffer is |
| 72 | * returned, or NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 73 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 74 | static inline struct buffer *b_alloc(struct buffer *buf) |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 75 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 76 | char *area; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 78 | *buf = BUF_WANTED; |
| 79 | area = pool_alloc_dirty(pool_head_buffer); |
| 80 | if (unlikely(!area)) |
| 81 | return NULL; |
| 82 | |
| 83 | buf->area = area; |
| 84 | buf->size = pool_head_buffer->size; |
| 85 | return buf; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 88 | /* Allocates a buffer and assigns it to *buf. If no memory is available, |
| 89 | * ((char *)1) is assigned instead with a zero size. No control is made to |
| 90 | * check if *buf already pointed to another buffer. The allocated buffer is |
| 91 | * returned, or NULL in case no memory is available. The difference with |
| 92 | * b_alloc() is that this function only picks from the pool and never calls |
| 93 | * malloc(), so it can fail even if some memory is available. |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 94 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 95 | static inline struct buffer *b_alloc_fast(struct buffer *buf) |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 96 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 97 | char *area; |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 98 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 99 | *buf = BUF_WANTED; |
| 100 | area = pool_get_first(pool_head_buffer); |
| 101 | if (unlikely(!area)) |
| 102 | return NULL; |
| 103 | |
| 104 | buf->area = area; |
| 105 | buf->size = pool_head_buffer->size; |
| 106 | return buf; |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 109 | /* Releases buffer <buf> (no check of emptiness) */ |
| 110 | static inline void __b_drop(struct buffer *buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 111 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 112 | pool_free(pool_head_buffer, buf->area); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 115 | /* Releases buffer <buf> if allocated. */ |
| 116 | static inline void b_drop(struct buffer *buf) |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 117 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 118 | if (buf->size) |
| 119 | __b_drop(buf); |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 122 | /* Releases buffer <buf> if allocated, and marks it empty. */ |
| 123 | static inline void b_free(struct buffer *buf) |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 124 | { |
| 125 | b_drop(buf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 126 | *buf = BUF_NULL; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 129 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 130 | * there are still at least <margin> buffers available in the pool after this |
| 131 | * allocation so that we don't leave the pool in a condition where a session or |
| 132 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 133 | * This means that we sometimes need to try to allocate extra entries even if |
| 134 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 135 | * |
| 136 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 137 | * after the allocation, regardless how many threads that doing it in the same |
| 138 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 139 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 140 | static inline struct buffer *b_alloc_margin(struct buffer *buf, int margin) |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 141 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 142 | char *area; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 143 | ssize_t idx; |
| 144 | unsigned int cached; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 145 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 146 | if (buf->size) |
| 147 | return buf; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 148 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 149 | cached = 0; |
| 150 | idx = pool_get_index(pool_head_buffer); |
| 151 | if (idx >= 0) |
| 152 | cached = pool_cache[idx].count; |
| 153 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 154 | *buf = BUF_WANTED; |
| 155 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 156 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 157 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 158 | #endif |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 159 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 160 | /* fast path */ |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 161 | if ((pool_head_buffer->allocated - pool_head_buffer->used + cached) > margin) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 162 | area = __pool_get_first(pool_head_buffer); |
| 163 | if (likely(area)) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 164 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 165 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 166 | #endif |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 167 | goto done; |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 170 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 171 | /* slow path, uses malloc() */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 172 | area = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 173 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 174 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 175 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 176 | #endif |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 177 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 178 | if (unlikely(!area)) |
| 179 | return NULL; |
| 180 | |
| 181 | done: |
| 182 | buf->area = area; |
| 183 | buf->size = pool_head_buffer->size; |
| 184 | return buf; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 187 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 188 | /* Offer a buffer currently belonging to target <from> to whoever needs one. |
| 189 | * Any pointer is valid for <from>, including NULL. Its purpose is to avoid |
| 190 | * passing a buffer to oneself in case of failed allocations (e.g. need two |
| 191 | * buffers, get one, fail, release it and wake up self again). In case of |
| 192 | * normal buffer release where it is expected that the caller is not waiting |
| 193 | * for a buffer, NULL is fine. |
| 194 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 195 | void __offer_buffer(void *from, unsigned int threshold); |
| 196 | |
| 197 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 198 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 199 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 200 | if (LIST_ISEMPTY(&buffer_wq)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 201 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 202 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 203 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 204 | __offer_buffer(from, threshold); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 205 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 209 | #endif /* _COMMON_BUFFER_H */ |
| 210 | |
| 211 | /* |
| 212 | * Local variables: |
| 213 | * c-indent-level: 8 |
| 214 | * c-basic-offset: 8 |
| 215 | * End: |
| 216 | */ |