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 | |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 16 | #ifdef USE_ZLIB |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 17 | /* Note: the crappy zlib and openssl libs both define the "free_func" type. |
| 18 | * That's a very clever idea to use such a generic name in general purpose |
| 19 | * libraries, really... The zlib one is easier to redefine than openssl's, |
| 20 | * so let's only fix this one. |
| 21 | */ |
| 22 | #define free_func zlib_free_func |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 23 | #include <zlib.h> |
Willy Tarreau | 3476364 | 2012-10-26 15:05:35 +0200 | [diff] [blame] | 24 | #undef free_func |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 25 | #endif /* USE_ZLIB */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 26 | |
| 27 | #include <common/compat.h> |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 28 | #include <common/memory.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 29 | |
| 30 | #include <types/global.h> |
| 31 | #include <types/compression.h> |
| 32 | |
| 33 | #include <proto/compression.h> |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 34 | #include <proto/freq_ctr.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 35 | #include <proto/proto_http.h> |
| 36 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 37 | |
| 38 | #ifdef USE_ZLIB |
| 39 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 40 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size); |
| 41 | static void free_zlib(void *opaque, void *ptr); |
| 42 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 43 | /* zlib allocation */ |
| 44 | static struct pool_head *zlib_pool_deflate_state = NULL; |
| 45 | static struct pool_head *zlib_pool_window = NULL; |
| 46 | static struct pool_head *zlib_pool_prev = NULL; |
| 47 | static struct pool_head *zlib_pool_head = NULL; |
| 48 | static struct pool_head *zlib_pool_pending_buf = NULL; |
| 49 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 50 | long zlib_used_memory = 0; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 51 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 52 | #endif |
| 53 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 54 | unsigned int compress_min_idle = 0; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 55 | static struct pool_head *pool_comp_ctx = NULL; |
| 56 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 57 | |
Cyril Bonté | 6162c43 | 2012-11-10 19:27:47 +0100 | [diff] [blame] | 58 | const struct comp_algo comp_algos[] = |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 59 | { |
| 60 | { "identity", 8, identity_init, identity_add_data, identity_flush, identity_reset, identity_end }, |
| 61 | #ifdef USE_ZLIB |
| 62 | { "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end }, |
| 63 | { "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_reset, deflate_end }, |
| 64 | #endif /* USE_ZLIB */ |
| 65 | { NULL, 0, NULL , NULL, NULL, NULL, NULL } |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Add a content-type in the configuration |
| 70 | */ |
| 71 | int comp_append_type(struct comp *comp, const char *type) |
| 72 | { |
| 73 | struct comp_type *comp_type; |
| 74 | |
| 75 | comp_type = calloc(1, sizeof(struct comp_type)); |
| 76 | comp_type->name_len = strlen(type); |
| 77 | comp_type->name = strdup(type); |
| 78 | comp_type->next = comp->types; |
| 79 | comp->types = comp_type; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Add an algorithm in the configuration |
| 85 | */ |
| 86 | int comp_append_algo(struct comp *comp, const char *algo) |
| 87 | { |
| 88 | struct comp_algo *comp_algo; |
| 89 | int i; |
| 90 | |
| 91 | for (i = 0; comp_algos[i].name; i++) { |
| 92 | if (!strcmp(algo, comp_algos[i].name)) { |
| 93 | comp_algo = calloc(1, sizeof(struct comp_algo)); |
| 94 | memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); |
| 95 | comp_algo->next = comp->algos; |
| 96 | comp->algos = comp_algo; |
| 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* emit the chunksize followed by a CRLF on the output and return the number of |
| 104 | * bytes written. Appends <add_crlf> additional CRLF after the first one. Chunk |
| 105 | * sizes are truncated to 6 hex digits (16 MB) and padded left. The caller is |
| 106 | * responsible for ensuring there is enough room left in the output buffer for |
| 107 | * the string (8 bytes * add_crlf*2). |
| 108 | */ |
| 109 | int http_emit_chunk_size(char *out, unsigned int chksz, int add_crlf) |
| 110 | { |
| 111 | int shift; |
| 112 | int pos = 0; |
| 113 | |
| 114 | for (shift = 20; shift >= 0; shift -= 4) |
| 115 | out[pos++] = hextab[(chksz >> shift) & 0xF]; |
| 116 | |
| 117 | do { |
| 118 | out[pos++] = '\r'; |
| 119 | out[pos++] = '\n'; |
| 120 | } while (--add_crlf >= 0); |
| 121 | |
| 122 | return pos; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Init HTTP compression |
| 127 | */ |
| 128 | int http_compression_buffer_init(struct session *s, struct buffer *in, struct buffer *out) |
| 129 | { |
| 130 | struct http_msg *msg = &s->txn.rsp; |
| 131 | int left; |
| 132 | |
| 133 | /* not enough space */ |
| 134 | if (in->size - buffer_len(in) < 40) |
| 135 | return -1; |
| 136 | |
| 137 | /* |
| 138 | * Skip data, we don't need them in the new buffer. They are results |
| 139 | * of CHUNK_CRLF and CHUNK_SIZE parsing. |
| 140 | */ |
| 141 | b_adv(in, msg->next); |
| 142 | msg->next = 0; |
| 143 | msg->sov = 0; |
| 144 | msg->sol = 0; |
| 145 | |
| 146 | out->size = global.tune.bufsize; |
| 147 | out->i = 0; |
| 148 | out->o = 0; |
| 149 | out->p = out->data; |
| 150 | /* copy output data */ |
| 151 | if (in->o > 0) { |
| 152 | left = in->o - bo_contig_data(in); |
| 153 | memcpy(out->data, bo_ptr(in), bo_contig_data(in)); |
| 154 | out->p += bo_contig_data(in); |
| 155 | if (left > 0) { /* second part of the buffer */ |
| 156 | memcpy(out->p, in->data, left); |
| 157 | out->p += left; |
| 158 | } |
| 159 | out->o = in->o; |
| 160 | } |
| 161 | out->i += http_emit_chunk_size(out->p, 0, 0); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Add data to compress |
| 168 | */ |
| 169 | int http_compression_buffer_add_data(struct session *s, struct buffer *in, struct buffer *out) |
| 170 | { |
| 171 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 172 | int consumed_data = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 173 | int data_process_len; |
| 174 | int left; |
| 175 | int ret; |
| 176 | |
| 177 | /* |
| 178 | * Skip data, we don't need them in the new buffer. They are results |
| 179 | * of CHUNK_CRLF and CHUNK_SIZE parsing. |
| 180 | */ |
| 181 | b_adv(in, msg->next); |
| 182 | msg->next = 0; |
| 183 | msg->sov = 0; |
| 184 | msg->sol = 0; |
| 185 | |
| 186 | /* |
| 187 | * select the smallest size between the announced chunk size, the input |
| 188 | * data, and the available output buffer size |
| 189 | */ |
| 190 | data_process_len = MIN(in->i, msg->chunk_len); |
| 191 | data_process_len = MIN(out->size - buffer_len(out), data_process_len); |
| 192 | |
| 193 | left = data_process_len - bi_contig_data(in); |
| 194 | if (left <= 0) { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 195 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), data_process_len, out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 196 | if (ret < 0) |
| 197 | return -1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 198 | |
| 199 | } else { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 200 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, bi_ptr(in), bi_contig_data(in), out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 203 | consumed_data += ret = s->comp_algo->add_data(s->comp_ctx, in->data, left, out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 204 | if (ret < 0) |
| 205 | return -1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | b_adv(in, data_process_len); |
| 209 | msg->chunk_len -= data_process_len; |
| 210 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 211 | return consumed_data; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Flush data in process, and write the header and footer of the chunk. Upon |
| 216 | * success, in and out buffers are swapped to avoid a copy. |
| 217 | */ |
| 218 | int http_compression_buffer_end(struct session *s, struct buffer **in, struct buffer **out, int end) |
| 219 | { |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 220 | int to_forward, forwarded; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 221 | int left; |
| 222 | struct http_msg *msg = &s->txn.rsp; |
| 223 | struct buffer *ib = *in, *ob = *out; |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 224 | |
| 225 | #ifdef USE_ZLIB |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 226 | int ret; |
| 227 | |
| 228 | /* flush data here */ |
| 229 | |
| 230 | if (end) |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 231 | ret = s->comp_algo->flush(s->comp_ctx, ob, Z_FINISH); /* end of data */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 232 | else |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 233 | ret = s->comp_algo->flush(s->comp_ctx, ob, Z_SYNC_FLUSH); /* end of buffer */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 234 | |
| 235 | if (ret < 0) |
| 236 | return -1; /* flush failed */ |
| 237 | |
William Lallemand | 08289f1 | 2012-10-31 11:19:18 +0100 | [diff] [blame] | 238 | #endif /* USE_ZLIB */ |
| 239 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 240 | if (ob->i > 8) { |
| 241 | /* more than a chunk size => some data were emitted */ |
| 242 | char *tail = ob->p + ob->i; |
| 243 | |
| 244 | /* write real size at the begining of the chunk, no need of wrapping */ |
| 245 | http_emit_chunk_size(ob->p, ob->i - 8, 0); |
| 246 | |
| 247 | /* chunked encoding requires CRLF after data */ |
| 248 | *tail++ = '\r'; |
| 249 | *tail++ = '\n'; |
| 250 | |
| 251 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->chunk_len == 0) { |
| 252 | /* End of data, 0<CRLF><CRLF> is needed but we're not |
| 253 | * in chunked mode on input so we must add it ourselves. |
| 254 | */ |
| 255 | memcpy(tail, "0\r\n\r\n", 5); |
| 256 | tail += 5; |
| 257 | } |
| 258 | ob->i = tail - ob->p; |
| 259 | } else { |
| 260 | /* no data were sent, cancel the chunk size */ |
| 261 | ob->i = 0; |
| 262 | } |
| 263 | |
| 264 | to_forward = ob->i; |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 265 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 266 | /* update input rate */ |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 267 | forwarded = ib->o - ob->o; |
| 268 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
| 269 | update_freq_ctr(&global.comp_bps_in, forwarded); |
| 270 | s->fe->fe_counters.comp_in += forwarded; |
| 271 | s->be->be_counters.comp_in += forwarded; |
| 272 | } else { |
| 273 | s->fe->fe_counters.comp_byp += forwarded; |
| 274 | s->be->be_counters.comp_byp += forwarded; |
| 275 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 276 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 277 | /* copy the remaining data in the tmp buffer. */ |
| 278 | if (ib->i > 0) { |
| 279 | left = ib->i - bi_contig_data(ib); |
| 280 | memcpy(bi_end(ob), bi_ptr(ib), bi_contig_data(ib)); |
| 281 | ob->i += bi_contig_data(ib); |
| 282 | if (left > 0) { |
| 283 | memcpy(bi_end(ob), ib->data, left); |
| 284 | ob->i += left; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* swap the buffers */ |
| 289 | *in = ob; |
| 290 | *out = ib; |
| 291 | |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 292 | if (s->comp_ctx && s->comp_ctx->cur_lvl > 0) { |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 293 | update_freq_ctr(&global.comp_bps_out, to_forward); |
Willy Tarreau | 55058a7 | 2012-11-21 08:27:21 +0100 | [diff] [blame] | 294 | s->fe->fe_counters.comp_out += to_forward; |
| 295 | s->be->be_counters.comp_out += to_forward; |
| 296 | } |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 297 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 298 | /* forward the new chunk without remaining data */ |
| 299 | b_adv(ob, to_forward); |
| 300 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 301 | return to_forward; |
| 302 | } |
| 303 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 304 | /* |
| 305 | * Alloc the comp_ctx |
| 306 | */ |
| 307 | static inline int init_comp_ctx(struct comp_ctx **comp_ctx) |
| 308 | { |
| 309 | #ifdef USE_ZLIB |
| 310 | z_stream *strm; |
| 311 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 312 | 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] | 313 | return -1; |
| 314 | #endif |
| 315 | |
| 316 | if (unlikely(pool_comp_ctx == NULL)) |
| 317 | pool_comp_ctx = create_pool("comp_ctx", sizeof(struct comp_ctx), MEM_F_SHARED); |
| 318 | |
| 319 | *comp_ctx = pool_alloc2(pool_comp_ctx); |
| 320 | if (*comp_ctx == NULL) |
| 321 | return -1; |
| 322 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 323 | zlib_used_memory += sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 324 | |
| 325 | strm = &(*comp_ctx)->strm; |
| 326 | strm->zalloc = alloc_zlib; |
| 327 | strm->zfree = free_zlib; |
| 328 | strm->opaque = *comp_ctx; |
| 329 | #endif |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Dealloc the comp_ctx |
| 335 | */ |
| 336 | static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx) |
| 337 | { |
| 338 | if (!*comp_ctx) |
| 339 | return 0; |
| 340 | |
| 341 | pool_free2(pool_comp_ctx, *comp_ctx); |
| 342 | *comp_ctx = NULL; |
| 343 | |
| 344 | #ifdef USE_ZLIB |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 345 | zlib_used_memory -= sizeof(struct comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 346 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 350 | |
| 351 | /**************************** |
| 352 | **** Identity algorithm **** |
| 353 | ****************************/ |
| 354 | |
| 355 | /* |
| 356 | * Init the identity algorithm |
| 357 | */ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 358 | int identity_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 359 | { |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Process data |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 365 | * Return size of consumed data or -1 on error |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 366 | */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 367 | 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] | 368 | { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 369 | char *out_data = bi_end(out); |
| 370 | int out_len = out->size - buffer_len(out); |
| 371 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 372 | if (out_len < in_len) |
| 373 | return -1; |
| 374 | |
| 375 | memcpy(out_data, in_data, in_len); |
| 376 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 377 | out->i += in_len; |
| 378 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 379 | return in_len; |
| 380 | } |
| 381 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 382 | int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 383 | { |
| 384 | return 0; |
| 385 | } |
| 386 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 387 | int identity_reset(struct comp_ctx *comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 388 | { |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Deinit the algorithm |
| 394 | */ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 395 | int identity_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 396 | { |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | #ifdef USE_ZLIB |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 402 | /* |
| 403 | * This is a tricky allocation function using the zlib. |
| 404 | * This is based on the allocation order in deflateInit2. |
| 405 | */ |
| 406 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size) |
| 407 | { |
| 408 | struct comp_ctx *ctx = opaque; |
| 409 | static char round = 0; /* order in deflateInit2 */ |
| 410 | void *buf = NULL; |
| 411 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 412 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size)) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 413 | goto end; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 414 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 415 | switch (round) { |
| 416 | case 0: |
| 417 | if (zlib_pool_deflate_state == NULL) |
| 418 | zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED); |
| 419 | ctx->zlib_deflate_state = buf = pool_alloc2(zlib_pool_deflate_state); |
| 420 | break; |
| 421 | |
| 422 | case 1: |
| 423 | if (zlib_pool_window == NULL) |
| 424 | zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED); |
| 425 | ctx->zlib_window = buf = pool_alloc2(zlib_pool_window); |
| 426 | break; |
| 427 | |
| 428 | case 2: |
| 429 | if (zlib_pool_prev == NULL) |
| 430 | zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED); |
| 431 | ctx->zlib_prev = buf = pool_alloc2(zlib_pool_prev); |
| 432 | break; |
| 433 | |
| 434 | case 3: |
| 435 | if (zlib_pool_head == NULL) |
| 436 | zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED); |
| 437 | ctx->zlib_head = buf = pool_alloc2(zlib_pool_head); |
| 438 | break; |
| 439 | |
| 440 | case 4: |
| 441 | if (zlib_pool_pending_buf == NULL) |
| 442 | zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED); |
| 443 | ctx->zlib_pending_buf = buf = pool_alloc2(zlib_pool_pending_buf); |
| 444 | break; |
| 445 | } |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 446 | if (buf != NULL) |
| 447 | zlib_used_memory += items * size; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 448 | |
| 449 | end: |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 450 | |
Willy Tarreau | 4690985 | 2012-11-15 14:57:56 +0100 | [diff] [blame] | 451 | /* deflateInit2() first allocates and checks the deflate_state, then if |
| 452 | * it succeeds, it allocates all other 4 areas at ones and checks them |
| 453 | * at the end. So we want to correctly count the rounds depending on when |
| 454 | * zlib is supposed to abort. |
| 455 | */ |
| 456 | if (buf || round) |
| 457 | round = (round + 1) % 5; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 458 | return buf; |
| 459 | } |
| 460 | |
| 461 | static void free_zlib(void *opaque, void *ptr) |
| 462 | { |
| 463 | struct comp_ctx *ctx = opaque; |
Willy Tarreau | b1fbd05 | 2012-11-10 17:49:37 +0100 | [diff] [blame] | 464 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 465 | |
| 466 | if (ptr == ctx->zlib_window) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 467 | pool = zlib_pool_window; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 468 | else if (ptr == ctx->zlib_deflate_state) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 469 | pool = zlib_pool_deflate_state; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 470 | else if (ptr == ctx->zlib_prev) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 471 | pool = zlib_pool_prev; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 472 | else if (ptr == ctx->zlib_head) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 473 | pool = zlib_pool_head; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 474 | else if (ptr == ctx->zlib_pending_buf) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 475 | pool = zlib_pool_pending_buf; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 476 | |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 477 | pool_free2(pool, ptr); |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 478 | zlib_used_memory -= pool->size; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 479 | } |
| 480 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 481 | /************************** |
| 482 | **** gzip algorithm **** |
| 483 | ***************************/ |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 484 | int gzip_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 485 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 486 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 487 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 488 | if (init_comp_ctx(comp_ctx) < 0) |
| 489 | return -1; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 490 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 491 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 492 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 493 | if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize + 16, global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
| 494 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 495 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | /************************** |
| 503 | **** Deflate algorithm **** |
| 504 | ***************************/ |
| 505 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 506 | int deflate_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 507 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 508 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 509 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 510 | if (init_comp_ctx(comp_ctx) < 0) |
| 511 | return -1; |
| 512 | |
| 513 | strm = &(*comp_ctx)->strm; |
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 | if (deflateInit(strm, level) != Z_OK) { |
| 516 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 517 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 525 | /* Return the size of consumed data or -1 */ |
| 526 | 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] | 527 | { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 528 | int ret; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 529 | z_stream *strm = &comp_ctx->strm; |
| 530 | char *out_data = bi_end(out); |
| 531 | int out_len = out->size - buffer_len(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 532 | |
| 533 | if (in_len <= 0) |
| 534 | return 0; |
| 535 | |
| 536 | |
| 537 | if (out_len <= 0) |
| 538 | return -1; |
| 539 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 540 | strm->next_in = (unsigned char *)in_data; |
| 541 | strm->avail_in = in_len; |
| 542 | strm->next_out = (unsigned char *)out_data; |
| 543 | strm->avail_out = out_len; |
| 544 | |
| 545 | ret = deflate(strm, Z_NO_FLUSH); |
| 546 | if (ret != Z_OK) |
| 547 | return -1; |
| 548 | |
| 549 | /* deflate update the available data out */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 550 | out->i += out_len - strm->avail_out; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 551 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 552 | return in_len - strm->avail_in; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 553 | } |
| 554 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 555 | int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out, int flag) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 556 | { |
| 557 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 558 | int out_len = 0; |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 559 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 560 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 561 | strm->next_out = (unsigned char *)bi_end(out); |
| 562 | strm->avail_out = out->size - buffer_len(out); |
| 563 | |
| 564 | ret = deflate(strm, flag); |
| 565 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 566 | return -1; |
| 567 | |
| 568 | out_len = (out->size - buffer_len(out)) - strm->avail_out; |
| 569 | out->i += out_len; |
| 570 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 571 | /* compression limit */ |
| 572 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
| 573 | (idle_pct < compress_min_idle)) { /* idle */ |
| 574 | /* decrease level */ |
| 575 | if (comp_ctx->cur_lvl > 0) { |
| 576 | comp_ctx->cur_lvl--; |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 577 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
| 578 | } |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 579 | |
| 580 | } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) { |
| 581 | /* increase level */ |
| 582 | comp_ctx->cur_lvl++ ; |
| 583 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 584 | } |
| 585 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 586 | return out_len; |
| 587 | } |
| 588 | |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 589 | int deflate_reset(struct comp_ctx *comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 590 | { |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 591 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 592 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 593 | if (deflateReset(strm) == Z_OK) |
| 594 | return 0; |
| 595 | return -1; |
| 596 | } |
| 597 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 598 | int deflate_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 599 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 600 | z_stream *strm = &(*comp_ctx)->strm; |
| 601 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 602 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 603 | ret = deflateEnd(strm); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 604 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 605 | deinit_comp_ctx(comp_ctx); |
| 606 | |
| 607 | return ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | #endif /* USE_ZLIB */ |
| 611 | |