blob: d114d469fda944796f0a65a0ced76251f6461d86 [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
Willy Tarreau48b60802021-11-19 17:47:18 +010037 BUG_ON(data_len < 0);
38
William Lallemand4f45bb92017-10-30 20:08:51 +010039 /* not enough usable blocks */
40 if (data_len > shctx->nbav * shctx->block_size)
41 goto out;
Emeric Brun3e541d12012-09-03 11:14:36 +020042
Frédéric Lécailleb7838af2018-10-22 16:21:39 +020043 /* Check the object size limit. */
44 if (shctx->max_obj_size > 0) {
45 if ((first && first->len + data_len > shctx->max_obj_size) ||
46 (!first && data_len > shctx->max_obj_size))
47 goto out;
48 }
49
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020050 /* Note that <remain> is nul only if <first> is not nul. */
51 remain = 1;
52 if (first) {
53 /* Check that there is some block to reserve.
54 * In this first block of code we compute the remaining room in the
55 * current list of block already reserved for this object.
56 * We return asap if there is enough room to copy <data_len> bytes.
57 */
58 last = first->last_reserved;
59 /* Remaining room. */
60 remain = (shctx->block_size * first->block_count - first->len);
61 if (remain) {
62 if (remain > data_len) {
63 return last ? last : first;
64 } else {
65 data_len -= remain;
Willy Tarreaucafe15c2021-11-19 17:42:49 +010066 if (data_len <= 0)
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020067 return last ? last : first;
68 }
69 }
70 }
71
William Lallemand4f45bb92017-10-30 20:08:51 +010072 while (!enough && !LIST_ISEMPTY(&shctx->avail)) {
73 int count = 0;
74 int first_count = 0, first_len = 0;
Emeric Brun3e541d12012-09-03 11:14:36 +020075
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020076 next = block = LIST_NEXT(&shctx->avail, struct shared_block *, list);
William Lallemand4f45bb92017-10-30 20:08:51 +010077 if (ret == NULL)
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020078 ret = next;
Emeric Brun3e541d12012-09-03 11:14:36 +020079
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020080 first_count = next->block_count;
81 first_len = next->len;
William Lallemand4f45bb92017-10-30 20:08:51 +010082 /*
83 Should never been set to 0.
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020084 if (next->block_count == 0)
85 next->block_count = 1;
William Lallemand4f45bb92017-10-30 20:08:51 +010086 */
Emeric Brun3e541d12012-09-03 11:14:36 +020087
William Lallemand4f45bb92017-10-30 20:08:51 +010088 list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) {
89
90 /* release callback */
91 if (first_len && shctx->free_block)
Frédéric Lécaille0bec8072018-10-22 17:55:57 +020092 shctx->free_block(next, block);
William Lallemand4f45bb92017-10-30 20:08:51 +010093
94 block->block_count = 1;
95 block->len = 0;
96
97 freed++;
Willy Tarreau48b60802021-11-19 17:47:18 +010098
99 BUG_ON(data_len < 0);
William Lallemand4f45bb92017-10-30 20:08:51 +0100100 data_len -= shctx->block_size;
101
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200102 if (data_len > 0 || !enough) {
103 if (last) {
104 shctx_block_append_hot(shctx, &last->list, block);
105 last = block;
106 } else {
107 shctx_block_set_hot(shctx, block);
108 }
109 if (!remain) {
110 first->last_append = block;
111 remain = 1;
112 }
113 if (data_len <= 0) {
114 ret->block_count = freed;
115 ret->refcount = 1;
116 ret->last_reserved = block;
117 enough = 1;
Willy Tarreaub15e8a12021-11-19 17:29:23 +0100118 break;
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200119 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100120 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100121 count++;
122 if (count >= first_count)
123 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100124 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100125 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100126
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200127 if (first) {
128 first->block_count += ret->block_count;
129 first->last_reserved = ret->last_reserved;
130 /* Reset this block. */
131 ret->last_reserved = NULL;
132 ret->block_count = 1;
133 ret->refcount = 0;
134 /* Return the first block. */
135 ret = first;
136 }
137
William Lallemand4f45bb92017-10-30 20:08:51 +0100138out:
Emeric Brunaf9619d2012-11-28 18:47:52 +0100139 return ret;
140}
Emeric Brun3e541d12012-09-03 11:14:36 +0200141
William Lallemand4f45bb92017-10-30 20:08:51 +0100142/*
143 * if the refcount is 0 move the row to the hot list. Increment the refcount
Emeric Brunaf9619d2012-11-28 18:47:52 +0100144 */
William Lallemand4f45bb92017-10-30 20:08:51 +0100145void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first)
Emeric Brun3e541d12012-09-03 11:14:36 +0200146{
William Lallemand4f45bb92017-10-30 20:08:51 +0100147 struct shared_block *block, *sblock;
148 int count = 0;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100149
William Lallemand4f45bb92017-10-30 20:08:51 +0100150 if (first->refcount <= 0) {
151
152 block = first;
153
154 list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) {
155
156 shctx_block_set_hot(shctx, block);
157
158 count++;
159 if (count >= first->block_count)
160 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100161 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100162 }
Emeric Brunaf9619d2012-11-28 18:47:52 +0100163
William Lallemand4f45bb92017-10-30 20:08:51 +0100164 first->refcount++;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100165}
Emeric Brun3e541d12012-09-03 11:14:36 +0200166
William Lallemand4f45bb92017-10-30 20:08:51 +0100167/*
168 * 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 +0100169 */
William Lallemand4f45bb92017-10-30 20:08:51 +0100170void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first)
Emeric Brunaf9619d2012-11-28 18:47:52 +0100171{
William Lallemand4f45bb92017-10-30 20:08:51 +0100172 struct shared_block *block, *sblock;
173 int count = 0;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100174
William Lallemand4f45bb92017-10-30 20:08:51 +0100175 first->refcount--;
Emeric Brun3e541d12012-09-03 11:14:36 +0200176
William Lallemand4f45bb92017-10-30 20:08:51 +0100177 if (first->refcount <= 0) {
Emeric Brun3e541d12012-09-03 11:14:36 +0200178
William Lallemand4f45bb92017-10-30 20:08:51 +0100179 block = first;
Emeric Brun3e541d12012-09-03 11:14:36 +0200180
William Lallemand4f45bb92017-10-30 20:08:51 +0100181 list_for_each_entry_safe_from(block, sblock, &shctx->hot, list) {
Emeric Brun3e541d12012-09-03 11:14:36 +0200182
William Lallemand4f45bb92017-10-30 20:08:51 +0100183 shctx_block_set_avail(shctx, block);
Emeric Brun3e541d12012-09-03 11:14:36 +0200184
William Lallemand4f45bb92017-10-30 20:08:51 +0100185 count++;
186 if (count >= first->block_count)
Emeric Brunaf9619d2012-11-28 18:47:52 +0100187 break;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100188 }
189 }
Emeric Brun3e541d12012-09-03 11:14:36 +0200190
William Lallemand4f45bb92017-10-30 20:08:51 +0100191}
192
193
194/*
195 * Append data in the row if there is enough space.
196 * The row should be in the hot list
197 *
198 * Return the amount of appended data if ret >= 0
199 * or how much more space it needs to contains the data if < 0.
200 */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200201int shctx_row_data_append(struct shared_context *shctx,
202 struct shared_block *first, struct shared_block *from,
203 unsigned char *data, int len)
William Lallemand4f45bb92017-10-30 20:08:51 +0100204{
205 int remain, start;
William Lallemand4f45bb92017-10-30 20:08:51 +0100206 struct shared_block *block;
207
William Lallemand4f45bb92017-10-30 20:08:51 +0100208 /* return -<len> needed to work */
209 if (len > first->block_count * shctx->block_size - first->len)
210 return (first->block_count * shctx->block_size - first->len) - len;
211
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200212 block = from ? from : first;
William Lallemand4f45bb92017-10-30 20:08:51 +0100213 list_for_each_entry_from(block, &shctx->hot, list) {
William Lallemand4f45bb92017-10-30 20:08:51 +0100214 /* end of copy */
215 if (len <= 0)
216 break;
217
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200218 /* remaining written bytes in the current block. */
219 remain = (shctx->block_size * first->block_count - first->len) % shctx->block_size;
Willy Tarreau48b60802021-11-19 17:47:18 +0100220 BUG_ON(remain < 0);
221
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200222 /* if remain == 0, previous buffers are full, or first->len == 0 */
223 if (!remain) {
224 remain = shctx->block_size;
225 start = 0;
226 }
227 else {
228 /* start must be calculated before remain is modified */
229 start = shctx->block_size - remain;
Willy Tarreau48b60802021-11-19 17:47:18 +0100230 BUG_ON(start < 0);
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200231 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100232
233 /* must not try to copy more than len */
234 remain = MIN(remain, len);
235
236 memcpy(block->data + start, data, remain);
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200237
William Lallemand4f45bb92017-10-30 20:08:51 +0100238 data += remain;
239 len -= remain;
240 first->len += remain; /* update len in the head of the row */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200241 first->last_append = block;
William Lallemand4f45bb92017-10-30 20:08:51 +0100242 }
243
244 return len;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100245}
Emeric Brun3e541d12012-09-03 11:14:36 +0200246
William Lallemand4f45bb92017-10-30 20:08:51 +0100247/*
248 * Copy <len> data from a row of blocks, return the remaining data to copy
Joseph Herlant39526432018-11-25 11:31:31 -0800249 * If 0 is returned, the full data has successfully been copied
William Lallemand4f45bb92017-10-30 20:08:51 +0100250 *
251 * The row should be in the hot list
252 */
253int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first,
254 unsigned char *dst, int offset, int len)
255{
256 int count = 0, size = 0, start = -1;
257 struct shared_block *block;
258
William Lallemand7217c462017-10-31 20:21:46 +0100259 /* can't copy more */
260 if (len > first->len)
261 len = first->len;
262
William Lallemand4f45bb92017-10-30 20:08:51 +0100263 block = first;
264 count = 0;
265 /* Pass through the blocks to copy them */
266 list_for_each_entry_from(block, &shctx->hot, list) {
267 if (count >= first->block_count || len <= 0)
268 break;
269
270 count++;
271 /* continue until we are in right block
272 corresponding to the offset */
273 if (count < offset / shctx->block_size + 1)
274 continue;
275
276 /* on the first block, data won't possibly began at offset 0 */
277 if (start == -1)
278 start = offset - (count - 1) * shctx->block_size;
Emeric Brun3e541d12012-09-03 11:14:36 +0200279
Willy Tarreau48b60802021-11-19 17:47:18 +0100280 BUG_ON(start < 0);
281
William Lallemand4f45bb92017-10-30 20:08:51 +0100282 /* size can be lower than a block when copying the last block */
283 size = MIN(shctx->block_size - start, len);
Willy Tarreau48b60802021-11-19 17:47:18 +0100284 BUG_ON(size < 0);
William Lallemand4f45bb92017-10-30 20:08:51 +0100285
286 memcpy(dst, block->data + start, size);
287 dst += size;
288 len -= size;
289 start = 0;
290 }
291 return len;
292}
Emeric Brun3e541d12012-09-03 11:14:36 +0200293
Emeric Brun3e541d12012-09-03 11:14:36 +0200294/* Allocate shared memory context.
William Lallemand4f45bb92017-10-30 20:08:51 +0100295 * <maxblocks> is maximum blocks.
296 * If <maxblocks> is set to less or equal to 0, ssl cache is disabled.
297 * Returns: -1 on alloc failure, <maxblocks> if it performs context alloc,
Emeric Brunaf9619d2012-11-28 18:47:52 +0100298 * and 0 if cache is already allocated.
299 */
Frédéric Lécailleb7838af2018-10-22 16:21:39 +0200300int shctx_init(struct shared_context **orig_shctx, int maxblocks, int blocksize,
Frédéric Lécailleb80bc272018-10-25 20:31:40 +0200301 unsigned int maxobjsz, int extra, int shared)
Emeric Brun3e541d12012-09-03 11:14:36 +0200302{
303 int i;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200304 struct shared_context *shctx;
305 int ret;
William Lallemand4f45bb92017-10-30 20:08:51 +0100306 void *cur;
Emeric Brun4b3091e2012-09-24 15:48:52 +0200307 int maptype = MAP_PRIVATE;
Emeric Brun3e541d12012-09-03 11:14:36 +0200308
William Lallemand4f45bb92017-10-30 20:08:51 +0100309 if (maxblocks <= 0)
Emeric Brun22890a12012-12-28 14:41:32 +0100310 return 0;
Emeric Brun3e541d12012-09-03 11:14:36 +0200311
Willy Tarreaua7ddab02020-02-21 13:45:58 +0100312 /* make sure to align the records on a pointer size */
313 blocksize = (blocksize + sizeof(void *) - 1) & -sizeof(void *);
314 extra = (extra + sizeof(void *) - 1) & -sizeof(void *);
315
Willy Tarreau6fd04502021-06-15 16:11:33 +0200316 if (shared) {
Emeric Brun4b3091e2012-09-24 15:48:52 +0200317 maptype = MAP_SHARED;
Willy Tarreau6fd04502021-06-15 16:11:33 +0200318 use_shared_mem = 1;
319 }
Emeric Brun4b3091e2012-09-24 15:48:52 +0200320
William Lallemand4f45bb92017-10-30 20:08:51 +0100321 shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)),
Emeric Brun4b3091e2012-09-24 15:48:52 +0200322 PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0);
Emeric Brun3e541d12012-09-03 11:14:36 +0200323 if (!shctx || shctx == MAP_FAILED) {
324 shctx = NULL;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200325 ret = SHCTX_E_ALLOC_CACHE;
326 goto err;
Emeric Brun3e541d12012-09-03 11:14:36 +0200327 }
328
Willy Tarreau6fd04502021-06-15 16:11:33 +0200329 HA_SPIN_INIT(&shctx->lock);
William Lallemand4f45bb92017-10-30 20:08:51 +0100330 shctx->nbav = 0;
331
William Lallemand4f45bb92017-10-30 20:08:51 +0100332 LIST_INIT(&shctx->avail);
333 LIST_INIT(&shctx->hot);
Emeric Brun3e541d12012-09-03 11:14:36 +0200334
William Lallemand4f45bb92017-10-30 20:08:51 +0100335 shctx->block_size = blocksize;
Frédéric Lécailleb80bc272018-10-25 20:31:40 +0200336 shctx->max_obj_size = maxobjsz == (unsigned int)-1 ? 0 : maxobjsz;
Emeric Brunaf9619d2012-11-28 18:47:52 +0100337
William Lallemand4f45bb92017-10-30 20:08:51 +0100338 /* init the free blocks after the shared context struct */
339 cur = (void *)shctx + sizeof(struct shared_context) + extra;
340 for (i = 0; i < maxblocks; i++) {
341 struct shared_block *cur_block = (struct shared_block *)cur;
342 cur_block->len = 0;
343 cur_block->refcount = 0;
344 cur_block->block_count = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +0200345 LIST_APPEND(&shctx->avail, &cur_block->list);
William Lallemand4f45bb92017-10-30 20:08:51 +0100346 shctx->nbav++;
347 cur += sizeof(struct shared_block) + blocksize;
Emeric Brun3e541d12012-09-03 11:14:36 +0200348 }
William Lallemand4f45bb92017-10-30 20:08:51 +0100349 ret = maxblocks;
William Lallemand3f85c9a2017-10-09 16:30:50 +0200350
351err:
352 *orig_shctx = shctx;
353 return ret;
Emeric Brun3e541d12012-09-03 11:14:36 +0200354}
355