William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 16 | #if defined(USE_SLZ) |
| 17 | #include <slz.h> |
| 18 | #elif defined(USE_ZLIB) |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 19 | /* 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 Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 25 | #include <zlib.h> |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 26 | #undef free_func |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 27 | #endif /* USE_ZLIB */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 28 | |
| 29 | #include <common/compat.h> |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 30 | #include <common/memory.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 31 | |
| 32 | #include <types/global.h> |
| 33 | #include <types/compression.h> |
| 34 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 35 | #include <proto/acl.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 36 | #include <proto/compression.h> |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 37 | #include <proto/freq_ctr.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 38 | #include <proto/proto_http.h> |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 39 | #include <proto/stream.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 40 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 41 | |
| 42 | #ifdef USE_ZLIB |
| 43 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 44 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size); |
| 45 | static void free_zlib(void *opaque, void *ptr); |
| 46 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 47 | /* zlib allocation */ |
| 48 | static struct pool_head *zlib_pool_deflate_state = NULL; |
| 49 | static struct pool_head *zlib_pool_window = NULL; |
| 50 | static struct pool_head *zlib_pool_prev = NULL; |
| 51 | static struct pool_head *zlib_pool_head = NULL; |
| 52 | static struct pool_head *zlib_pool_pending_buf = NULL; |
| 53 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 54 | long zlib_used_memory = 0; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 55 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 56 | #endif |
| 57 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 58 | unsigned int compress_min_idle = 0; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 59 | static struct pool_head *pool_comp_ctx = NULL; |
| 60 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 61 | static int identity_init(struct comp_ctx **comp_ctx, int level); |
| 62 | static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out); |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 63 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 64 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 65 | static int identity_end(struct comp_ctx **comp_ctx); |
| 66 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 67 | #if defined(USE_SLZ) |
| 68 | |
| 69 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level); |
| 70 | static int rfc1951_init(struct comp_ctx **comp_ctx, int level); |
| 71 | static int rfc1952_init(struct comp_ctx **comp_ctx, int level); |
| 72 | static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out); |
| 73 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 74 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
| 75 | static int rfc195x_end(struct comp_ctx **comp_ctx); |
| 76 | |
| 77 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 79 | static int gzip_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 80 | static int raw_def_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 81 | static int deflate_init(struct comp_ctx **comp_ctx, int level); |
| 82 | static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out); |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 83 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 84 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 85 | static int deflate_end(struct comp_ctx **comp_ctx); |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 86 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 87 | #endif /* USE_ZLIB */ |
| 88 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 89 | |
Cyril Bonté | 6162c43 | 2012-11-10 19:27:47 +0100 | [diff] [blame] | 90 | const struct comp_algo comp_algos[] = |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 91 | { |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 92 | { "identity", 8, "identity", 8, identity_init, identity_add_data, identity_flush, identity_finish, identity_end }, |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 93 | #if defined(USE_SLZ) |
| 94 | { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 95 | { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 96 | { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 97 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 98 | { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 99 | { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 100 | { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 101 | #endif /* USE_ZLIB */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 102 | { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * Add a content-type in the configuration |
| 107 | */ |
| 108 | int comp_append_type(struct comp *comp, const char *type) |
| 109 | { |
| 110 | struct comp_type *comp_type; |
| 111 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 112 | comp_type = calloc(1, sizeof(*comp_type)); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 113 | comp_type->name_len = strlen(type); |
| 114 | comp_type->name = strdup(type); |
| 115 | comp_type->next = comp->types; |
| 116 | comp->types = comp_type; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Add an algorithm in the configuration |
| 122 | */ |
| 123 | int comp_append_algo(struct comp *comp, const char *algo) |
| 124 | { |
| 125 | struct comp_algo *comp_algo; |
| 126 | int i; |
| 127 | |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 128 | for (i = 0; comp_algos[i].cfg_name; i++) { |
| 129 | if (!strcmp(algo, comp_algos[i].cfg_name)) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 130 | comp_algo = calloc(1, sizeof(*comp_algo)); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 131 | memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); |
| 132 | comp_algo->next = comp->algos; |
| 133 | comp->algos = comp_algo; |
| 134 | return 0; |
| 135 | } |
| 136 | } |
| 137 | return -1; |
| 138 | } |
| 139 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 140 | /* |
| 141 | * Alloc the comp_ctx |
| 142 | */ |
| 143 | static inline int init_comp_ctx(struct comp_ctx **comp_ctx) |
| 144 | { |
| 145 | #ifdef USE_ZLIB |
| 146 | z_stream *strm; |
| 147 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 148 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < sizeof(struct comp_ctx)) |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 149 | return -1; |
| 150 | #endif |
| 151 | |
| 152 | if (unlikely(pool_comp_ctx == NULL)) |
| 153 | pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED); |
| 154 | |
| 155 | *comp_ctx = pool_alloc2(pool_comp_ctx); |
| 156 | if (*comp_ctx == NULL) |
| 157 | return -1; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 158 | #if defined(USE_SLZ) |
| 159 | (*comp_ctx)->direct_ptr = NULL; |
| 160 | (*comp_ctx)->direct_len = 0; |
| 161 | (*comp_ctx)->queued = NULL; |
| 162 | #elif defined(USE_ZLIB) |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 163 | zlib_used_memory += sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 164 | |
| 165 | strm = &(*comp_ctx)->strm; |
| 166 | strm->zalloc = alloc_zlib; |
| 167 | strm->zfree = free_zlib; |
| 168 | strm->opaque = *comp_ctx; |
| 169 | #endif |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Dealloc the comp_ctx |
| 175 | */ |
| 176 | static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx) |
| 177 | { |
| 178 | if (!*comp_ctx) |
| 179 | return 0; |
| 180 | |
| 181 | pool_free2(pool_comp_ctx, *comp_ctx); |
| 182 | *comp_ctx = NULL; |
| 183 | |
| 184 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 185 | zlib_used_memory -= sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 186 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 190 | |
| 191 | /**************************** |
| 192 | **** Identity algorithm **** |
| 193 | ****************************/ |
| 194 | |
| 195 | /* |
| 196 | * Init the identity algorithm |
| 197 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 198 | static int identity_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 199 | { |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Process data |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 205 | * Return size of consumed data or -1 on error |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 206 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 207 | static int identity_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 208 | { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 209 | char *out_data = bi_end(out); |
| 210 | int out_len = out->size - buffer_len(out); |
| 211 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 212 | if (out_len < in_len) |
| 213 | return -1; |
| 214 | |
| 215 | memcpy(out_data, in_data, in_len); |
| 216 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 217 | out->i += in_len; |
| 218 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 219 | return in_len; |
| 220 | } |
| 221 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 222 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 223 | { |
| 224 | return 0; |
| 225 | } |
| 226 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 227 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 228 | { |
| 229 | return 0; |
| 230 | } |
| 231 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 232 | /* |
| 233 | * Deinit the algorithm |
| 234 | */ |
| 235 | static int identity_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 236 | { |
| 237 | return 0; |
| 238 | } |
| 239 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 240 | |
| 241 | #ifdef USE_SLZ |
| 242 | |
| 243 | /* SLZ's gzip format (RFC1952). Returns < 0 on error. */ |
| 244 | static int rfc1952_init(struct comp_ctx **comp_ctx, int level) |
| 245 | { |
| 246 | if (init_comp_ctx(comp_ctx) < 0) |
| 247 | return -1; |
| 248 | |
| 249 | (*comp_ctx)->cur_lvl = !!level; |
| 250 | return slz_rfc1952_init(&(*comp_ctx)->strm, !!level); |
| 251 | } |
| 252 | |
| 253 | /* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */ |
| 254 | static int rfc1951_init(struct comp_ctx **comp_ctx, int level) |
| 255 | { |
| 256 | if (init_comp_ctx(comp_ctx) < 0) |
| 257 | return -1; |
| 258 | |
| 259 | (*comp_ctx)->cur_lvl = !!level; |
| 260 | return slz_rfc1951_init(&(*comp_ctx)->strm, !!level); |
| 261 | } |
| 262 | |
| 263 | /* SLZ's zlib format (RFC1950). Returns < 0 on error. */ |
| 264 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level) |
| 265 | { |
| 266 | if (init_comp_ctx(comp_ctx) < 0) |
| 267 | return -1; |
| 268 | |
| 269 | (*comp_ctx)->cur_lvl = !!level; |
| 270 | return slz_rfc1950_init(&(*comp_ctx)->strm, !!level); |
| 271 | } |
| 272 | |
| 273 | /* Return the size of consumed data or -1. The output buffer is unused at this |
| 274 | * point, we only keep a reference to the input data or a copy of them if the |
| 275 | * reference is already used. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 276 | */ |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 277 | static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 278 | { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 279 | static struct buffer *tmpbuf = &buf_empty; |
| 280 | |
| 281 | if (in_len <= 0) |
| 282 | return 0; |
| 283 | |
| 284 | if (comp_ctx->direct_ptr && !comp_ctx->queued) { |
| 285 | /* data already being pointed to, we're in front of fragmented |
| 286 | * data and need a buffer now. We reuse the same buffer, as it's |
| 287 | * not used out of the scope of a series of add_data()*, end(). |
| 288 | */ |
| 289 | if (unlikely(!tmpbuf->size)) { |
| 290 | /* this is the first time we need the compression buffer */ |
| 291 | if (b_alloc(&tmpbuf) == NULL) |
| 292 | return -1; /* no memory */ |
| 293 | } |
| 294 | b_reset(tmpbuf); |
| 295 | memcpy(bi_end(tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len); |
| 296 | tmpbuf->i += comp_ctx->direct_len; |
| 297 | comp_ctx->direct_ptr = NULL; |
| 298 | comp_ctx->direct_len = 0; |
| 299 | comp_ctx->queued = tmpbuf; |
| 300 | /* fall through buffer copy */ |
| 301 | } |
| 302 | |
| 303 | if (comp_ctx->queued) { |
| 304 | /* data already pending */ |
| 305 | memcpy(bi_end(comp_ctx->queued), in_data, in_len); |
| 306 | comp_ctx->queued->i += in_len; |
| 307 | return in_len; |
| 308 | } |
| 309 | |
| 310 | comp_ctx->direct_ptr = in_data; |
| 311 | comp_ctx->direct_len = in_len; |
| 312 | return in_len; |
| 313 | } |
| 314 | |
| 315 | /* Compresses the data accumulated using add_data(), and optionally sends the |
| 316 | * format-specific trailer if <finish> is non-null. <out> is expected to have a |
| 317 | * large enough free non-wrapping space as verified by http_comp_buffer_init(). |
| 318 | * The number of bytes emitted is reported. |
| 319 | */ |
| 320 | static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish) |
| 321 | { |
| 322 | struct slz_stream *strm = &comp_ctx->strm; |
| 323 | const char *in_ptr; |
| 324 | int in_len; |
| 325 | int out_len; |
| 326 | |
| 327 | in_ptr = comp_ctx->direct_ptr; |
| 328 | in_len = comp_ctx->direct_len; |
| 329 | |
| 330 | if (comp_ctx->queued) { |
| 331 | in_ptr = comp_ctx->queued->p; |
| 332 | in_len = comp_ctx->queued->i; |
| 333 | } |
| 334 | |
| 335 | out_len = out->i; |
| 336 | |
| 337 | if (in_ptr) |
| 338 | out->i += slz_encode(strm, bi_end(out), in_ptr, in_len, !finish); |
| 339 | |
| 340 | if (finish) |
| 341 | out->i += slz_finish(strm, bi_end(out)); |
| 342 | |
| 343 | out_len = out->i - out_len; |
| 344 | |
| 345 | /* very important, we must wipe the data we've just flushed */ |
| 346 | comp_ctx->direct_len = 0; |
| 347 | comp_ctx->direct_ptr = NULL; |
| 348 | comp_ctx->queued = NULL; |
| 349 | |
| 350 | /* Verify compression rate limiting and CPU usage */ |
| 351 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
| 352 | (idle_pct < compress_min_idle)) { /* idle */ |
| 353 | if (comp_ctx->cur_lvl > 0) |
| 354 | strm->level = --comp_ctx->cur_lvl; |
| 355 | } |
| 356 | else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) { |
| 357 | strm->level = ++comp_ctx->cur_lvl; |
| 358 | } |
| 359 | |
| 360 | /* and that's all */ |
| 361 | return out_len; |
| 362 | } |
| 363 | |
| 364 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 365 | { |
| 366 | return rfc195x_flush_or_finish(comp_ctx, out, 0); |
| 367 | } |
| 368 | |
| 369 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 370 | { |
| 371 | return rfc195x_flush_or_finish(comp_ctx, out, 1); |
| 372 | } |
| 373 | |
| 374 | /* we just need to free the comp_ctx here, nothing was allocated */ |
| 375 | static int rfc195x_end(struct comp_ctx **comp_ctx) |
| 376 | { |
| 377 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 381 | #elif defined(USE_ZLIB) /* ! USE_SLZ */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 382 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 383 | /* |
| 384 | * This is a tricky allocation function using the zlib. |
| 385 | * This is based on the allocation order in deflateInit2. |
| 386 | */ |
| 387 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size) |
| 388 | { |
| 389 | struct comp_ctx *ctx = opaque; |
| 390 | static char round = 0; /* order in deflateInit2 */ |
| 391 | void *buf = NULL; |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 392 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 393 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 394 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size)) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 395 | goto end; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 396 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 397 | switch (round) { |
| 398 | case 0: |
| 399 | if (zlib_pool_deflate_state == NULL) |
| 400 | zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 401 | pool = zlib_pool_deflate_state; |
| 402 | ctx->zlib_deflate_state = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | case 1: |
| 406 | if (zlib_pool_window == NULL) |
| 407 | zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 408 | pool = zlib_pool_window; |
| 409 | ctx->zlib_window = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | case 2: |
| 413 | if (zlib_pool_prev == NULL) |
| 414 | zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 415 | pool = zlib_pool_prev; |
| 416 | ctx->zlib_prev = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 417 | break; |
| 418 | |
| 419 | case 3: |
| 420 | if (zlib_pool_head == NULL) |
| 421 | zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 422 | pool = zlib_pool_head; |
| 423 | ctx->zlib_head = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | case 4: |
| 427 | if (zlib_pool_pending_buf == NULL) |
| 428 | zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED); |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 429 | pool = zlib_pool_pending_buf; |
| 430 | ctx->zlib_pending_buf = buf = pool_alloc2(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 431 | break; |
| 432 | } |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 433 | if (buf != NULL) |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 434 | zlib_used_memory += pool->size; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 435 | |
| 436 | end: |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 437 | |
Willy Tarreau | 4690985 | 2012-11-15 14:57:56 +0100 | [diff] [blame] | 438 | /* deflateInit2() first allocates and checks the deflate_state, then if |
| 439 | * it succeeds, it allocates all other 4 areas at ones and checks them |
| 440 | * at the end. So we want to correctly count the rounds depending on when |
| 441 | * zlib is supposed to abort. |
| 442 | */ |
| 443 | if (buf || round) |
| 444 | round = (round + 1) % 5; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 445 | return buf; |
| 446 | } |
| 447 | |
| 448 | static void free_zlib(void *opaque, void *ptr) |
| 449 | { |
| 450 | struct comp_ctx *ctx = opaque; |
Willy Tarreau | b1fbd05 | 2012-11-10 17:49:37 +0100 | [diff] [blame] | 451 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 452 | |
| 453 | if (ptr == ctx->zlib_window) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 454 | pool = zlib_pool_window; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 455 | else if (ptr == ctx->zlib_deflate_state) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 456 | pool = zlib_pool_deflate_state; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 457 | else if (ptr == ctx->zlib_prev) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 458 | pool = zlib_pool_prev; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 459 | else if (ptr == ctx->zlib_head) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 460 | pool = zlib_pool_head; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 461 | else if (ptr == ctx->zlib_pending_buf) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 462 | pool = zlib_pool_pending_buf; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 463 | |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 464 | pool_free2(pool, ptr); |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 465 | zlib_used_memory -= pool->size; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 466 | } |
| 467 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 468 | /************************** |
| 469 | **** gzip algorithm **** |
| 470 | ***************************/ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 471 | static int gzip_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 472 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 473 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 474 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 475 | if (init_comp_ctx(comp_ctx) < 0) |
| 476 | return -1; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 477 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 478 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 479 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 480 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 481 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 482 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 486 | |
| 487 | return 0; |
| 488 | } |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 489 | |
| 490 | /* Raw deflate algorithm */ |
| 491 | static int raw_def_init(struct comp_ctx **comp_ctx, int level) |
| 492 | { |
| 493 | z_stream *strm; |
| 494 | |
| 495 | if (init_comp_ctx(comp_ctx) < 0) |
| 496 | return -1; |
| 497 | |
| 498 | strm = &(*comp_ctx)->strm; |
| 499 | |
| 500 | if (deflateInit2(strm, level, Z_DEFLATED, -global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 501 | deinit_comp_ctx(comp_ctx); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | (*comp_ctx)->cur_lvl = level; |
| 506 | return 0; |
| 507 | } |
| 508 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 509 | /************************** |
| 510 | **** Deflate algorithm **** |
| 511 | ***************************/ |
| 512 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 513 | static int deflate_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 514 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 515 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 516 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 517 | if (init_comp_ctx(comp_ctx) < 0) |
| 518 | return -1; |
| 519 | |
| 520 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | c5599e7 | 2013-04-28 08:52:52 +0200 | [diff] [blame] | 522 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 523 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 524 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 532 | /* Return the size of consumed data or -1 */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 533 | static int deflate_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 534 | { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 535 | int ret; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 536 | z_stream *strm = &comp_ctx->strm; |
| 537 | char *out_data = bi_end(out); |
| 538 | int out_len = out->size - buffer_len(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 539 | |
| 540 | if (in_len <= 0) |
| 541 | return 0; |
| 542 | |
| 543 | |
| 544 | if (out_len <= 0) |
| 545 | return -1; |
| 546 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 547 | strm->next_in = (unsigned char *)in_data; |
| 548 | strm->avail_in = in_len; |
| 549 | strm->next_out = (unsigned char *)out_data; |
| 550 | strm->avail_out = out_len; |
| 551 | |
| 552 | ret = deflate(strm, Z_NO_FLUSH); |
| 553 | if (ret != Z_OK) |
| 554 | return -1; |
| 555 | |
| 556 | /* deflate update the available data out */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 557 | out->i += out_len - strm->avail_out; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 558 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 559 | return in_len - strm->avail_in; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 562 | static int deflate_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int flag) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 563 | { |
| 564 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 565 | int out_len = 0; |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 566 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 567 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 568 | strm->next_out = (unsigned char *)bi_end(out); |
| 569 | strm->avail_out = out->size - buffer_len(out); |
| 570 | |
| 571 | ret = deflate(strm, flag); |
| 572 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 573 | return -1; |
| 574 | |
| 575 | out_len = (out->size - buffer_len(out)) - strm->avail_out; |
| 576 | out->i += out_len; |
| 577 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 578 | /* compression limit */ |
| 579 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
| 580 | (idle_pct < compress_min_idle)) { /* idle */ |
| 581 | /* decrease level */ |
| 582 | if (comp_ctx->cur_lvl > 0) { |
| 583 | comp_ctx->cur_lvl--; |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 584 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
| 585 | } |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 586 | |
| 587 | } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) { |
| 588 | /* increase level */ |
| 589 | comp_ctx->cur_lvl++ ; |
| 590 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 591 | } |
| 592 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 593 | return out_len; |
| 594 | } |
| 595 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 596 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 597 | { |
| 598 | return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH); |
| 599 | } |
| 600 | |
| 601 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 602 | { |
| 603 | return deflate_flush_or_finish(comp_ctx, out, Z_FINISH); |
| 604 | } |
| 605 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 606 | static int deflate_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 607 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 608 | z_stream *strm = &(*comp_ctx)->strm; |
| 609 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 610 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 611 | ret = deflateEnd(strm); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 612 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 613 | deinit_comp_ctx(comp_ctx); |
| 614 | |
| 615 | return ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | #endif /* USE_ZLIB */ |
| 619 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 620 | __attribute__((constructor)) |
| 621 | static void __comp_fetch_init(void) |
| 622 | { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 623 | #ifdef USE_SLZ |
| 624 | slz_make_crc_table(); |
| 625 | slz_prepare_dist_table(); |
| 626 | #endif |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 627 | } |