blob: 1d275edac7b8f42f9d3c979f084c9e4460a0ef30 [file] [log] [blame]
William Lallemand82fe75c2012-10-23 10:25:10 +02001/*
2 * HTTP compression.
3 *
4 * Copyright 2012 Exceliance, David Du Colombier <dducolombier@exceliance.fr>
5 * William Lallemand <wlallemand@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <stdio.h>
Willy Tarreau34763642012-10-26 15:05:35 +020015
Willy Tarreau418b8c02015-03-29 03:32:06 +020016#if defined(USE_SLZ)
17#include <slz.h>
18#elif defined(USE_ZLIB)
Willy Tarreau34763642012-10-26 15:05:35 +020019/* Note: the crappy zlib and openssl libs both define the "free_func" type.
20 * That's a very clever idea to use such a generic name in general purpose
21 * libraries, really... The zlib one is easier to redefine than openssl's,
22 * so let's only fix this one.
23 */
24#define free_func zlib_free_func
William Lallemand82fe75c2012-10-23 10:25:10 +020025#include <zlib.h>
Willy Tarreau34763642012-10-26 15:05:35 +020026#undef free_func
William Lallemand08289f12012-10-31 11:19:18 +010027#endif /* USE_ZLIB */
William Lallemand82fe75c2012-10-23 10:25:10 +020028
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020030#include <haproxy/cfgparse.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020031#include <haproxy/compression-t.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020032#include <haproxy/compression.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/dynbuf.h>
Willy Tarreau66347942020-06-01 12:18:08 +020034#include <haproxy/freq_ctr.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/global.h>
36#include <haproxy/pool.h>
37#include <haproxy/stream.h>
38#include <haproxy/thread.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020039
William Lallemand2b502472012-10-30 14:30:39 +010040
Willy Tarreaue5489742018-11-26 14:44:03 +010041#if defined(USE_ZLIB)
Willy Tarreau86abe442018-11-25 20:12:18 +010042__decl_spinlock(comp_pool_lock);
Emeric Brun11f58862017-11-07 11:57:54 +010043#endif
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020044
William Lallemand2b502472012-10-30 14:30:39 +010045#ifdef USE_ZLIB
46
William Lallemand8b52bb32012-11-16 18:06:41 +010047static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size);
48static void free_zlib(void *opaque, void *ptr);
49
William Lallemand2b502472012-10-30 14:30:39 +010050/* zlib allocation */
Willy Tarreauff882702021-04-10 17:23:00 +020051static struct pool_head *zlib_pool_deflate_state __read_mostly = NULL;
52static struct pool_head *zlib_pool_window __read_mostly = NULL;
53static struct pool_head *zlib_pool_prev __read_mostly = NULL;
54static struct pool_head *zlib_pool_head __read_mostly = NULL;
55static struct pool_head *zlib_pool_pending_buf __read_mostly = NULL;
William Lallemand2b502472012-10-30 14:30:39 +010056
William Lallemande3a7d992012-11-20 11:25:20 +010057long zlib_used_memory = 0;
William Lallemand9d5f5482012-11-07 16:12:57 +010058
Willy Tarreau36878032016-12-22 19:46:17 +010059static int global_tune_zlibmemlevel = 8; /* zlib memlevel */
60static int global_tune_zlibwindowsize = MAX_WBITS; /* zlib window size */
61
William Lallemand2b502472012-10-30 14:30:39 +010062#endif
63
William Lallemand072a2bf2012-11-20 17:01:01 +010064unsigned int compress_min_idle = 0;
William Lallemand8b52bb32012-11-16 18:06:41 +010065
Willy Tarreau9f640a12015-03-28 15:46:00 +010066static int identity_init(struct comp_ctx **comp_ctx, int level);
67static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
Willy Tarreau9787efa2015-03-28 19:17:31 +010068static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out);
69static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010070static int identity_end(struct comp_ctx **comp_ctx);
71
Willy Tarreau418b8c02015-03-29 03:32:06 +020072#if defined(USE_SLZ)
73
74static int rfc1950_init(struct comp_ctx **comp_ctx, int level);
75static int rfc1951_init(struct comp_ctx **comp_ctx, int level);
76static int rfc1952_init(struct comp_ctx **comp_ctx, int level);
77static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
78static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out);
79static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out);
80static int rfc195x_end(struct comp_ctx **comp_ctx);
81
82#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +010083
Willy Tarreau9f640a12015-03-28 15:46:00 +010084static int gzip_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreauc91840a2015-03-28 17:00:39 +010085static int raw_def_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreau9f640a12015-03-28 15:46:00 +010086static int deflate_init(struct comp_ctx **comp_ctx, int level);
87static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
Willy Tarreau9787efa2015-03-28 19:17:31 +010088static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out);
89static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010090static int deflate_end(struct comp_ctx **comp_ctx);
Willy Tarreau7b218772015-03-28 22:08:25 +010091
Willy Tarreau9f640a12015-03-28 15:46:00 +010092#endif /* USE_ZLIB */
93
William Lallemand2b502472012-10-30 14:30:39 +010094
Cyril Bonté6162c432012-11-10 19:27:47 +010095const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020096{
Willy Tarreau7b218772015-03-28 22:08:25 +010097 { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_end },
Willy Tarreau418b8c02015-03-29 03:32:06 +020098#if defined(USE_SLZ)
99 { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
100 { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
101 { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
102#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +0100103 { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
104 { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
105 { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
William Lallemand82fe75c2012-10-23 10:25:10 +0200106#endif /* USE_ZLIB */
Willy Tarreau615105e2015-03-28 16:40:46 +0100107 { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL }
William Lallemand82fe75c2012-10-23 10:25:10 +0200108};
109
110/*
111 * Add a content-type in the configuration
112 */
113int comp_append_type(struct comp *comp, const char *type)
114{
115 struct comp_type *comp_type;
116
Vincent Bernat02779b62016-04-03 13:48:43 +0200117 comp_type = calloc(1, sizeof(*comp_type));
William Lallemand82fe75c2012-10-23 10:25:10 +0200118 comp_type->name_len = strlen(type);
119 comp_type->name = strdup(type);
120 comp_type->next = comp->types;
121 comp->types = comp_type;
122 return 0;
123}
124
125/*
126 * Add an algorithm in the configuration
127 */
128int comp_append_algo(struct comp *comp, const char *algo)
129{
130 struct comp_algo *comp_algo;
131 int i;
132
Willy Tarreau615105e2015-03-28 16:40:46 +0100133 for (i = 0; comp_algos[i].cfg_name; i++) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100134 if (strcmp(algo, comp_algos[i].cfg_name) == 0) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200135 comp_algo = calloc(1, sizeof(*comp_algo));
William Lallemand82fe75c2012-10-23 10:25:10 +0200136 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
137 comp_algo->next = comp->algos;
138 comp->algos = comp_algo;
139 return 0;
140 }
141 }
142 return -1;
143}
144
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200145#if defined(USE_ZLIB) || defined(USE_SLZ)
Willy Tarreau8ceae722018-11-26 11:58:30 +0100146DECLARE_STATIC_POOL(pool_comp_ctx, "comp_ctx", sizeof(struct comp_ctx));
147
William Lallemand8b52bb32012-11-16 18:06:41 +0100148/*
149 * Alloc the comp_ctx
150 */
151static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
152{
153#ifdef USE_ZLIB
154 z_stream *strm;
155
William Lallemande3a7d992012-11-20 11:25:20 +0100156 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100157 return -1;
158#endif
159
Willy Tarreaubafbe012017-11-24 17:34:44 +0100160 *comp_ctx = pool_alloc(pool_comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100161 if (*comp_ctx == NULL)
162 return -1;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200163#if defined(USE_SLZ)
164 (*comp_ctx)->direct_ptr = NULL;
165 (*comp_ctx)->direct_len = 0;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200166 (*comp_ctx)->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200167#elif defined(USE_ZLIB)
Olivier Houchard43da3432019-03-08 18:50:27 +0100168 _HA_ATOMIC_ADD(&zlib_used_memory, sizeof(struct comp_ctx));
169 __ha_barrier_atomic_store();
William Lallemand8b52bb32012-11-16 18:06:41 +0100170
171 strm = &(*comp_ctx)->strm;
172 strm->zalloc = alloc_zlib;
173 strm->zfree = free_zlib;
174 strm->opaque = *comp_ctx;
175#endif
176 return 0;
177}
178
179/*
180 * Dealloc the comp_ctx
181 */
182static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
183{
184 if (!*comp_ctx)
185 return 0;
186
Willy Tarreaubafbe012017-11-24 17:34:44 +0100187 pool_free(pool_comp_ctx, *comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100188 *comp_ctx = NULL;
189
190#ifdef USE_ZLIB
Olivier Houchard43da3432019-03-08 18:50:27 +0100191 _HA_ATOMIC_SUB(&zlib_used_memory, sizeof(struct comp_ctx));
192 __ha_barrier_atomic_store();
William Lallemand8b52bb32012-11-16 18:06:41 +0100193#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100194 return 0;
195}
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200196#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100197
William Lallemand82fe75c2012-10-23 10:25:10 +0200198
199/****************************
200 **** Identity algorithm ****
201 ****************************/
202
203/*
204 * Init the identity algorithm
205 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100206static int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200207{
208 return 0;
209}
210
211/*
212 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100213 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200214 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100215static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200216{
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200217 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200218 int out_len = b_room(out);
William Lallemandbf3ae612012-11-19 12:35:37 +0100219
William Lallemand82fe75c2012-10-23 10:25:10 +0200220 if (out_len < in_len)
221 return -1;
222
223 memcpy(out_data, in_data, in_len);
224
Olivier Houchardacd14032018-06-28 18:17:23 +0200225 b_add(out, in_len);
William Lallemandbf3ae612012-11-19 12:35:37 +0100226
William Lallemand82fe75c2012-10-23 10:25:10 +0200227 return in_len;
228}
229
Willy Tarreau9787efa2015-03-28 19:17:31 +0100230static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200231{
232 return 0;
233}
234
Willy Tarreau9787efa2015-03-28 19:17:31 +0100235static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out)
236{
237 return 0;
238}
239
Willy Tarreau418b8c02015-03-29 03:32:06 +0200240/*
241 * Deinit the algorithm
242 */
243static int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200244{
245 return 0;
246}
247
Willy Tarreau418b8c02015-03-29 03:32:06 +0200248
249#ifdef USE_SLZ
250
251/* SLZ's gzip format (RFC1952). Returns < 0 on error. */
252static int rfc1952_init(struct comp_ctx **comp_ctx, int level)
253{
254 if (init_comp_ctx(comp_ctx) < 0)
255 return -1;
256
257 (*comp_ctx)->cur_lvl = !!level;
258 return slz_rfc1952_init(&(*comp_ctx)->strm, !!level);
259}
260
261/* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */
262static int rfc1951_init(struct comp_ctx **comp_ctx, int level)
263{
264 if (init_comp_ctx(comp_ctx) < 0)
265 return -1;
266
267 (*comp_ctx)->cur_lvl = !!level;
268 return slz_rfc1951_init(&(*comp_ctx)->strm, !!level);
269}
270
271/* SLZ's zlib format (RFC1950). Returns < 0 on error. */
272static int rfc1950_init(struct comp_ctx **comp_ctx, int level)
273{
274 if (init_comp_ctx(comp_ctx) < 0)
275 return -1;
276
277 (*comp_ctx)->cur_lvl = !!level;
278 return slz_rfc1950_init(&(*comp_ctx)->strm, !!level);
279}
280
281/* Return the size of consumed data or -1. The output buffer is unused at this
282 * point, we only keep a reference to the input data or a copy of them if the
283 * reference is already used.
William Lallemand82fe75c2012-10-23 10:25:10 +0200284 */
Willy Tarreau418b8c02015-03-29 03:32:06 +0200285static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200286{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200287 static THREAD_LOCAL struct buffer tmpbuf = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200288
289 if (in_len <= 0)
290 return 0;
291
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200292 if (comp_ctx->direct_ptr && b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200293 /* data already being pointed to, we're in front of fragmented
294 * data and need a buffer now. We reuse the same buffer, as it's
295 * not used out of the scope of a series of add_data()*, end().
296 */
Willy Tarreau862ad822021-03-22 16:16:22 +0100297 if (b_alloc(&tmpbuf) == NULL)
298 return -1; /* no memory */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200299 b_reset(&tmpbuf);
300 memcpy(b_tail(&tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len);
301 b_add(&tmpbuf, comp_ctx->direct_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200302 comp_ctx->direct_ptr = NULL;
303 comp_ctx->direct_len = 0;
304 comp_ctx->queued = tmpbuf;
305 /* fall through buffer copy */
306 }
307
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200308 if (!b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200309 /* data already pending */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200310 memcpy(b_tail(&comp_ctx->queued), in_data, in_len);
311 b_add(&comp_ctx->queued, in_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200312 return in_len;
313 }
314
315 comp_ctx->direct_ptr = in_data;
316 comp_ctx->direct_len = in_len;
317 return in_len;
318}
319
320/* Compresses the data accumulated using add_data(), and optionally sends the
321 * format-specific trailer if <finish> is non-null. <out> is expected to have a
322 * large enough free non-wrapping space as verified by http_comp_buffer_init().
323 * The number of bytes emitted is reported.
324 */
325static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish)
326{
327 struct slz_stream *strm = &comp_ctx->strm;
328 const char *in_ptr;
329 int in_len;
330 int out_len;
331
332 in_ptr = comp_ctx->direct_ptr;
333 in_len = comp_ctx->direct_len;
334
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335 if (!b_is_null(&comp_ctx->queued)) {
336 in_ptr = b_head(&comp_ctx->queued);
337 in_len = b_data(&comp_ctx->queued);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200338 }
339
Olivier Houchard0b662842018-06-29 18:16:31 +0200340 out_len = b_data(out);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200341
342 if (in_ptr)
Olivier Houchardacd14032018-06-28 18:17:23 +0200343 b_add(out, slz_encode(strm, b_tail(out), in_ptr, in_len, !finish));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200344
345 if (finish)
Olivier Houchardacd14032018-06-28 18:17:23 +0200346 b_add(out, slz_finish(strm, b_tail(out)));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200347
Olivier Houchard0b662842018-06-29 18:16:31 +0200348 out_len = b_data(out) - out_len;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200349
350 /* very important, we must wipe the data we've just flushed */
351 comp_ctx->direct_len = 0;
352 comp_ctx->direct_ptr = NULL;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200353 comp_ctx->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200354
355 /* Verify compression rate limiting and CPU usage */
356 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
Willy Tarreau81036f22019-05-20 19:24:50 +0200357 (ti->idle_pct < compress_min_idle)) { /* idle */
Willy Tarreau418b8c02015-03-29 03:32:06 +0200358 if (comp_ctx->cur_lvl > 0)
359 strm->level = --comp_ctx->cur_lvl;
360 }
361 else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) {
362 strm->level = ++comp_ctx->cur_lvl;
363 }
364
365 /* and that's all */
366 return out_len;
367}
368
369static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out)
370{
371 return rfc195x_flush_or_finish(comp_ctx, out, 0);
372}
373
374static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out)
375{
376 return rfc195x_flush_or_finish(comp_ctx, out, 1);
377}
378
379/* we just need to free the comp_ctx here, nothing was allocated */
380static int rfc195x_end(struct comp_ctx **comp_ctx)
381{
382 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200383 return 0;
384}
385
Willy Tarreau418b8c02015-03-29 03:32:06 +0200386#elif defined(USE_ZLIB) /* ! USE_SLZ */
William Lallemand82fe75c2012-10-23 10:25:10 +0200387
William Lallemand2b502472012-10-30 14:30:39 +0100388/*
389 * This is a tricky allocation function using the zlib.
390 * This is based on the allocation order in deflateInit2.
391 */
392static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
393{
394 struct comp_ctx *ctx = opaque;
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200395 static THREAD_LOCAL char round = 0; /* order in deflateInit2 */
William Lallemand2b502472012-10-30 14:30:39 +0100396 void *buf = NULL;
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100397 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100398
William Lallemande3a7d992012-11-20 11:25:20 +0100399 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100400 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100401
William Lallemand2b502472012-10-30 14:30:39 +0100402 switch (round) {
403 case 0:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200404 if (zlib_pool_deflate_state == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100405 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200406 if (zlib_pool_deflate_state == NULL)
407 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100408 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200409 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100410 pool = zlib_pool_deflate_state;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100411 ctx->zlib_deflate_state = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100412 break;
413
414 case 1:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200415 if (zlib_pool_window == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100416 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200417 if (zlib_pool_window == NULL)
418 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100419 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200420 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100421 pool = zlib_pool_window;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100422 ctx->zlib_window = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100423 break;
424
425 case 2:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200426 if (zlib_pool_prev == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100427 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200428 if (zlib_pool_prev == NULL)
429 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100430 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200431 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100432 pool = zlib_pool_prev;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100433 ctx->zlib_prev = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100434 break;
435
436 case 3:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200437 if (zlib_pool_head == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100438 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200439 if (zlib_pool_head == NULL)
440 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200442 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100443 pool = zlib_pool_head;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100444 ctx->zlib_head = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100445 break;
446
447 case 4:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200448 if (zlib_pool_pending_buf == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200450 if (zlib_pool_pending_buf == NULL)
451 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100452 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200453 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100454 pool = zlib_pool_pending_buf;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100455 ctx->zlib_pending_buf = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100456 break;
457 }
Olivier Houchard43da3432019-03-08 18:50:27 +0100458 if (buf != NULL) {
459 _HA_ATOMIC_ADD(&zlib_used_memory, pool->size);
460 __ha_barrier_atomic_store();
461 }
William Lallemand9d5f5482012-11-07 16:12:57 +0100462
463end:
William Lallemand2b502472012-10-30 14:30:39 +0100464
Willy Tarreau46909852012-11-15 14:57:56 +0100465 /* deflateInit2() first allocates and checks the deflate_state, then if
466 * it succeeds, it allocates all other 4 areas at ones and checks them
467 * at the end. So we want to correctly count the rounds depending on when
468 * zlib is supposed to abort.
469 */
470 if (buf || round)
471 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100472 return buf;
473}
474
475static void free_zlib(void *opaque, void *ptr)
476{
477 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100478 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100479
480 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100481 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100482 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100483 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100484 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100485 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100486 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100487 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100488 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100489 pool = zlib_pool_pending_buf;
Willy Tarreaud999a492020-06-14 07:50:18 +0200490 else {
491 // never matched, just to silence gcc
492 ABORT_NOW();
493 return;
494 }
William Lallemand2b502472012-10-30 14:30:39 +0100495
Willy Tarreaubafbe012017-11-24 17:34:44 +0100496 pool_free(pool, ptr);
Olivier Houchard43da3432019-03-08 18:50:27 +0100497 _HA_ATOMIC_SUB(&zlib_used_memory, pool->size);
498 __ha_barrier_atomic_store();
William Lallemand2b502472012-10-30 14:30:39 +0100499}
500
William Lallemand82fe75c2012-10-23 10:25:10 +0200501/**************************
502**** gzip algorithm ****
503***************************/
Willy Tarreau9f640a12015-03-28 15:46:00 +0100504static int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200505{
William Lallemand8b52bb32012-11-16 18:06:41 +0100506 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200507
William Lallemand8b52bb32012-11-16 18:06:41 +0100508 if (init_comp_ctx(comp_ctx) < 0)
509 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100510
William Lallemand8b52bb32012-11-16 18:06:41 +0100511 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200512
Willy Tarreau36878032016-12-22 19:46:17 +0100513 if (deflateInit2(strm, level, Z_DEFLATED, global_tune_zlibwindowsize + 16, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100514 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200515 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100516 }
517
518 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200519
520 return 0;
521}
Willy Tarreauc91840a2015-03-28 17:00:39 +0100522
523/* Raw deflate algorithm */
524static int raw_def_init(struct comp_ctx **comp_ctx, int level)
525{
526 z_stream *strm;
527
528 if (init_comp_ctx(comp_ctx) < 0)
529 return -1;
530
531 strm = &(*comp_ctx)->strm;
532
Willy Tarreau36878032016-12-22 19:46:17 +0100533 if (deflateInit2(strm, level, Z_DEFLATED, -global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
Willy Tarreauc91840a2015-03-28 17:00:39 +0100534 deinit_comp_ctx(comp_ctx);
535 return -1;
536 }
537
538 (*comp_ctx)->cur_lvl = level;
539 return 0;
540}
541
William Lallemand82fe75c2012-10-23 10:25:10 +0200542/**************************
543**** Deflate algorithm ****
544***************************/
545
Willy Tarreau9f640a12015-03-28 15:46:00 +0100546static int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200547{
William Lallemand8b52bb32012-11-16 18:06:41 +0100548 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200549
William Lallemand8b52bb32012-11-16 18:06:41 +0100550 if (init_comp_ctx(comp_ctx) < 0)
551 return -1;
552
553 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200554
Willy Tarreau36878032016-12-22 19:46:17 +0100555 if (deflateInit2(strm, level, Z_DEFLATED, global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100556 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200557 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100558 }
559
560 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200561
562 return 0;
563}
564
William Lallemandbf3ae612012-11-19 12:35:37 +0100565/* Return the size of consumed data or -1 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100566static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200567{
William Lallemand82fe75c2012-10-23 10:25:10 +0200568 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100569 z_stream *strm = &comp_ctx->strm;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200570 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200571 int out_len = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200572
573 if (in_len <= 0)
574 return 0;
575
576
577 if (out_len <= 0)
578 return -1;
579
William Lallemand82fe75c2012-10-23 10:25:10 +0200580 strm->next_in = (unsigned char *)in_data;
581 strm->avail_in = in_len;
582 strm->next_out = (unsigned char *)out_data;
583 strm->avail_out = out_len;
584
585 ret = deflate(strm, Z_NO_FLUSH);
586 if (ret != Z_OK)
587 return -1;
588
589 /* deflate update the available data out */
Olivier Houchardacd14032018-06-28 18:17:23 +0200590 b_add(out, out_len - strm->avail_out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200591
William Lallemandbf3ae612012-11-19 12:35:37 +0100592 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200593}
594
Willy Tarreau9787efa2015-03-28 19:17:31 +0100595static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200596{
597 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200598 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100599 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200600
Willy Tarreaud8b8b532016-08-08 16:41:01 +0200601 strm->next_in = NULL;
602 strm->avail_in = 0;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200603 strm->next_out = (unsigned char *)b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200604 strm->avail_out = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200605
606 ret = deflate(strm, flag);
607 if (ret != Z_OK && ret != Z_STREAM_END)
608 return -1;
609
Willy Tarreaueac52592018-06-15 13:59:36 +0200610 out_len = b_room(out) - strm->avail_out;
Olivier Houchardacd14032018-06-28 18:17:23 +0200611 b_add(out, out_len);
William Lallemand82fe75c2012-10-23 10:25:10 +0200612
William Lallemand072a2bf2012-11-20 17:01:01 +0100613 /* compression limit */
614 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
Willy Tarreau81036f22019-05-20 19:24:50 +0200615 (ti->idle_pct < compress_min_idle)) { /* idle */
William Lallemand072a2bf2012-11-20 17:01:01 +0100616 /* decrease level */
617 if (comp_ctx->cur_lvl > 0) {
618 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100619 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
620 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100621
622 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
623 /* increase level */
624 comp_ctx->cur_lvl++ ;
625 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100626 }
627
William Lallemand82fe75c2012-10-23 10:25:10 +0200628 return out_len;
629}
630
Willy Tarreau9787efa2015-03-28 19:17:31 +0100631static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out)
632{
633 return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH);
634}
635
636static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out)
637{
638 return deflate_flush_or_finish(comp_ctx, out, Z_FINISH);
639}
640
Willy Tarreau9f640a12015-03-28 15:46:00 +0100641static int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200642{
William Lallemand8b52bb32012-11-16 18:06:41 +0100643 z_stream *strm = &(*comp_ctx)->strm;
644 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200645
William Lallemand8b52bb32012-11-16 18:06:41 +0100646 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200647
William Lallemand8b52bb32012-11-16 18:06:41 +0100648 deinit_comp_ctx(comp_ctx);
649
650 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200651}
652
Willy Tarreau36878032016-12-22 19:46:17 +0100653/* config parser for global "tune.zlibmemlevel" */
654static int zlib_parse_global_memlevel(char **args, int section_type, struct proxy *curpx,
Willy Tarreau5a1c7282021-03-09 16:55:18 +0100655 const struct proxy *defpx, const char *file, int line,
Willy Tarreau36878032016-12-22 19:46:17 +0100656 char **err)
657{
658 if (too_many_args(1, args, err, NULL))
659 return -1;
660
661 if (*(args[1]) == 0) {
662 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
663 return -1;
664 }
665
666 global_tune_zlibmemlevel = atoi(args[1]);
667 if (global_tune_zlibmemlevel < 1 || global_tune_zlibmemlevel > 9) {
668 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
669 return -1;
670 }
671 return 0;
672}
673
674
675/* config parser for global "tune.zlibwindowsize" */
676static int zlib_parse_global_windowsize(char **args, int section_type, struct proxy *curpx,
Willy Tarreau5a1c7282021-03-09 16:55:18 +0100677 const struct proxy *defpx, const char *file, int line,
Willy Tarreau36878032016-12-22 19:46:17 +0100678 char **err)
679{
680 if (too_many_args(1, args, err, NULL))
681 return -1;
682
683 if (*(args[1]) == 0) {
684 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
685 return -1;
686 }
687
688 global_tune_zlibwindowsize = atoi(args[1]);
689 if (global_tune_zlibwindowsize < 8 || global_tune_zlibwindowsize > 15) {
690 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
691 return -1;
692 }
693 return 0;
694}
695
William Lallemand82fe75c2012-10-23 10:25:10 +0200696#endif /* USE_ZLIB */
697
Willy Tarreau36878032016-12-22 19:46:17 +0100698
699/* config keyword parsers */
700static struct cfg_kw_list cfg_kws = {ILH, {
701#ifdef USE_ZLIB
702 { CFG_GLOBAL, "tune.zlib.memlevel", zlib_parse_global_memlevel },
703 { CFG_GLOBAL, "tune.zlib.windowsize", zlib_parse_global_windowsize },
704#endif
705 { 0, NULL, NULL }
706}};
707
Willy Tarreau0108d902018-11-25 19:14:37 +0100708INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
709
William Lallemand727db8b2013-04-20 17:33:20 +0200710__attribute__((constructor))
711static void __comp_fetch_init(void)
712{
Willy Tarreau418b8c02015-03-29 03:32:06 +0200713#ifdef USE_SLZ
714 slz_make_crc_table();
715 slz_prepare_dist_table();
716#endif
Willy Tarreaue5489742018-11-26 14:44:03 +0100717
Willy Tarreau36878032016-12-22 19:46:17 +0100718#if defined(USE_ZLIB) && defined(DEFAULT_MAXZLIBMEM)
Willy Tarreau3bfcd102018-11-26 10:24:45 +0100719 global.maxzlibmem = DEFAULT_MAXZLIBMEM * 1024U * 1024U;
Willy Tarreau36878032016-12-22 19:46:17 +0100720#endif
Willy Tarreau80713382018-11-26 10:19:54 +0100721}
722
723static void comp_register_build_opts(void)
724{
725 char *ptr = NULL;
726 int i;
727
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100728#ifdef USE_ZLIB
729 memprintf(&ptr, "Built with zlib version : " ZLIB_VERSION);
730 memprintf(&ptr, "%s\nRunning on zlib version : %s", ptr, zlibVersion());
731#elif defined(USE_SLZ)
732 memprintf(&ptr, "Built with libslz for stateless compression.");
Lukas Tribusb7323212017-01-11 14:24:35 +0000733#else
734 memprintf(&ptr, "Built without compression support (neither USE_ZLIB nor USE_SLZ are set).");
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100735#endif
736 memprintf(&ptr, "%s\nCompression algorithms supported :", ptr);
737
738 for (i = 0; comp_algos[i].cfg_name; i++)
739 memprintf(&ptr, "%s%s %s(\"%s\")", ptr, (i == 0 ? "" : ","), comp_algos[i].cfg_name, comp_algos[i].ua_name);
740
741 if (i == 0)
742 memprintf(&ptr, "%s none", ptr);
743
744 hap_register_build_opts(ptr, 1);
William Lallemand727db8b2013-04-20 17:33:20 +0200745}
Willy Tarreau80713382018-11-26 10:19:54 +0100746
747INITCALL0(STG_REGISTER, comp_register_build_opts);