blob: 0ac3d5e8384202609361034ef12832fdf21657e3 [file] [log] [blame]
Emeric Brun3e541d12012-09-03 11:14:36 +02001/*
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 Brunaf9619d2012-11-28 18:47:52 +010015#include <arpa/inet.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020016#include <import/ebmbtree.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020017#include <haproxy/list.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020018#include <haproxy/shctx.h>
William Lallemanded0b5ad2017-10-30 19:36:36 +010019
William Lallemand24a7a752017-10-09 14:17:39 +020020int use_shared_mem = 0;
William Lallemand4f45bb92017-10-30 20:08:51 +010021
William Lallemand4f45bb92017-10-30 20:08:51 +010022/*
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020023 * 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 Lallemand4f45bb92017-10-30 20:08:51 +010025 *
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écaille0bec8072018-10-22 17:55:57 +020029struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx,
30 struct shared_block *first, int data_len)
William Lallemand4f45bb92017-10-30 20:08:51 +010031{
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020032 struct shared_block *last = NULL, *block, *sblock, *ret = NULL, *next;
William Lallemand4f45bb92017-10-30 20:08:51 +010033 int enough = 0;
34 int freed = 0;
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020035 int remain;
William Lallemand4f45bb92017-10-30 20:08:51 +010036
37 /* not enough usable blocks */
38 if (data_len > shctx->nbav * shctx->block_size)
39 goto out;
Emeric Brun3e541d12012-09-03 11:14:36 +020040
Frédéric Lécailleb7838af2018-10-22 16:21:39 +020041 /* 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écaille0bec8072018-10-22 17:55:57 +020048 /* 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 Lallemand4f45bb92017-10-30 20:08:51 +010070 while (!enough && !LIST_ISEMPTY(&shctx->avail)) {
71 int count = 0;
72 int first_count = 0, first_len = 0;
Emeric Brun3e541d12012-09-03 11:14:36 +020073
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020074 next = block = LIST_NEXT(&shctx->avail, struct shared_block *, list);
William Lallemand4f45bb92017-10-30 20:08:51 +010075 if (ret == NULL)
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020076 ret = next;
Emeric Brun3e541d12012-09-03 11:14:36 +020077
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020078 first_count = next->block_count;
79 first_len = next->len;
William Lallemand4f45bb92017-10-30 20:08:51 +010080 /*
81 Should never been set to 0.
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020082 if (next->block_count == 0)
83 next->block_count = 1;
William Lallemand4f45bb92017-10-30 20:08:51 +010084 */
Emeric Brun3e541d12012-09-03 11:14:36 +020085
William Lallemand4f45bb92017-10-30 20:08:51 +010086 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écaille0bec8072018-10-22 17:55:57 +020090 shctx->free_block(next, block);
William Lallemand4f45bb92017-10-30 20:08:51 +010091
92 block->block_count = 1;
93 block->len = 0;
94
95 freed++;
96 data_len -= shctx->block_size;
97
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020098 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;
114 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100115 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100116 count++;
117 if (count >= first_count)
118 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100119 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100120 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100121
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200122 if (first) {
123 first->block_count += ret->block_count;
124 first->last_reserved = ret->last_reserved;
125 /* Reset this block. */
126 ret->last_reserved = NULL;
127 ret->block_count = 1;
128 ret->refcount = 0;
129 /* Return the first block. */
130 ret = first;
131 }
132
William Lallemand4f45bb92017-10-30 20:08:51 +0100133out:
Emeric Brunaf9619d2012-11-28 18:47:52 +0100134 return ret;
135}
Emeric Brun3e541d12012-09-03 11:14:36 +0200136
William Lallemand4f45bb92017-10-30 20:08:51 +0100137/*
138 * if the refcount is 0 move the row to the hot list. Increment the refcount
Emeric Brunaf9619d2012-11-28 18:47:52 +0100139 */
William Lallemand4f45bb92017-10-30 20:08:51 +0100140void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first)
Emeric Brun3e541d12012-09-03 11:14:36 +0200141{
William Lallemand4f45bb92017-10-30 20:08:51 +0100142 struct shared_block *block, *sblock;
143 int count = 0;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100144
William Lallemand4f45bb92017-10-30 20:08:51 +0100145 if (first->refcount <= 0) {
146
147 block = first;
148
149 list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) {
150
151 shctx_block_set_hot(shctx, block);
152
153 count++;
154 if (count >= first->block_count)
155 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100156 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100157 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100158
William Lallemand4f45bb92017-10-30 20:08:51 +0100159 first->refcount++;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100160}
Emeric Brun3e541d12012-09-03 11:14:36 +0200161
William Lallemand4f45bb92017-10-30 20:08:51 +0100162/*
163 * decrement the refcount and move the row at the end of the avail list if it reaches 0.
Emeric Brunaf9619d2012-11-28 18:47:52 +0100164 */
William Lallemand4f45bb92017-10-30 20:08:51 +0100165void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first)
Emeric Brunaf9619d2012-11-28 18:47:52 +0100166{
William Lallemand4f45bb92017-10-30 20:08:51 +0100167 struct shared_block *block, *sblock;
168 int count = 0;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100169
William Lallemand4f45bb92017-10-30 20:08:51 +0100170 first->refcount--;
Emeric Brun3e541d12012-09-03 11:14:36 +0200171
William Lallemand4f45bb92017-10-30 20:08:51 +0100172 if (first->refcount <= 0) {
Emeric Brun3e541d12012-09-03 11:14:36 +0200173
William Lallemand4f45bb92017-10-30 20:08:51 +0100174 block = first;
Emeric Brun3e541d12012-09-03 11:14:36 +0200175
William Lallemand4f45bb92017-10-30 20:08:51 +0100176 list_for_each_entry_safe_from(block, sblock, &shctx->hot, list) {
Emeric Brun3e541d12012-09-03 11:14:36 +0200177
William Lallemand4f45bb92017-10-30 20:08:51 +0100178 shctx_block_set_avail(shctx, block);
Emeric Brun3e541d12012-09-03 11:14:36 +0200179
William Lallemand4f45bb92017-10-30 20:08:51 +0100180 count++;
181 if (count >= first->block_count)
Emeric Brunaf9619d2012-11-28 18:47:52 +0100182 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100183 }
184 }
Emeric Brun3e541d12012-09-03 11:14:36 +0200185
William Lallemand4f45bb92017-10-30 20:08:51 +0100186}
187
188
189/*
190 * Append data in the row if there is enough space.
191 * The row should be in the hot list
192 *
193 * Return the amount of appended data if ret >= 0
194 * or how much more space it needs to contains the data if < 0.
195 */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200196int shctx_row_data_append(struct shared_context *shctx,
197 struct shared_block *first, struct shared_block *from,
198 unsigned char *data, int len)
William Lallemand4f45bb92017-10-30 20:08:51 +0100199{
200 int remain, start;
William Lallemand4f45bb92017-10-30 20:08:51 +0100201 struct shared_block *block;
202
William Lallemand4f45bb92017-10-30 20:08:51 +0100203 /* return -<len> needed to work */
204 if (len > first->block_count * shctx->block_size - first->len)
205 return (first->block_count * shctx->block_size - first->len) - len;
206
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200207 block = from ? from : first;
William Lallemand4f45bb92017-10-30 20:08:51 +0100208 list_for_each_entry_from(block, &shctx->hot, list) {
William Lallemand4f45bb92017-10-30 20:08:51 +0100209 /* end of copy */
210 if (len <= 0)
211 break;
212
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200213 /* remaining written bytes in the current block. */
214 remain = (shctx->block_size * first->block_count - first->len) % shctx->block_size;
215 /* if remain == 0, previous buffers are full, or first->len == 0 */
216 if (!remain) {
217 remain = shctx->block_size;
218 start = 0;
219 }
220 else {
221 /* start must be calculated before remain is modified */
222 start = shctx->block_size - remain;
223 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100224
225 /* must not try to copy more than len */
226 remain = MIN(remain, len);
227
228 memcpy(block->data + start, data, remain);
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200229
William Lallemand4f45bb92017-10-30 20:08:51 +0100230 data += remain;
231 len -= remain;
232 first->len += remain; /* update len in the head of the row */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200233 first->last_append = block;
William Lallemand4f45bb92017-10-30 20:08:51 +0100234 }
235
236 return len;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100237}
Emeric Brun3e541d12012-09-03 11:14:36 +0200238
William Lallemand4f45bb92017-10-30 20:08:51 +0100239/*
240 * Copy <len> data from a row of blocks, return the remaining data to copy
Joseph Herlant39526432018-11-25 11:31:31 -0800241 * If 0 is returned, the full data has successfully been copied
William Lallemand4f45bb92017-10-30 20:08:51 +0100242 *
243 * The row should be in the hot list
244 */
245int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first,
246 unsigned char *dst, int offset, int len)
247{
248 int count = 0, size = 0, start = -1;
249 struct shared_block *block;
250
William Lallemand7217c462017-10-31 20:21:46 +0100251 /* can't copy more */
252 if (len > first->len)
253 len = first->len;
254
William Lallemand4f45bb92017-10-30 20:08:51 +0100255 block = first;
256 count = 0;
257 /* Pass through the blocks to copy them */
258 list_for_each_entry_from(block, &shctx->hot, list) {
259 if (count >= first->block_count || len <= 0)
260 break;
261
262 count++;
263 /* continue until we are in right block
264 corresponding to the offset */
265 if (count < offset / shctx->block_size + 1)
266 continue;
267
268 /* on the first block, data won't possibly began at offset 0 */
269 if (start == -1)
270 start = offset - (count - 1) * shctx->block_size;
Emeric Brun3e541d12012-09-03 11:14:36 +0200271
William Lallemand4f45bb92017-10-30 20:08:51 +0100272 /* size can be lower than a block when copying the last block */
273 size = MIN(shctx->block_size - start, len);
274
275 memcpy(dst, block->data + start, size);
276 dst += size;
277 len -= size;
278 start = 0;
279 }
280 return len;
281}
Emeric Brun3e541d12012-09-03 11:14:36 +0200282
Emeric Brun3e541d12012-09-03 11:14:36 +0200283/* Allocate shared memory context.
William Lallemand4f45bb92017-10-30 20:08:51 +0100284 * <maxblocks> is maximum blocks.
285 * If <maxblocks> is set to less or equal to 0, ssl cache is disabled.
286 * Returns: -1 on alloc failure, <maxblocks> if it performs context alloc,
Emeric Brunaf9619d2012-11-28 18:47:52 +0100287 * and 0 if cache is already allocated.
288 */
Frédéric Lécailleb7838af2018-10-22 16:21:39 +0200289int shctx_init(struct shared_context **orig_shctx, int maxblocks, int blocksize,
Frédéric Lécailleb80bc272018-10-25 20:31:40 +0200290 unsigned int maxobjsz, int extra, int shared)
Emeric Brun3e541d12012-09-03 11:14:36 +0200291{
292 int i;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200293 struct shared_context *shctx;
294 int ret;
Emeric Bruncd1a5262014-05-07 23:11:42 +0200295#ifdef USE_PTHREAD_PSHARED
Emeric Brun3e541d12012-09-03 11:14:36 +0200296 pthread_mutexattr_t attr;
Emeric Bruncd1a5262014-05-07 23:11:42 +0200297#endif
William Lallemand4f45bb92017-10-30 20:08:51 +0100298 void *cur;
Emeric Brun4b3091e2012-09-24 15:48:52 +0200299 int maptype = MAP_PRIVATE;
Emeric Brun3e541d12012-09-03 11:14:36 +0200300
William Lallemand4f45bb92017-10-30 20:08:51 +0100301 if (maxblocks <= 0)
Emeric Brun22890a12012-12-28 14:41:32 +0100302 return 0;
Emeric Brun3e541d12012-09-03 11:14:36 +0200303
Willy Tarreaua7ddab02020-02-21 13:45:58 +0100304 /* make sure to align the records on a pointer size */
305 blocksize = (blocksize + sizeof(void *) - 1) & -sizeof(void *);
306 extra = (extra + sizeof(void *) - 1) & -sizeof(void *);
307
Emeric Brun4b3091e2012-09-24 15:48:52 +0200308 if (shared)
309 maptype = MAP_SHARED;
310
William Lallemand4f45bb92017-10-30 20:08:51 +0100311 shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)),
Emeric Brun4b3091e2012-09-24 15:48:52 +0200312 PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0);
Emeric Brun3e541d12012-09-03 11:14:36 +0200313 if (!shctx || shctx == MAP_FAILED) {
314 shctx = NULL;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200315 ret = SHCTX_E_ALLOC_CACHE;
316 goto err;
Emeric Brun3e541d12012-09-03 11:14:36 +0200317 }
318
William Lallemand4f45bb92017-10-30 20:08:51 +0100319 shctx->nbav = 0;
320
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200321 if (maptype == MAP_SHARED) {
Willy Tarreauca367712021-06-15 15:03:19 +0200322#ifndef USE_PRIVATE_CACHE
Emeric Bruncd1a5262014-05-07 23:11:42 +0200323#ifdef USE_PTHREAD_PSHARED
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200324 if (pthread_mutexattr_init(&attr)) {
William Lallemand4f45bb92017-10-30 20:08:51 +0100325 munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)));
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200326 shctx = NULL;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200327 ret = SHCTX_E_INIT_LOCK;
328 goto err;
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200329 }
330
331 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
332 pthread_mutexattr_destroy(&attr);
William Lallemand4f45bb92017-10-30 20:08:51 +0100333 munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)));
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200334 shctx = NULL;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200335 ret = SHCTX_E_INIT_LOCK;
336 goto err;
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200337 }
338
339 if (pthread_mutex_init(&shctx->mutex, &attr)) {
340 pthread_mutexattr_destroy(&attr);
William Lallemand4f45bb92017-10-30 20:08:51 +0100341 munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)));
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200342 shctx = NULL;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200343 ret = SHCTX_E_INIT_LOCK;
344 goto err;
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200345 }
Emeric Bruncd1a5262014-05-07 23:11:42 +0200346#else
347 shctx->waiters = 0;
Emeric Brun3e541d12012-09-03 11:14:36 +0200348#endif
Willy Tarreauca367712021-06-15 15:03:19 +0200349#else
350 HA_SPIN_INIT(&shctx->lock);
351#endif
Emeric Brun4b3091e2012-09-24 15:48:52 +0200352 use_shared_mem = 1;
Emeric Bruncaa19cc2014-05-07 16:10:18 +0200353 }
Emeric Brun4b3091e2012-09-24 15:48:52 +0200354
William Lallemand4f45bb92017-10-30 20:08:51 +0100355 LIST_INIT(&shctx->avail);
356 LIST_INIT(&shctx->hot);
Emeric Brun3e541d12012-09-03 11:14:36 +0200357
William Lallemand4f45bb92017-10-30 20:08:51 +0100358 shctx->block_size = blocksize;
Frédéric Lécailleb80bc272018-10-25 20:31:40 +0200359 shctx->max_obj_size = maxobjsz == (unsigned int)-1 ? 0 : maxobjsz;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100360
William Lallemand4f45bb92017-10-30 20:08:51 +0100361 /* init the free blocks after the shared context struct */
362 cur = (void *)shctx + sizeof(struct shared_context) + extra;
363 for (i = 0; i < maxblocks; i++) {
364 struct shared_block *cur_block = (struct shared_block *)cur;
365 cur_block->len = 0;
366 cur_block->refcount = 0;
367 cur_block->block_count = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +0200368 LIST_APPEND(&shctx->avail, &cur_block->list);
William Lallemand4f45bb92017-10-30 20:08:51 +0100369 shctx->nbav++;
370 cur += sizeof(struct shared_block) + blocksize;
Emeric Brun3e541d12012-09-03 11:14:36 +0200371 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100372 ret = maxblocks;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200373
374err:
375 *orig_shctx = shctx;
376 return ret;
Emeric Brun3e541d12012-09-03 11:14:36 +0200377}
378