blob: 88ca0bc7e8e77856b56438f2c3f258a6e795dc8e [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>
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020031#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010032#include <common/initcall.h>
33#include <common/memory.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020034
35#include <types/global.h>
36#include <types/compression.h>
37
William Lallemand727db8b2013-04-20 17:33:20 +020038#include <proto/acl.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020039#include <proto/compression.h>
William Lallemandd85f9172012-11-09 17:05:39 +010040#include <proto/freq_ctr.h>
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020041#include <proto/stream.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020042
William Lallemand2b502472012-10-30 14:30:39 +010043
Willy Tarreaue5489742018-11-26 14:44:03 +010044#if defined(USE_ZLIB)
Willy Tarreau86abe442018-11-25 20:12:18 +010045__decl_spinlock(comp_pool_lock);
Emeric Brun11f58862017-11-07 11:57:54 +010046#endif
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +020047
William Lallemand2b502472012-10-30 14:30:39 +010048#ifdef USE_ZLIB
49
William Lallemand8b52bb32012-11-16 18:06:41 +010050static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size);
51static void free_zlib(void *opaque, void *ptr);
52
William Lallemand2b502472012-10-30 14:30:39 +010053/* zlib allocation */
54static struct pool_head *zlib_pool_deflate_state = NULL;
55static struct pool_head *zlib_pool_window = NULL;
56static struct pool_head *zlib_pool_prev = NULL;
57static struct pool_head *zlib_pool_head = NULL;
58static struct pool_head *zlib_pool_pending_buf = NULL;
59
William Lallemande3a7d992012-11-20 11:25:20 +010060long zlib_used_memory = 0;
William Lallemand9d5f5482012-11-07 16:12:57 +010061
Willy Tarreau36878032016-12-22 19:46:17 +010062static int global_tune_zlibmemlevel = 8; /* zlib memlevel */
63static int global_tune_zlibwindowsize = MAX_WBITS; /* zlib window size */
64
William Lallemand2b502472012-10-30 14:30:39 +010065#endif
66
William Lallemand072a2bf2012-11-20 17:01:01 +010067unsigned int compress_min_idle = 0;
William Lallemand8b52bb32012-11-16 18:06:41 +010068
Willy Tarreau9f640a12015-03-28 15:46:00 +010069static int identity_init(struct comp_ctx **comp_ctx, int level);
70static 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 +010071static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out);
72static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010073static int identity_end(struct comp_ctx **comp_ctx);
74
Willy Tarreau418b8c02015-03-29 03:32:06 +020075#if defined(USE_SLZ)
76
77static int rfc1950_init(struct comp_ctx **comp_ctx, int level);
78static int rfc1951_init(struct comp_ctx **comp_ctx, int level);
79static int rfc1952_init(struct comp_ctx **comp_ctx, int level);
80static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out);
81static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out);
82static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out);
83static int rfc195x_end(struct comp_ctx **comp_ctx);
84
85#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +010086
Willy Tarreau9f640a12015-03-28 15:46:00 +010087static int gzip_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreauc91840a2015-03-28 17:00:39 +010088static int raw_def_init(struct comp_ctx **comp_ctx, int level);
Willy Tarreau9f640a12015-03-28 15:46:00 +010089static int deflate_init(struct comp_ctx **comp_ctx, int level);
90static 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 +010091static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out);
92static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out);
Willy Tarreau9f640a12015-03-28 15:46:00 +010093static int deflate_end(struct comp_ctx **comp_ctx);
Willy Tarreau7b218772015-03-28 22:08:25 +010094
Willy Tarreau9f640a12015-03-28 15:46:00 +010095#endif /* USE_ZLIB */
96
William Lallemand2b502472012-10-30 14:30:39 +010097
Cyril Bonté6162c432012-11-10 19:27:47 +010098const struct comp_algo comp_algos[] =
William Lallemand82fe75c2012-10-23 10:25:10 +020099{
Willy Tarreau7b218772015-03-28 22:08:25 +0100100 { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_end },
Willy Tarreau418b8c02015-03-29 03:32:06 +0200101#if defined(USE_SLZ)
102 { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
103 { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
104 { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end },
105#elif defined(USE_ZLIB)
Willy Tarreau7b218772015-03-28 22:08:25 +0100106 { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
107 { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
108 { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end },
William Lallemand82fe75c2012-10-23 10:25:10 +0200109#endif /* USE_ZLIB */
Willy Tarreau615105e2015-03-28 16:40:46 +0100110 { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL }
William Lallemand82fe75c2012-10-23 10:25:10 +0200111};
112
113/*
114 * Add a content-type in the configuration
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +0200115 * Returns 0 in case of success, 1 in case of allocation failure.
William Lallemand82fe75c2012-10-23 10:25:10 +0200116 */
117int comp_append_type(struct comp *comp, const char *type)
118{
119 struct comp_type *comp_type;
120
Vincent Bernat02779b62016-04-03 13:48:43 +0200121 comp_type = calloc(1, sizeof(*comp_type));
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +0200122 if (!comp_type)
123 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200124 comp_type->name_len = strlen(type);
125 comp_type->name = strdup(type);
126 comp_type->next = comp->types;
127 comp->types = comp_type;
128 return 0;
129}
130
131/*
132 * Add an algorithm in the configuration
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +0200133 * Returns 0 in case of success, -1 if the <algo> is unmanaged, 1 in case of
134 * allocation failure.
William Lallemand82fe75c2012-10-23 10:25:10 +0200135 */
136int comp_append_algo(struct comp *comp, const char *algo)
137{
138 struct comp_algo *comp_algo;
139 int i;
140
Willy Tarreau615105e2015-03-28 16:40:46 +0100141 for (i = 0; comp_algos[i].cfg_name; i++) {
142 if (!strcmp(algo, comp_algos[i].cfg_name)) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200143 comp_algo = calloc(1, sizeof(*comp_algo));
Remi Tricot-Le Bretondcec0ae2021-05-17 10:35:08 +0200144 if (!comp_algo)
145 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +0200146 memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo));
147 comp_algo->next = comp->algos;
148 comp->algos = comp_algo;
149 return 0;
150 }
151 }
152 return -1;
153}
154
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200155#if defined(USE_ZLIB) || defined(USE_SLZ)
Willy Tarreau8ceae722018-11-26 11:58:30 +0100156DECLARE_STATIC_POOL(pool_comp_ctx, "comp_ctx", sizeof(struct comp_ctx));
157
William Lallemand8b52bb32012-11-16 18:06:41 +0100158/*
159 * Alloc the comp_ctx
160 */
161static inline int init_comp_ctx(struct comp_ctx **comp_ctx)
162{
163#ifdef USE_ZLIB
164 z_stream *strm;
165
William Lallemande3a7d992012-11-20 11:25:20 +0100166 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx))
William Lallemand8b52bb32012-11-16 18:06:41 +0100167 return -1;
168#endif
169
Willy Tarreaubafbe012017-11-24 17:34:44 +0100170 *comp_ctx = pool_alloc(pool_comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100171 if (*comp_ctx == NULL)
172 return -1;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200173#if defined(USE_SLZ)
174 (*comp_ctx)->direct_ptr = NULL;
175 (*comp_ctx)->direct_len = 0;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200176 (*comp_ctx)->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200177#elif defined(USE_ZLIB)
Olivier Houchard43da3432019-03-08 18:50:27 +0100178 _HA_ATOMIC_ADD(&zlib_used_memory, sizeof(struct comp_ctx));
179 __ha_barrier_atomic_store();
William Lallemand8b52bb32012-11-16 18:06:41 +0100180
181 strm = &(*comp_ctx)->strm;
182 strm->zalloc = alloc_zlib;
183 strm->zfree = free_zlib;
184 strm->opaque = *comp_ctx;
185#endif
186 return 0;
187}
188
189/*
190 * Dealloc the comp_ctx
191 */
192static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx)
193{
194 if (!*comp_ctx)
195 return 0;
196
Willy Tarreaubafbe012017-11-24 17:34:44 +0100197 pool_free(pool_comp_ctx, *comp_ctx);
William Lallemand8b52bb32012-11-16 18:06:41 +0100198 *comp_ctx = NULL;
199
200#ifdef USE_ZLIB
Olivier Houchard43da3432019-03-08 18:50:27 +0100201 _HA_ATOMIC_SUB(&zlib_used_memory, sizeof(struct comp_ctx));
202 __ha_barrier_atomic_store();
William Lallemand8b52bb32012-11-16 18:06:41 +0100203#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100204 return 0;
205}
Willy Tarreaue1cc4b52016-08-10 21:17:06 +0200206#endif
William Lallemand8b52bb32012-11-16 18:06:41 +0100207
William Lallemand82fe75c2012-10-23 10:25:10 +0200208
209/****************************
210 **** Identity algorithm ****
211 ****************************/
212
213/*
214 * Init the identity algorithm
215 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100216static int identity_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200217{
218 return 0;
219}
220
221/*
222 * Process data
William Lallemandbf3ae612012-11-19 12:35:37 +0100223 * Return size of consumed data or -1 on error
William Lallemand82fe75c2012-10-23 10:25:10 +0200224 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100225static 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 +0200226{
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200227 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200228 int out_len = b_room(out);
William Lallemandbf3ae612012-11-19 12:35:37 +0100229
William Lallemand82fe75c2012-10-23 10:25:10 +0200230 if (out_len < in_len)
231 return -1;
232
233 memcpy(out_data, in_data, in_len);
234
Olivier Houchardacd14032018-06-28 18:17:23 +0200235 b_add(out, in_len);
William Lallemandbf3ae612012-11-19 12:35:37 +0100236
William Lallemand82fe75c2012-10-23 10:25:10 +0200237 return in_len;
238}
239
Willy Tarreau9787efa2015-03-28 19:17:31 +0100240static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out)
William Lallemand82fe75c2012-10-23 10:25:10 +0200241{
242 return 0;
243}
244
Willy Tarreau9787efa2015-03-28 19:17:31 +0100245static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out)
246{
247 return 0;
248}
249
Willy Tarreau418b8c02015-03-29 03:32:06 +0200250/*
251 * Deinit the algorithm
252 */
253static int identity_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200254{
255 return 0;
256}
257
Willy Tarreau418b8c02015-03-29 03:32:06 +0200258
259#ifdef USE_SLZ
260
261/* SLZ's gzip format (RFC1952). Returns < 0 on error. */
262static int rfc1952_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_rfc1952_init(&(*comp_ctx)->strm, !!level);
269}
270
271/* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */
272static int rfc1951_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_rfc1951_init(&(*comp_ctx)->strm, !!level);
279}
280
281/* SLZ's zlib format (RFC1950). Returns < 0 on error. */
282static int rfc1950_init(struct comp_ctx **comp_ctx, int level)
283{
284 if (init_comp_ctx(comp_ctx) < 0)
285 return -1;
286
287 (*comp_ctx)->cur_lvl = !!level;
288 return slz_rfc1950_init(&(*comp_ctx)->strm, !!level);
289}
290
291/* Return the size of consumed data or -1. The output buffer is unused at this
292 * point, we only keep a reference to the input data or a copy of them if the
293 * reference is already used.
William Lallemand82fe75c2012-10-23 10:25:10 +0200294 */
Willy Tarreau418b8c02015-03-29 03:32:06 +0200295static 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 +0200296{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200297 static THREAD_LOCAL struct buffer tmpbuf = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200298
299 if (in_len <= 0)
300 return 0;
301
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200302 if (comp_ctx->direct_ptr && b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200303 /* data already being pointed to, we're in front of fragmented
304 * data and need a buffer now. We reuse the same buffer, as it's
305 * not used out of the scope of a series of add_data()*, end().
306 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200307 if (unlikely(!tmpbuf.size)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200308 /* this is the first time we need the compression buffer */
309 if (b_alloc(&tmpbuf) == NULL)
310 return -1; /* no memory */
311 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200312 b_reset(&tmpbuf);
313 memcpy(b_tail(&tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len);
314 b_add(&tmpbuf, comp_ctx->direct_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200315 comp_ctx->direct_ptr = NULL;
316 comp_ctx->direct_len = 0;
317 comp_ctx->queued = tmpbuf;
318 /* fall through buffer copy */
319 }
320
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (!b_is_null(&comp_ctx->queued)) {
Willy Tarreau418b8c02015-03-29 03:32:06 +0200322 /* data already pending */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200323 memcpy(b_tail(&comp_ctx->queued), in_data, in_len);
324 b_add(&comp_ctx->queued, in_len);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200325 return in_len;
326 }
327
328 comp_ctx->direct_ptr = in_data;
329 comp_ctx->direct_len = in_len;
330 return in_len;
331}
332
333/* Compresses the data accumulated using add_data(), and optionally sends the
334 * format-specific trailer if <finish> is non-null. <out> is expected to have a
335 * large enough free non-wrapping space as verified by http_comp_buffer_init().
336 * The number of bytes emitted is reported.
337 */
338static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish)
339{
340 struct slz_stream *strm = &comp_ctx->strm;
341 const char *in_ptr;
342 int in_len;
343 int out_len;
344
345 in_ptr = comp_ctx->direct_ptr;
346 in_len = comp_ctx->direct_len;
347
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200348 if (!b_is_null(&comp_ctx->queued)) {
349 in_ptr = b_head(&comp_ctx->queued);
350 in_len = b_data(&comp_ctx->queued);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200351 }
352
Olivier Houchard0b662842018-06-29 18:16:31 +0200353 out_len = b_data(out);
Willy Tarreau418b8c02015-03-29 03:32:06 +0200354
355 if (in_ptr)
Olivier Houchardacd14032018-06-28 18:17:23 +0200356 b_add(out, slz_encode(strm, b_tail(out), in_ptr, in_len, !finish));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200357
358 if (finish)
Olivier Houchardacd14032018-06-28 18:17:23 +0200359 b_add(out, slz_finish(strm, b_tail(out)));
Willy Tarreau418b8c02015-03-29 03:32:06 +0200360
Olivier Houchard0b662842018-06-29 18:16:31 +0200361 out_len = b_data(out) - out_len;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200362
363 /* very important, we must wipe the data we've just flushed */
364 comp_ctx->direct_len = 0;
365 comp_ctx->direct_ptr = NULL;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200366 comp_ctx->queued = BUF_NULL;
Willy Tarreau418b8c02015-03-29 03:32:06 +0200367
368 /* Verify compression rate limiting and CPU usage */
369 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 +0200370 (ti->idle_pct < compress_min_idle)) { /* idle */
Willy Tarreau418b8c02015-03-29 03:32:06 +0200371 if (comp_ctx->cur_lvl > 0)
372 strm->level = --comp_ctx->cur_lvl;
373 }
374 else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) {
375 strm->level = ++comp_ctx->cur_lvl;
376 }
377
378 /* and that's all */
379 return out_len;
380}
381
382static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out)
383{
384 return rfc195x_flush_or_finish(comp_ctx, out, 0);
385}
386
387static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out)
388{
389 return rfc195x_flush_or_finish(comp_ctx, out, 1);
390}
391
392/* we just need to free the comp_ctx here, nothing was allocated */
393static int rfc195x_end(struct comp_ctx **comp_ctx)
394{
395 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200396 return 0;
397}
398
Willy Tarreau418b8c02015-03-29 03:32:06 +0200399#elif defined(USE_ZLIB) /* ! USE_SLZ */
William Lallemand82fe75c2012-10-23 10:25:10 +0200400
William Lallemand2b502472012-10-30 14:30:39 +0100401/*
402 * This is a tricky allocation function using the zlib.
403 * This is based on the allocation order in deflateInit2.
404 */
405static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size)
406{
407 struct comp_ctx *ctx = opaque;
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200408 static THREAD_LOCAL char round = 0; /* order in deflateInit2 */
William Lallemand2b502472012-10-30 14:30:39 +0100409 void *buf = NULL;
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100410 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100411
William Lallemande3a7d992012-11-20 11:25:20 +0100412 if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size))
William Lallemand9d5f5482012-11-07 16:12:57 +0100413 goto end;
William Lallemand9d5f5482012-11-07 16:12:57 +0100414
William Lallemand2b502472012-10-30 14:30:39 +0100415 switch (round) {
416 case 0:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200417 if (zlib_pool_deflate_state == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100418 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200419 if (zlib_pool_deflate_state == NULL)
420 zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100421 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200422 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100423 pool = zlib_pool_deflate_state;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100424 ctx->zlib_deflate_state = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100425 break;
426
427 case 1:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200428 if (zlib_pool_window == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100429 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200430 if (zlib_pool_window == NULL)
431 zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100432 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200433 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100434 pool = zlib_pool_window;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100435 ctx->zlib_window = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100436 break;
437
438 case 2:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200439 if (zlib_pool_prev == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100440 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200441 if (zlib_pool_prev == NULL)
442 zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100443 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200444 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100445 pool = zlib_pool_prev;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100446 ctx->zlib_prev = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100447 break;
448
449 case 3:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200450 if (zlib_pool_head == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100451 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200452 if (zlib_pool_head == NULL)
453 zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100454 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200455 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100456 pool = zlib_pool_head;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100457 ctx->zlib_head = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100458 break;
459
460 case 4:
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200461 if (zlib_pool_pending_buf == NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100462 HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200463 if (zlib_pool_pending_buf == NULL)
464 zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100465 HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock);
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200466 }
Willy Tarreau4f31fc22014-12-24 18:07:55 +0100467 pool = zlib_pool_pending_buf;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100468 ctx->zlib_pending_buf = buf = pool_alloc(pool);
William Lallemand2b502472012-10-30 14:30:39 +0100469 break;
470 }
Olivier Houchard43da3432019-03-08 18:50:27 +0100471 if (buf != NULL) {
472 _HA_ATOMIC_ADD(&zlib_used_memory, pool->size);
473 __ha_barrier_atomic_store();
474 }
William Lallemand9d5f5482012-11-07 16:12:57 +0100475
476end:
William Lallemand2b502472012-10-30 14:30:39 +0100477
Willy Tarreau46909852012-11-15 14:57:56 +0100478 /* deflateInit2() first allocates and checks the deflate_state, then if
479 * it succeeds, it allocates all other 4 areas at ones and checks them
480 * at the end. So we want to correctly count the rounds depending on when
481 * zlib is supposed to abort.
482 */
483 if (buf || round)
484 round = (round + 1) % 5;
William Lallemand2b502472012-10-30 14:30:39 +0100485 return buf;
486}
487
488static void free_zlib(void *opaque, void *ptr)
489{
490 struct comp_ctx *ctx = opaque;
Willy Tarreaub1fbd052012-11-10 17:49:37 +0100491 struct pool_head *pool = NULL;
William Lallemand2b502472012-10-30 14:30:39 +0100492
493 if (ptr == ctx->zlib_window)
William Lallemand9d5f5482012-11-07 16:12:57 +0100494 pool = zlib_pool_window;
William Lallemand2b502472012-10-30 14:30:39 +0100495 else if (ptr == ctx->zlib_deflate_state)
William Lallemand9d5f5482012-11-07 16:12:57 +0100496 pool = zlib_pool_deflate_state;
William Lallemand2b502472012-10-30 14:30:39 +0100497 else if (ptr == ctx->zlib_prev)
William Lallemand9d5f5482012-11-07 16:12:57 +0100498 pool = zlib_pool_prev;
William Lallemand2b502472012-10-30 14:30:39 +0100499 else if (ptr == ctx->zlib_head)
William Lallemand9d5f5482012-11-07 16:12:57 +0100500 pool = zlib_pool_head;
William Lallemand2b502472012-10-30 14:30:39 +0100501 else if (ptr == ctx->zlib_pending_buf)
William Lallemand9d5f5482012-11-07 16:12:57 +0100502 pool = zlib_pool_pending_buf;
William Lallemand2b502472012-10-30 14:30:39 +0100503
Willy Tarreaubafbe012017-11-24 17:34:44 +0100504 pool_free(pool, ptr);
Olivier Houchard43da3432019-03-08 18:50:27 +0100505 _HA_ATOMIC_SUB(&zlib_used_memory, pool->size);
506 __ha_barrier_atomic_store();
William Lallemand2b502472012-10-30 14:30:39 +0100507}
508
William Lallemand82fe75c2012-10-23 10:25:10 +0200509/**************************
510**** gzip algorithm ****
511***************************/
Willy Tarreau9f640a12015-03-28 15:46:00 +0100512static int gzip_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200513{
William Lallemand8b52bb32012-11-16 18:06:41 +0100514 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200515
William Lallemand8b52bb32012-11-16 18:06:41 +0100516 if (init_comp_ctx(comp_ctx) < 0)
517 return -1;
William Lallemand9d5f5482012-11-07 16:12:57 +0100518
William Lallemand8b52bb32012-11-16 18:06:41 +0100519 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200520
Willy Tarreau36878032016-12-22 19:46:17 +0100521 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 +0100522 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200523 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100524 }
525
526 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200527
528 return 0;
529}
Willy Tarreauc91840a2015-03-28 17:00:39 +0100530
531/* Raw deflate algorithm */
532static int raw_def_init(struct comp_ctx **comp_ctx, int level)
533{
534 z_stream *strm;
535
536 if (init_comp_ctx(comp_ctx) < 0)
537 return -1;
538
539 strm = &(*comp_ctx)->strm;
540
Willy Tarreau36878032016-12-22 19:46:17 +0100541 if (deflateInit2(strm, level, Z_DEFLATED, -global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
Willy Tarreauc91840a2015-03-28 17:00:39 +0100542 deinit_comp_ctx(comp_ctx);
543 return -1;
544 }
545
546 (*comp_ctx)->cur_lvl = level;
547 return 0;
548}
549
William Lallemand82fe75c2012-10-23 10:25:10 +0200550/**************************
551**** Deflate algorithm ****
552***************************/
553
Willy Tarreau9f640a12015-03-28 15:46:00 +0100554static int deflate_init(struct comp_ctx **comp_ctx, int level)
William Lallemand82fe75c2012-10-23 10:25:10 +0200555{
William Lallemand8b52bb32012-11-16 18:06:41 +0100556 z_stream *strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200557
William Lallemand8b52bb32012-11-16 18:06:41 +0100558 if (init_comp_ctx(comp_ctx) < 0)
559 return -1;
560
561 strm = &(*comp_ctx)->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200562
Willy Tarreau36878032016-12-22 19:46:17 +0100563 if (deflateInit2(strm, level, Z_DEFLATED, global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
William Lallemand8b52bb32012-11-16 18:06:41 +0100564 deinit_comp_ctx(comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +0200565 return -1;
William Lallemand8b52bb32012-11-16 18:06:41 +0100566 }
567
568 (*comp_ctx)->cur_lvl = level;
William Lallemand82fe75c2012-10-23 10:25:10 +0200569
570 return 0;
571}
572
William Lallemandbf3ae612012-11-19 12:35:37 +0100573/* Return the size of consumed data or -1 */
Willy Tarreau9f640a12015-03-28 15:46:00 +0100574static 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 +0200575{
William Lallemand82fe75c2012-10-23 10:25:10 +0200576 int ret;
William Lallemandbf3ae612012-11-19 12:35:37 +0100577 z_stream *strm = &comp_ctx->strm;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200578 char *out_data = b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200579 int out_len = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200580
581 if (in_len <= 0)
582 return 0;
583
584
585 if (out_len <= 0)
586 return -1;
587
William Lallemand82fe75c2012-10-23 10:25:10 +0200588 strm->next_in = (unsigned char *)in_data;
589 strm->avail_in = in_len;
590 strm->next_out = (unsigned char *)out_data;
591 strm->avail_out = out_len;
592
593 ret = deflate(strm, Z_NO_FLUSH);
594 if (ret != Z_OK)
595 return -1;
596
597 /* deflate update the available data out */
Olivier Houchardacd14032018-06-28 18:17:23 +0200598 b_add(out, out_len - strm->avail_out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200599
William Lallemandbf3ae612012-11-19 12:35:37 +0100600 return in_len - strm->avail_in;
William Lallemand82fe75c2012-10-23 10:25:10 +0200601}
602
Willy Tarreau9787efa2015-03-28 19:17:31 +0100603static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int flag)
William Lallemand82fe75c2012-10-23 10:25:10 +0200604{
605 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200606 int out_len = 0;
William Lallemand1c2d6222012-10-30 15:52:53 +0100607 z_stream *strm = &comp_ctx->strm;
William Lallemand82fe75c2012-10-23 10:25:10 +0200608
Willy Tarreaud8b8b532016-08-08 16:41:01 +0200609 strm->next_in = NULL;
610 strm->avail_in = 0;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200611 strm->next_out = (unsigned char *)b_tail(out);
Willy Tarreaueac52592018-06-15 13:59:36 +0200612 strm->avail_out = b_room(out);
William Lallemand82fe75c2012-10-23 10:25:10 +0200613
614 ret = deflate(strm, flag);
615 if (ret != Z_OK && ret != Z_STREAM_END)
616 return -1;
617
Willy Tarreaueac52592018-06-15 13:59:36 +0200618 out_len = b_room(out) - strm->avail_out;
Olivier Houchardacd14032018-06-28 18:17:23 +0200619 b_add(out, out_len);
William Lallemand82fe75c2012-10-23 10:25:10 +0200620
William Lallemand072a2bf2012-11-20 17:01:01 +0100621 /* compression limit */
622 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 +0200623 (ti->idle_pct < compress_min_idle)) { /* idle */
William Lallemand072a2bf2012-11-20 17:01:01 +0100624 /* decrease level */
625 if (comp_ctx->cur_lvl > 0) {
626 comp_ctx->cur_lvl--;
William Lallemandd85f9172012-11-09 17:05:39 +0100627 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
628 }
William Lallemand072a2bf2012-11-20 17:01:01 +0100629
630 } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) {
631 /* increase level */
632 comp_ctx->cur_lvl++ ;
633 deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY);
William Lallemandd85f9172012-11-09 17:05:39 +0100634 }
635
William Lallemand82fe75c2012-10-23 10:25:10 +0200636 return out_len;
637}
638
Willy Tarreau9787efa2015-03-28 19:17:31 +0100639static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out)
640{
641 return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH);
642}
643
644static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out)
645{
646 return deflate_flush_or_finish(comp_ctx, out, Z_FINISH);
647}
648
Willy Tarreau9f640a12015-03-28 15:46:00 +0100649static int deflate_end(struct comp_ctx **comp_ctx)
William Lallemand82fe75c2012-10-23 10:25:10 +0200650{
William Lallemand8b52bb32012-11-16 18:06:41 +0100651 z_stream *strm = &(*comp_ctx)->strm;
652 int ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200653
William Lallemand8b52bb32012-11-16 18:06:41 +0100654 ret = deflateEnd(strm);
William Lallemand82fe75c2012-10-23 10:25:10 +0200655
William Lallemand8b52bb32012-11-16 18:06:41 +0100656 deinit_comp_ctx(comp_ctx);
657
658 return ret;
William Lallemand82fe75c2012-10-23 10:25:10 +0200659}
660
Willy Tarreau36878032016-12-22 19:46:17 +0100661/* config parser for global "tune.zlibmemlevel" */
662static int zlib_parse_global_memlevel(char **args, int section_type, struct proxy *curpx,
663 struct proxy *defpx, const char *file, int line,
664 char **err)
665{
666 if (too_many_args(1, args, err, NULL))
667 return -1;
668
669 if (*(args[1]) == 0) {
670 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
671 return -1;
672 }
673
674 global_tune_zlibmemlevel = atoi(args[1]);
675 if (global_tune_zlibmemlevel < 1 || global_tune_zlibmemlevel > 9) {
676 memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]);
677 return -1;
678 }
679 return 0;
680}
681
682
683/* config parser for global "tune.zlibwindowsize" */
684static int zlib_parse_global_windowsize(char **args, int section_type, struct proxy *curpx,
685 struct proxy *defpx, const char *file, int line,
686 char **err)
687{
688 if (too_many_args(1, args, err, NULL))
689 return -1;
690
691 if (*(args[1]) == 0) {
692 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
693 return -1;
694 }
695
696 global_tune_zlibwindowsize = atoi(args[1]);
697 if (global_tune_zlibwindowsize < 8 || global_tune_zlibwindowsize > 15) {
698 memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]);
699 return -1;
700 }
701 return 0;
702}
703
William Lallemand82fe75c2012-10-23 10:25:10 +0200704#endif /* USE_ZLIB */
705
Willy Tarreau36878032016-12-22 19:46:17 +0100706
707/* config keyword parsers */
708static struct cfg_kw_list cfg_kws = {ILH, {
709#ifdef USE_ZLIB
710 { CFG_GLOBAL, "tune.zlib.memlevel", zlib_parse_global_memlevel },
711 { CFG_GLOBAL, "tune.zlib.windowsize", zlib_parse_global_windowsize },
712#endif
713 { 0, NULL, NULL }
714}};
715
Willy Tarreau0108d902018-11-25 19:14:37 +0100716INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
717
William Lallemand727db8b2013-04-20 17:33:20 +0200718__attribute__((constructor))
719static void __comp_fetch_init(void)
720{
Willy Tarreau418b8c02015-03-29 03:32:06 +0200721#ifdef USE_SLZ
722 slz_make_crc_table();
723 slz_prepare_dist_table();
724#endif
Willy Tarreaue5489742018-11-26 14:44:03 +0100725
Willy Tarreau36878032016-12-22 19:46:17 +0100726#if defined(USE_ZLIB) && defined(DEFAULT_MAXZLIBMEM)
Willy Tarreau3bfcd102018-11-26 10:24:45 +0100727 global.maxzlibmem = DEFAULT_MAXZLIBMEM * 1024U * 1024U;
Willy Tarreau36878032016-12-22 19:46:17 +0100728#endif
Willy Tarreau80713382018-11-26 10:19:54 +0100729}
730
731static void comp_register_build_opts(void)
732{
733 char *ptr = NULL;
734 int i;
735
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100736#ifdef USE_ZLIB
737 memprintf(&ptr, "Built with zlib version : " ZLIB_VERSION);
738 memprintf(&ptr, "%s\nRunning on zlib version : %s", ptr, zlibVersion());
739#elif defined(USE_SLZ)
740 memprintf(&ptr, "Built with libslz for stateless compression.");
Lukas Tribusb7323212017-01-11 14:24:35 +0000741#else
742 memprintf(&ptr, "Built without compression support (neither USE_ZLIB nor USE_SLZ are set).");
Willy Tarreaub97c6fb2016-12-21 19:30:30 +0100743#endif
744 memprintf(&ptr, "%s\nCompression algorithms supported :", ptr);
745
746 for (i = 0; comp_algos[i].cfg_name; i++)
747 memprintf(&ptr, "%s%s %s(\"%s\")", ptr, (i == 0 ? "" : ","), comp_algos[i].cfg_name, comp_algos[i].ua_name);
748
749 if (i == 0)
750 memprintf(&ptr, "%s none", ptr);
751
752 hap_register_build_opts(ptr, 1);
William Lallemand727db8b2013-04-20 17:33:20 +0200753}
Willy Tarreau80713382018-11-26 10:19:54 +0100754
755INITCALL0(STG_REGISTER, comp_register_build_opts);