blob: e7ce90a1f21416095fb1102240fd0ccf1b01f9af [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 Tarreau36878032016-12-22 19:46:17 +010029#include <common/cfgparse.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020030#include <common/compat.h>
William Lallemand2b502472012-10-30 14:30:39 +010031#include <common/memory.h>
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020032#include <common/hathreads.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020033
34#include <types/global.h>
35#include <types/compression.h>
36
William Lallemand727db8b2013-04-20 17:33:20 +020037#include <proto/acl.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020038#include <proto/compression.h>
William Lallemandd85f9172012-11-09 17:05:39 +010039#include <proto/freq_ctr.h>
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020040#include <proto/stream.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020041
William Lallemand2b502472012-10-30 14:30:39 +010042
Emeric Brun11f58862017-11-07 11:57:54 +010043#if defined(USE_SLZ) || defined(USE_ZLIB)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010044__decl_hathreads(static HA_SPINLOCK_T comp_pool_lock);
Emeric Brun11f58862017-11-07 11:57:54 +010045#endif
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020046
William Lallemand2b502472012-10-30 14:30:39 +010047#ifdef USE_ZLIB
48
William Lallemand8b52bb32012-11-16 18:06:41 +010049static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size);
50static void free_zlib(void *opaque, void *ptr);
51
William Lallemand2b502472012-10-30 14:30:39 +010052/* zlib allocation */
53static struct pool_head *zlib_pool_deflate_state = NULL;
54static struct pool_head *zlib_pool_window = NULL;
55static struct pool_head *zlib_pool_prev = NULL;
56static struct pool_head *zlib_pool_head = NULL;
57static struct pool_head *zlib_pool_pending_buf = NULL;
58
William Lallemande3a7d992012-11-20 11:25:20 +010059long zlib_used_memory = 0;
William Lallemand9d5f5482012-11-07 16:12:57 +010060
Willy Tarreau36878032016-12-22 19:46:17 +010061static int global_tune_zlibmemlevel = 8; /* zlib memlevel */
62static int global_tune_zlibwindowsize = MAX_WBITS; /* zlib window size */
63
William Lallemand2b502472012-10-30 14:30:39 +010064#endif
65
William Lallemand072a2bf2012-11-20 17:01:01 +010066unsigned int compress_min_idle = 0;
William Lallemand8b52bb32012-11-16 18:06:41 +010067
Willy Tarreau9f640a12015-03-28 15:46:00 +010068static int identity_init(struct comp_ctx **comp_ctx, int level);
69static 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 +010070static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out);
71static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010072static int identity_end(struct comp_ctx **comp_ctx);
73
Willy Tarreau418b8c02015-03-29 03:32:06 +020074#if defined(USE_SLZ)
75
76static int rfc1950_init(struct comp_ctx **comp_ctx, int level);
77static int rfc1951_init(struct comp_ctx **comp_ctx, int level);
78static int rfc1952_init(struct comp_ctx **comp_ctx, int level);
79static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
80static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out);
81static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out);
82static int rfc195x_end(struct comp_ctx **comp_ctx);
83
84#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +010085
Willy Tarreau9f640a12015-03-28 15:46:00 +010086static int gzip_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreauc91840a2015-03-28 17:00:39 +010087static int raw_def_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreau9f640a12015-03-28 15:46:00 +010088static int deflate_init(struct comp_ctx **comp_ctx, int level);
89static 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 +010090static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out);
91static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010092static int deflate_end(struct comp_ctx **comp_ctx);
Willy Tarreau7b218772015-03-28 22:08:25 +010093
Willy Tarreau9f640a12015-03-28 15:46:00 +010094#endif /* USE_ZLIB */
95
William Lallemand2b502472012-10-30 14:30:39 +010096
Cyril Bonté6162c432012-11-10 19:27:47 +010097const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020098{
Willy Tarreau7b218772015-03-28 22:08:25 +010099 { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_end },
Willy Tarreau418b8c02015-03-29 03:32:06 +0200100#if defined(USE_SLZ)
101 { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
102 { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
103 { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
104#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +0100105 { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
106 { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
107 { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
William Lallemand82fe75c2012-10-23 10:25:10 +0200108#endif /* USE_ZLIB */
Willy Tarreau615105e2015-03-28 16:40:46 +0100109 { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL }
William Lallemand82fe75c2012-10-23 10:25:10 +0200110};
111
112/*
113 * Add a content-type in the configuration
114 */
115int comp_append_type(struct comp *comp, const char *type)
116{
117 struct comp_type *comp_type;
118
Vincent Bernat02779b62016-04-03 13:48:43 +0200119 comp_type = calloc(1, sizeof(*comp_type));
William Lallemand82fe75c2012-10-23 10:25:10 +0200120 comp_type->name_len = strlen(type);
121 comp_type->name = strdup(type);
122 comp_type->next = comp->types;
123 comp->types = comp_type;
124 return 0;
125}
126
127/*
128 * Add an algorithm in the configuration
129 */
130int comp_append_algo(struct comp *comp, const char *algo)
131{
132 struct comp_algo *comp_algo;
133 int i;
134
Willy Tarreau615105e2015-03-28 16:40:46 +0100135 for (i = 0; comp_algos[i].cfg_name; i++) {
136 if (!strcmp(algo, comp_algos[i].cfg_name)) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200137 comp_algo = calloc(1, sizeof(*comp_algo));
William Lallemand82fe75c2012-10-23 10:25:10 +0200138 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
139 comp_algo->next = comp->algos;
140 comp->algos = comp_algo;
141 return 0;
142 }
143 }
144 return -1;
145}
146
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200147#if defined(USE_ZLIB) || defined(USE_SLZ)
148static struct pool_head *pool_comp_ctx = NULL;
William Lallemand8b52bb32012-11-16 18:06:41 +0100149/*
150 * Alloc the comp_ctx
151 */
152static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
153{
154#ifdef USE_ZLIB
155 z_stream *strm;
156
William Lallemande3a7d992012-11-20 11:25:20 +0100157 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100158 return -1;
159#endif
160
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200161 if (unlikely(pool_comp_ctx == NULL)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100162 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200163 if (unlikely(pool_comp_ctx == NULL))
164 pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100165 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200166 }
William Lallemand8b52bb32012-11-16 18:06:41 +0100167
Willy Tarreaubafbe012017-11-24 17:34:44 +0100168 *comp_ctx = pool_alloc(pool_comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100169 if (*comp_ctx == NULL)
170 return -1;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200171#if defined(USE_SLZ)
172 (*comp_ctx)->direct_ptr = NULL;
173 (*comp_ctx)->direct_len = 0;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200174 (*comp_ctx)->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200175#elif defined(USE_ZLIB)
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200176 HA_ATOMIC_ADD(&zlib_used_memory, sizeof(struct comp_ctx));
William Lallemand8b52bb32012-11-16 18:06:41 +0100177
178 strm = &(*comp_ctx)->strm;
179 strm->zalloc = alloc_zlib;
180 strm->zfree = free_zlib;
181 strm->opaque = *comp_ctx;
182#endif
183 return 0;
184}
185
186/*
187 * Dealloc the comp_ctx
188 */
189static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
190{
191 if (!*comp_ctx)
192 return 0;
193
Willy Tarreaubafbe012017-11-24 17:34:44 +0100194 pool_free(pool_comp_ctx, *comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100195 *comp_ctx = NULL;
196
197#ifdef USE_ZLIB
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200198 HA_ATOMIC_SUB(&zlib_used_memory, sizeof(struct comp_ctx));
William Lallemand8b52bb32012-11-16 18:06:41 +0100199#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100200 return 0;
201}
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200202#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100203
William Lallemand82fe75c2012-10-23 10:25:10 +0200204
205/****************************
206 **** Identity algorithm ****
207 ****************************/
208
209/*
210 * Init the identity algorithm
211 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100212static int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200213{
214 return 0;
215}
216
217/*
218 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100219 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200220 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100221static 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 +0200222{
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200223 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200224 int out_len = b_room(out);
William Lallemandbf3ae612012-11-19 12:35:37 +0100225
William Lallemand82fe75c2012-10-23 10:25:10 +0200226 if (out_len < in_len)
227 return -1;
228
229 memcpy(out_data, in_data, in_len);
230
Olivier Houchardacd14032018-06-28 18:17:23 +0200231 b_add(out, in_len);
William Lallemandbf3ae612012-11-19 12:35:37 +0100232
William Lallemand82fe75c2012-10-23 10:25:10 +0200233 return in_len;
234}
235
Willy Tarreau9787efa2015-03-28 19:17:31 +0100236static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200237{
238 return 0;
239}
240
Willy Tarreau9787efa2015-03-28 19:17:31 +0100241static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out)
242{
243 return 0;
244}
245
Willy Tarreau418b8c02015-03-29 03:32:06 +0200246/*
247 * Deinit the algorithm
248 */
249static int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200250{
251 return 0;
252}
253
Willy Tarreau418b8c02015-03-29 03:32:06 +0200254
255#ifdef USE_SLZ
256
257/* SLZ's gzip format (RFC1952). Returns < 0 on error. */
258static int rfc1952_init(struct comp_ctx **comp_ctx, int level)
259{
260 if (init_comp_ctx(comp_ctx) < 0)
261 return -1;
262
263 (*comp_ctx)->cur_lvl = !!level;
264 return slz_rfc1952_init(&(*comp_ctx)->strm, !!level);
265}
266
267/* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */
268static int rfc1951_init(struct comp_ctx **comp_ctx, int level)
269{
270 if (init_comp_ctx(comp_ctx) < 0)
271 return -1;
272
273 (*comp_ctx)->cur_lvl = !!level;
274 return slz_rfc1951_init(&(*comp_ctx)->strm, !!level);
275}
276
277/* SLZ's zlib format (RFC1950). Returns < 0 on error. */
278static int rfc1950_init(struct comp_ctx **comp_ctx, int level)
279{
280 if (init_comp_ctx(comp_ctx) < 0)
281 return -1;
282
283 (*comp_ctx)->cur_lvl = !!level;
284 return slz_rfc1950_init(&(*comp_ctx)->strm, !!level);
285}
286
287/* Return the size of consumed data or -1. The output buffer is unused at this
288 * point, we only keep a reference to the input data or a copy of them if the
289 * reference is already used.
William Lallemand82fe75c2012-10-23 10:25:10 +0200290 */
Willy Tarreau418b8c02015-03-29 03:32:06 +0200291static 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 +0200292{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200293 static THREAD_LOCAL struct buffer tmpbuf = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200294
295 if (in_len <= 0)
296 return 0;
297
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200298 if (comp_ctx->direct_ptr && b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200299 /* data already being pointed to, we're in front of fragmented
300 * data and need a buffer now. We reuse the same buffer, as it's
301 * not used out of the scope of a series of add_data()*, end().
302 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200303 if (unlikely(!tmpbuf.size)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200304 /* this is the first time we need the compression buffer */
305 if (b_alloc(&tmpbuf) == NULL)
306 return -1; /* no memory */
307 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200308 b_reset(&tmpbuf);
309 memcpy(b_tail(&tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len);
310 b_add(&tmpbuf, comp_ctx->direct_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200311 comp_ctx->direct_ptr = NULL;
312 comp_ctx->direct_len = 0;
313 comp_ctx->queued = tmpbuf;
314 /* fall through buffer copy */
315 }
316
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200317 if (!b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200318 /* data already pending */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200319 memcpy(b_tail(&comp_ctx->queued), in_data, in_len);
320 b_add(&comp_ctx->queued, in_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200321 return in_len;
322 }
323
324 comp_ctx->direct_ptr = in_data;
325 comp_ctx->direct_len = in_len;
326 return in_len;
327}
328
329/* Compresses the data accumulated using add_data(), and optionally sends the
330 * format-specific trailer if <finish> is non-null. <out> is expected to have a
331 * large enough free non-wrapping space as verified by http_comp_buffer_init().
332 * The number of bytes emitted is reported.
333 */
334static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish)
335{
336 struct slz_stream *strm = &comp_ctx->strm;
337 const char *in_ptr;
338 int in_len;
339 int out_len;
340
341 in_ptr = comp_ctx->direct_ptr;
342 in_len = comp_ctx->direct_len;
343
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200344 if (!b_is_null(&comp_ctx->queued)) {
345 in_ptr = b_head(&comp_ctx->queued);
346 in_len = b_data(&comp_ctx->queued);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200347 }
348
Olivier Houchard0b662842018-06-29 18:16:31 +0200349 out_len = b_data(out);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200350
351 if (in_ptr)
Olivier Houchardacd14032018-06-28 18:17:23 +0200352 b_add(out, slz_encode(strm, b_tail(out), in_ptr, in_len, !finish));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200353
354 if (finish)
Olivier Houchardacd14032018-06-28 18:17:23 +0200355 b_add(out, slz_finish(strm, b_tail(out)));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200356
Olivier Houchard0b662842018-06-29 18:16:31 +0200357 out_len = b_data(out) - out_len;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200358
359 /* very important, we must wipe the data we've just flushed */
360 comp_ctx->direct_len = 0;
361 comp_ctx->direct_ptr = NULL;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200362 comp_ctx->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200363
364 /* Verify compression rate limiting and CPU usage */
365 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
366 (idle_pct < compress_min_idle)) { /* idle */
367 if (comp_ctx->cur_lvl > 0)
368 strm->level = --comp_ctx->cur_lvl;
369 }
370 else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) {
371 strm->level = ++comp_ctx->cur_lvl;
372 }
373
374 /* and that's all */
375 return out_len;
376}
377
378static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out)
379{
380 return rfc195x_flush_or_finish(comp_ctx, out, 0);
381}
382
383static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out)
384{
385 return rfc195x_flush_or_finish(comp_ctx, out, 1);
386}
387
388/* we just need to free the comp_ctx here, nothing was allocated */
389static int rfc195x_end(struct comp_ctx **comp_ctx)
390{
391 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200392 return 0;
393}
394
Willy Tarreau418b8c02015-03-29 03:32:06 +0200395#elif defined(USE_ZLIB) /* ! USE_SLZ */
William Lallemand82fe75c2012-10-23 10:25:10 +0200396
William Lallemand2b502472012-10-30 14:30:39 +0100397/*
398 * This is a tricky allocation function using the zlib.
399 * This is based on the allocation order in deflateInit2.
400 */
401static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
402{
403 struct comp_ctx *ctx = opaque;
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200404 static THREAD_LOCAL char round = 0; /* order in deflateInit2 */
William Lallemand2b502472012-10-30 14:30:39 +0100405 void *buf = NULL;
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100406 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100407
William Lallemande3a7d992012-11-20 11:25:20 +0100408 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100409 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100410
William Lallemand2b502472012-10-30 14:30:39 +0100411 switch (round) {
412 case 0:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200413 if (zlib_pool_deflate_state == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100414 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200415 if (zlib_pool_deflate_state == NULL)
416 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100417 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200418 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100419 pool = zlib_pool_deflate_state;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100420 ctx->zlib_deflate_state = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100421 break;
422
423 case 1:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200424 if (zlib_pool_window == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100425 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200426 if (zlib_pool_window == NULL)
427 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200429 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100430 pool = zlib_pool_window;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100431 ctx->zlib_window = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100432 break;
433
434 case 2:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200435 if (zlib_pool_prev == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200437 if (zlib_pool_prev == NULL)
438 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200440 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100441 pool = zlib_pool_prev;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100442 ctx->zlib_prev = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100443 break;
444
445 case 3:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200446 if (zlib_pool_head == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100447 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200448 if (zlib_pool_head == NULL)
449 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100450 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200451 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100452 pool = zlib_pool_head;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100453 ctx->zlib_head = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100454 break;
455
456 case 4:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200457 if (zlib_pool_pending_buf == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100458 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200459 if (zlib_pool_pending_buf == NULL)
460 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200462 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100463 pool = zlib_pool_pending_buf;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100464 ctx->zlib_pending_buf = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100465 break;
466 }
William Lallemande3a7d992012-11-20 11:25:20 +0100467 if (buf != NULL)
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200468 HA_ATOMIC_ADD(&zlib_used_memory, pool->size);
William Lallemand9d5f5482012-11-07 16:12:57 +0100469
470end:
William Lallemand2b502472012-10-30 14:30:39 +0100471
Willy Tarreau46909852012-11-15 14:57:56 +0100472 /* deflateInit2() first allocates and checks the deflate_state, then if
473 * it succeeds, it allocates all other 4 areas at ones and checks them
474 * at the end. So we want to correctly count the rounds depending on when
475 * zlib is supposed to abort.
476 */
477 if (buf || round)
478 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100479 return buf;
480}
481
482static void free_zlib(void *opaque, void *ptr)
483{
484 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100485 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100486
487 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100488 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100489 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100490 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100491 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100492 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100493 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100494 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100495 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100496 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100497
Willy Tarreaubafbe012017-11-24 17:34:44 +0100498 pool_free(pool, ptr);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200499 HA_ATOMIC_SUB(&zlib_used_memory, pool->size);
William Lallemand2b502472012-10-30 14:30:39 +0100500}
501
William Lallemand82fe75c2012-10-23 10:25:10 +0200502/**************************
503**** gzip algorithm ****
504***************************/
Willy Tarreau9f640a12015-03-28 15:46:00 +0100505static int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200506{
William Lallemand8b52bb32012-11-16 18:06:41 +0100507 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200508
William Lallemand8b52bb32012-11-16 18:06:41 +0100509 if (init_comp_ctx(comp_ctx) < 0)
510 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100511
William Lallemand8b52bb32012-11-16 18:06:41 +0100512 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200513
Willy Tarreau36878032016-12-22 19:46:17 +0100514 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 +0100515 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200516 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100517 }
518
519 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200520
521 return 0;
522}
Willy Tarreauc91840a2015-03-28 17:00:39 +0100523
524/* Raw deflate algorithm */
525static int raw_def_init(struct comp_ctx **comp_ctx, int level)
526{
527 z_stream *strm;
528
529 if (init_comp_ctx(comp_ctx) < 0)
530 return -1;
531
532 strm = &(*comp_ctx)->strm;
533
Willy Tarreau36878032016-12-22 19:46:17 +0100534 if (deflateInit2(strm, level, Z_DEFLATED, -global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
Willy Tarreauc91840a2015-03-28 17:00:39 +0100535 deinit_comp_ctx(comp_ctx);
536 return -1;
537 }
538
539 (*comp_ctx)->cur_lvl = level;
540 return 0;
541}
542
William Lallemand82fe75c2012-10-23 10:25:10 +0200543/**************************
544**** Deflate algorithm ****
545***************************/
546
Willy Tarreau9f640a12015-03-28 15:46:00 +0100547static int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200548{
William Lallemand8b52bb32012-11-16 18:06:41 +0100549 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200550
William Lallemand8b52bb32012-11-16 18:06:41 +0100551 if (init_comp_ctx(comp_ctx) < 0)
552 return -1;
553
554 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200555
Willy Tarreau36878032016-12-22 19:46:17 +0100556 if (deflateInit2(strm, level, Z_DEFLATED, global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100557 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200558 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100559 }
560
561 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200562
563 return 0;
564}
565
William Lallemandbf3ae612012-11-19 12:35:37 +0100566/* Return the size of consumed data or -1 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100567static 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 +0200568{
William Lallemand82fe75c2012-10-23 10:25:10 +0200569 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100570 z_stream *strm = &comp_ctx->strm;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200571 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200572 int out_len = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200573
574 if (in_len <= 0)
575 return 0;
576
577
578 if (out_len <= 0)
579 return -1;
580
William Lallemand82fe75c2012-10-23 10:25:10 +0200581 strm->next_in = (unsigned char *)in_data;
582 strm->avail_in = in_len;
583 strm->next_out = (unsigned char *)out_data;
584 strm->avail_out = out_len;
585
586 ret = deflate(strm, Z_NO_FLUSH);
587 if (ret != Z_OK)
588 return -1;
589
590 /* deflate update the available data out */
Olivier Houchardacd14032018-06-28 18:17:23 +0200591 b_add(out, out_len - strm->avail_out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200592
William Lallemandbf3ae612012-11-19 12:35:37 +0100593 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200594}
595
Willy Tarreau9787efa2015-03-28 19:17:31 +0100596static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200597{
598 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200599 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100600 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200601
Willy Tarreaud8b8b532016-08-08 16:41:01 +0200602 strm->next_in = NULL;
603 strm->avail_in = 0;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200604 strm->next_out = (unsigned char *)b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200605 strm->avail_out = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200606
607 ret = deflate(strm, flag);
608 if (ret != Z_OK && ret != Z_STREAM_END)
609 return -1;
610
Willy Tarreaueac52592018-06-15 13:59:36 +0200611 out_len = b_room(out) - strm->avail_out;
Olivier Houchardacd14032018-06-28 18:17:23 +0200612 b_add(out, out_len);
William Lallemand82fe75c2012-10-23 10:25:10 +0200613
William Lallemand072a2bf2012-11-20 17:01:01 +0100614 /* compression limit */
615 if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */
616 (idle_pct < compress_min_idle)) { /* idle */
617 /* decrease level */
618 if (comp_ctx->cur_lvl > 0) {
619 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100620 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
621 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100622
623 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
624 /* increase level */
625 comp_ctx->cur_lvl++ ;
626 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100627 }
628
William Lallemand82fe75c2012-10-23 10:25:10 +0200629 return out_len;
630}
631
Willy Tarreau9787efa2015-03-28 19:17:31 +0100632static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out)
633{
634 return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH);
635}
636
637static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out)
638{
639 return deflate_flush_or_finish(comp_ctx, out, Z_FINISH);
640}
641
Willy Tarreau9f640a12015-03-28 15:46:00 +0100642static int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200643{
William Lallemand8b52bb32012-11-16 18:06:41 +0100644 z_stream *strm = &(*comp_ctx)->strm;
645 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200646
William Lallemand8b52bb32012-11-16 18:06:41 +0100647 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200648
William Lallemand8b52bb32012-11-16 18:06:41 +0100649 deinit_comp_ctx(comp_ctx);
650
651 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200652}
653
Willy Tarreau36878032016-12-22 19:46:17 +0100654/* config parser for global "tune.zlibmemlevel" */
655static int zlib_parse_global_memlevel(char **args, int section_type, struct proxy *curpx,
656 struct proxy *defpx, const char *file, int line,
657 char **err)
658{
659 if (too_many_args(1, args, err, NULL))
660 return -1;
661
662 if (*(args[1]) == 0) {
663 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
664 return -1;
665 }
666
667 global_tune_zlibmemlevel = atoi(args[1]);
668 if (global_tune_zlibmemlevel < 1 || global_tune_zlibmemlevel > 9) {
669 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
670 return -1;
671 }
672 return 0;
673}
674
675
676/* config parser for global "tune.zlibwindowsize" */
677static int zlib_parse_global_windowsize(char **args, int section_type, struct proxy *curpx,
678 struct proxy *defpx, const char *file, int line,
679 char **err)
680{
681 if (too_many_args(1, args, err, NULL))
682 return -1;
683
684 if (*(args[1]) == 0) {
685 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
686 return -1;
687 }
688
689 global_tune_zlibwindowsize = atoi(args[1]);
690 if (global_tune_zlibwindowsize < 8 || global_tune_zlibwindowsize > 15) {
691 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
692 return -1;
693 }
694 return 0;
695}
696
William Lallemand82fe75c2012-10-23 10:25:10 +0200697#endif /* USE_ZLIB */
698
Willy Tarreau36878032016-12-22 19:46:17 +0100699
700/* config keyword parsers */
701static struct cfg_kw_list cfg_kws = {ILH, {
702#ifdef USE_ZLIB
703 { CFG_GLOBAL, "tune.zlib.memlevel", zlib_parse_global_memlevel },
704 { CFG_GLOBAL, "tune.zlib.windowsize", zlib_parse_global_windowsize },
705#endif
706 { 0, NULL, NULL }
707}};
708
William Lallemand727db8b2013-04-20 17:33:20 +0200709__attribute__((constructor))
710static void __comp_fetch_init(void)
711{
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100712 char *ptr = NULL;
713 int i;
714
Willy Tarreau418b8c02015-03-29 03:32:06 +0200715#ifdef USE_SLZ
716 slz_make_crc_table();
717 slz_prepare_dist_table();
718#endif
Willy Tarreau36878032016-12-22 19:46:17 +0100719#if defined(USE_ZLIB) && defined(DEFAULT_MAXZLIBMEM)
720 global.tune.maxzlibmem = DEFAULT_MAXZLIBMEM * 1024U * 1024U,
721#endif
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100722#ifdef USE_ZLIB
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100723 HA_SPIN_INIT(&comp_pool_lock);
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100724 memprintf(&ptr, "Built with zlib version : " ZLIB_VERSION);
725 memprintf(&ptr, "%s\nRunning on zlib version : %s", ptr, zlibVersion());
726#elif defined(USE_SLZ)
727 memprintf(&ptr, "Built with libslz for stateless compression.");
Lukas Tribusb7323212017-01-11 14:24:35 +0000728#else
729 memprintf(&ptr, "Built without compression support (neither USE_ZLIB nor USE_SLZ are set).");
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100730#endif
731 memprintf(&ptr, "%s\nCompression algorithms supported :", ptr);
732
733 for (i = 0; comp_algos[i].cfg_name; i++)
734 memprintf(&ptr, "%s%s %s(\"%s\")", ptr, (i == 0 ? "" : ","), comp_algos[i].cfg_name, comp_algos[i].ua_name);
735
736 if (i == 0)
737 memprintf(&ptr, "%s none", ptr);
738
739 hap_register_build_opts(ptr, 1);
Willy Tarreau36878032016-12-22 19:46:17 +0100740 cfg_register_keywords(&cfg_kws);
William Lallemand727db8b2013-04-20 17:33:20 +0200741}