Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * shctx.c - shared context management functions for SSL |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 EXCELIANCE |
| 5 | * |
| 6 | * Author: Emeric Brun - emeric@exceliance.fr |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <sys/mman.h> |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 15 | #include <arpa/inet.h> |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 16 | #include <import/ebmbtree.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 17 | #include <haproxy/list.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 18 | #include <haproxy/shctx.h> |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 19 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 20 | int use_shared_mem = 0; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 21 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 22 | /* |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 23 | * Reserve a new row if <first> is null, put it in the hotlist, set the refcount to 1 |
| 24 | * or append new blocks to the row with <first> as first block if non null. |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 25 | * |
| 26 | * Reserve blocks in the avail list and put them in the hot list |
| 27 | * Return the first block put in the hot list or NULL if not enough blocks available |
| 28 | */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 29 | struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx, |
| 30 | struct shared_block *first, int data_len) |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 31 | { |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 32 | struct shared_block *last = NULL, *block, *sblock, *ret = NULL, *next; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 33 | int enough = 0; |
| 34 | int freed = 0; |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 35 | int remain; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 36 | |
| 37 | /* not enough usable blocks */ |
| 38 | if (data_len > shctx->nbav * shctx->block_size) |
| 39 | goto out; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 40 | |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 41 | /* Check the object size limit. */ |
| 42 | if (shctx->max_obj_size > 0) { |
| 43 | if ((first && first->len + data_len > shctx->max_obj_size) || |
| 44 | (!first && data_len > shctx->max_obj_size)) |
| 45 | goto out; |
| 46 | } |
| 47 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 48 | /* Note that <remain> is nul only if <first> is not nul. */ |
| 49 | remain = 1; |
| 50 | if (first) { |
| 51 | /* Check that there is some block to reserve. |
| 52 | * In this first block of code we compute the remaining room in the |
| 53 | * current list of block already reserved for this object. |
| 54 | * We return asap if there is enough room to copy <data_len> bytes. |
| 55 | */ |
| 56 | last = first->last_reserved; |
| 57 | /* Remaining room. */ |
| 58 | remain = (shctx->block_size * first->block_count - first->len); |
| 59 | if (remain) { |
| 60 | if (remain > data_len) { |
| 61 | return last ? last : first; |
| 62 | } else { |
| 63 | data_len -= remain; |
| 64 | if (!data_len) |
| 65 | return last ? last : first; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 70 | while (!enough && !LIST_ISEMPTY(&shctx->avail)) { |
| 71 | int count = 0; |
| 72 | int first_count = 0, first_len = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 73 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 74 | next = block = LIST_NEXT(&shctx->avail, struct shared_block *, list); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 75 | if (ret == NULL) |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 76 | ret = next; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 77 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 78 | first_count = next->block_count; |
| 79 | first_len = next->len; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 80 | /* |
| 81 | Should never been set to 0. |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 82 | if (next->block_count == 0) |
| 83 | next->block_count = 1; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 84 | */ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 85 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 86 | list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) { |
| 87 | |
| 88 | /* release callback */ |
| 89 | if (first_len && shctx->free_block) |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 90 | shctx->free_block(next, block); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 91 | |
| 92 | block->block_count = 1; |
| 93 | block->len = 0; |
| 94 | |
| 95 | freed++; |
| 96 | data_len -= shctx->block_size; |
| 97 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 98 | if (data_len > 0 || !enough) { |
| 99 | if (last) { |
| 100 | shctx_block_append_hot(shctx, &last->list, block); |
| 101 | last = block; |
| 102 | } else { |
| 103 | shctx_block_set_hot(shctx, block); |
| 104 | } |
| 105 | if (!remain) { |
| 106 | first->last_append = block; |
| 107 | remain = 1; |
| 108 | } |
| 109 | if (data_len <= 0) { |
| 110 | ret->block_count = freed; |
| 111 | ret->refcount = 1; |
| 112 | ret->last_reserved = block; |
| 113 | enough = 1; |
Willy Tarreau | ca4d7da | 2021-11-19 17:29:23 +0100 | [diff] [blame^] | 114 | break; |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 115 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 116 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 117 | count++; |
| 118 | if (count >= first_count) |
| 119 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 120 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 121 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 122 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 123 | if (first) { |
| 124 | first->block_count += ret->block_count; |
| 125 | first->last_reserved = ret->last_reserved; |
| 126 | /* Reset this block. */ |
| 127 | ret->last_reserved = NULL; |
| 128 | ret->block_count = 1; |
| 129 | ret->refcount = 0; |
| 130 | /* Return the first block. */ |
| 131 | ret = first; |
| 132 | } |
| 133 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 134 | out: |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 135 | return ret; |
| 136 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 137 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 138 | /* |
| 139 | * if the refcount is 0 move the row to the hot list. Increment the refcount |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 140 | */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 141 | void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 142 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 143 | struct shared_block *block, *sblock; |
| 144 | int count = 0; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 145 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 146 | if (first->refcount <= 0) { |
| 147 | |
| 148 | block = first; |
| 149 | |
| 150 | list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) { |
| 151 | |
| 152 | shctx_block_set_hot(shctx, block); |
| 153 | |
| 154 | count++; |
| 155 | if (count >= first->block_count) |
| 156 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 157 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 158 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 159 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 160 | first->refcount++; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 161 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 162 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 163 | /* |
| 164 | * decrement the refcount and move the row at the end of the avail list if it reaches 0. |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 165 | */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 166 | void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first) |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 167 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 168 | struct shared_block *block, *sblock; |
| 169 | int count = 0; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 170 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 171 | first->refcount--; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 172 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 173 | if (first->refcount <= 0) { |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 174 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 175 | block = first; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 176 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 177 | list_for_each_entry_safe_from(block, sblock, &shctx->hot, list) { |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 178 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 179 | shctx_block_set_avail(shctx, block); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 180 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 181 | count++; |
| 182 | if (count >= first->block_count) |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 183 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 186 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | /* |
| 191 | * Append data in the row if there is enough space. |
| 192 | * The row should be in the hot list |
| 193 | * |
| 194 | * Return the amount of appended data if ret >= 0 |
| 195 | * or how much more space it needs to contains the data if < 0. |
| 196 | */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 197 | int shctx_row_data_append(struct shared_context *shctx, |
| 198 | struct shared_block *first, struct shared_block *from, |
| 199 | unsigned char *data, int len) |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 200 | { |
| 201 | int remain, start; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 202 | struct shared_block *block; |
| 203 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 204 | /* return -<len> needed to work */ |
| 205 | if (len > first->block_count * shctx->block_size - first->len) |
| 206 | return (first->block_count * shctx->block_size - first->len) - len; |
| 207 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 208 | block = from ? from : first; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 209 | list_for_each_entry_from(block, &shctx->hot, list) { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 210 | /* end of copy */ |
| 211 | if (len <= 0) |
| 212 | break; |
| 213 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 214 | /* remaining written bytes in the current block. */ |
| 215 | remain = (shctx->block_size * first->block_count - first->len) % shctx->block_size; |
| 216 | /* if remain == 0, previous buffers are full, or first->len == 0 */ |
| 217 | if (!remain) { |
| 218 | remain = shctx->block_size; |
| 219 | start = 0; |
| 220 | } |
| 221 | else { |
| 222 | /* start must be calculated before remain is modified */ |
| 223 | start = shctx->block_size - remain; |
| 224 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 225 | |
| 226 | /* must not try to copy more than len */ |
| 227 | remain = MIN(remain, len); |
| 228 | |
| 229 | memcpy(block->data + start, data, remain); |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 230 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 231 | data += remain; |
| 232 | len -= remain; |
| 233 | first->len += remain; /* update len in the head of the row */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 234 | first->last_append = block; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return len; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 238 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 239 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 240 | /* |
| 241 | * Copy <len> data from a row of blocks, return the remaining data to copy |
Joseph Herlant | 3952643 | 2018-11-25 11:31:31 -0800 | [diff] [blame] | 242 | * If 0 is returned, the full data has successfully been copied |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 243 | * |
| 244 | * The row should be in the hot list |
| 245 | */ |
| 246 | int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first, |
| 247 | unsigned char *dst, int offset, int len) |
| 248 | { |
| 249 | int count = 0, size = 0, start = -1; |
| 250 | struct shared_block *block; |
| 251 | |
William Lallemand | 7217c46 | 2017-10-31 20:21:46 +0100 | [diff] [blame] | 252 | /* can't copy more */ |
| 253 | if (len > first->len) |
| 254 | len = first->len; |
| 255 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 256 | block = first; |
| 257 | count = 0; |
| 258 | /* Pass through the blocks to copy them */ |
| 259 | list_for_each_entry_from(block, &shctx->hot, list) { |
| 260 | if (count >= first->block_count || len <= 0) |
| 261 | break; |
| 262 | |
| 263 | count++; |
| 264 | /* continue until we are in right block |
| 265 | corresponding to the offset */ |
| 266 | if (count < offset / shctx->block_size + 1) |
| 267 | continue; |
| 268 | |
| 269 | /* on the first block, data won't possibly began at offset 0 */ |
| 270 | if (start == -1) |
| 271 | start = offset - (count - 1) * shctx->block_size; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 272 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 273 | /* size can be lower than a block when copying the last block */ |
| 274 | size = MIN(shctx->block_size - start, len); |
| 275 | |
| 276 | memcpy(dst, block->data + start, size); |
| 277 | dst += size; |
| 278 | len -= size; |
| 279 | start = 0; |
| 280 | } |
| 281 | return len; |
| 282 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 283 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 284 | /* Allocate shared memory context. |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 285 | * <maxblocks> is maximum blocks. |
| 286 | * If <maxblocks> is set to less or equal to 0, ssl cache is disabled. |
| 287 | * Returns: -1 on alloc failure, <maxblocks> if it performs context alloc, |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 288 | * and 0 if cache is already allocated. |
| 289 | */ |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 290 | int shctx_init(struct shared_context **orig_shctx, int maxblocks, int blocksize, |
Frédéric Lécaille | b80bc27 | 2018-10-25 20:31:40 +0200 | [diff] [blame] | 291 | unsigned int maxobjsz, int extra, int shared) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 292 | { |
| 293 | int i; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 294 | struct shared_context *shctx; |
| 295 | int ret; |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 296 | #ifdef USE_PTHREAD_PSHARED |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 297 | pthread_mutexattr_t attr; |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 298 | #endif |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 299 | void *cur; |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 300 | int maptype = MAP_PRIVATE; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 301 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 302 | if (maxblocks <= 0) |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 303 | return 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 304 | |
Willy Tarreau | a7ddab0 | 2020-02-21 13:45:58 +0100 | [diff] [blame] | 305 | /* make sure to align the records on a pointer size */ |
| 306 | blocksize = (blocksize + sizeof(void *) - 1) & -sizeof(void *); |
| 307 | extra = (extra + sizeof(void *) - 1) & -sizeof(void *); |
| 308 | |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 309 | if (shared) |
| 310 | maptype = MAP_SHARED; |
| 311 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 312 | shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)), |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 313 | PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 314 | if (!shctx || shctx == MAP_FAILED) { |
| 315 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 316 | ret = SHCTX_E_ALLOC_CACHE; |
| 317 | goto err; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 318 | } |
| 319 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 320 | shctx->nbav = 0; |
| 321 | |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 322 | if (maptype == MAP_SHARED) { |
Willy Tarreau | ca36771 | 2021-06-15 15:03:19 +0200 | [diff] [blame] | 323 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 324 | #ifdef USE_PTHREAD_PSHARED |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 325 | if (pthread_mutexattr_init(&attr)) { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 326 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 327 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 328 | ret = SHCTX_E_INIT_LOCK; |
| 329 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) { |
| 333 | pthread_mutexattr_destroy(&attr); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 334 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 335 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 336 | ret = SHCTX_E_INIT_LOCK; |
| 337 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (pthread_mutex_init(&shctx->mutex, &attr)) { |
| 341 | pthread_mutexattr_destroy(&attr); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 342 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 343 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 344 | ret = SHCTX_E_INIT_LOCK; |
| 345 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 346 | } |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 347 | #else |
| 348 | shctx->waiters = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 349 | #endif |
Willy Tarreau | ca36771 | 2021-06-15 15:03:19 +0200 | [diff] [blame] | 350 | #else |
| 351 | HA_SPIN_INIT(&shctx->lock); |
| 352 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 353 | use_shared_mem = 1; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 354 | } |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 355 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 356 | LIST_INIT(&shctx->avail); |
| 357 | LIST_INIT(&shctx->hot); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 358 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 359 | shctx->block_size = blocksize; |
Frédéric Lécaille | b80bc27 | 2018-10-25 20:31:40 +0200 | [diff] [blame] | 360 | shctx->max_obj_size = maxobjsz == (unsigned int)-1 ? 0 : maxobjsz; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 361 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 362 | /* init the free blocks after the shared context struct */ |
| 363 | cur = (void *)shctx + sizeof(struct shared_context) + extra; |
| 364 | for (i = 0; i < maxblocks; i++) { |
| 365 | struct shared_block *cur_block = (struct shared_block *)cur; |
| 366 | cur_block->len = 0; |
| 367 | cur_block->refcount = 0; |
| 368 | cur_block->block_count = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 369 | LIST_APPEND(&shctx->avail, &cur_block->list); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 370 | shctx->nbav++; |
| 371 | cur += sizeof(struct shared_block) + blocksize; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 372 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 373 | ret = maxblocks; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 374 | |
| 375 | err: |
| 376 | *orig_shctx = shctx; |
| 377 | return ret; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 378 | } |
| 379 | |