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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 29 | #include <haproxy/api.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 30 | #include <haproxy/dynbuf.h> |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 31 | #include <common/cfgparse.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 32 | #include <haproxy/thread.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 33 | #include <haproxy/pool.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 35 | #include <haproxy/global.h> |
Willy Tarreau | 0a3bd39 | 2020-06-04 08:52:38 +0200 | [diff] [blame] | 36 | #include <haproxy/compression-t.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 0a3bd39 | 2020-06-04 08:52:38 +0200 | [diff] [blame] | 38 | #include <haproxy/compression.h> |
Willy Tarreau | 6634794 | 2020-06-01 12:18:08 +0200 | [diff] [blame] | 39 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 40 | #include <proto/stream.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 41 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 42 | |
Willy Tarreau | e548974 | 2018-11-26 14:44:03 +0100 | [diff] [blame] | 43 | #if defined(USE_ZLIB) |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 44 | __decl_spinlock(comp_pool_lock); |
Emeric Brun | 11f5886 | 2017-11-07 11:57:54 +0100 | [diff] [blame] | 45 | #endif |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 46 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 47 | #ifdef USE_ZLIB |
| 48 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 49 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size); |
| 50 | static void free_zlib(void *opaque, void *ptr); |
| 51 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 52 | /* zlib allocation */ |
| 53 | static struct pool_head *zlib_pool_deflate_state = NULL; |
| 54 | static struct pool_head *zlib_pool_window = NULL; |
| 55 | static struct pool_head *zlib_pool_prev = NULL; |
| 56 | static struct pool_head *zlib_pool_head = NULL; |
| 57 | static struct pool_head *zlib_pool_pending_buf = NULL; |
| 58 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 59 | long zlib_used_memory = 0; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 60 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 61 | static int global_tune_zlibmemlevel = 8; /* zlib memlevel */ |
| 62 | static int global_tune_zlibwindowsize = MAX_WBITS; /* zlib window size */ |
| 63 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 66 | unsigned int compress_min_idle = 0; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 67 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 68 | static int identity_init(struct comp_ctx **comp_ctx, int level); |
| 69 | 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] | 70 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 71 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 72 | static int identity_end(struct comp_ctx **comp_ctx); |
| 73 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 74 | #if defined(USE_SLZ) |
| 75 | |
| 76 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level); |
| 77 | static int rfc1951_init(struct comp_ctx **comp_ctx, int level); |
| 78 | static int rfc1952_init(struct comp_ctx **comp_ctx, int level); |
| 79 | static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out); |
| 80 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 81 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
| 82 | static int rfc195x_end(struct comp_ctx **comp_ctx); |
| 83 | |
| 84 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 85 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 86 | static int gzip_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 87 | static int raw_def_init(struct comp_ctx **comp_ctx, int level); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 88 | static int deflate_init(struct comp_ctx **comp_ctx, int level); |
| 89 | 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] | 90 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out); |
| 91 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out); |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 92 | static int deflate_end(struct comp_ctx **comp_ctx); |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 93 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 94 | #endif /* USE_ZLIB */ |
| 95 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 96 | |
Cyril Bonté | 6162c43 | 2012-11-10 19:27:47 +0100 | [diff] [blame] | 97 | const struct comp_algo comp_algos[] = |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 98 | { |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 99 | { "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] | 100 | #if defined(USE_SLZ) |
| 101 | { "deflate", 7, "deflate", 7, rfc1950_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 102 | { "raw-deflate", 11, "deflate", 7, rfc1951_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 103 | { "gzip", 4, "gzip", 4, rfc1952_init, rfc195x_add_data, rfc195x_flush, rfc195x_finish, rfc195x_end }, |
| 104 | #elif defined(USE_ZLIB) |
Willy Tarreau | 7b21877 | 2015-03-28 22:08:25 +0100 | [diff] [blame] | 105 | { "deflate", 7, "deflate", 7, deflate_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 106 | { "raw-deflate", 11, "deflate", 7, raw_def_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
| 107 | { "gzip", 4, "gzip", 4, gzip_init, deflate_add_data, deflate_flush, deflate_finish, deflate_end }, |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 108 | #endif /* USE_ZLIB */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 109 | { NULL, 0, NULL, 0, NULL , NULL, NULL, NULL, NULL } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /* |
| 113 | * Add a content-type in the configuration |
| 114 | */ |
| 115 | int comp_append_type(struct comp *comp, const char *type) |
| 116 | { |
| 117 | struct comp_type *comp_type; |
| 118 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 119 | comp_type = calloc(1, sizeof(*comp_type)); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 120 | comp_type->name_len = strlen(type); |
| 121 | comp_type->name = strdup(type); |
| 122 | comp_type->next = comp->types; |
| 123 | comp->types = comp_type; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Add an algorithm in the configuration |
| 129 | */ |
| 130 | int comp_append_algo(struct comp *comp, const char *algo) |
| 131 | { |
| 132 | struct comp_algo *comp_algo; |
| 133 | int i; |
| 134 | |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 135 | for (i = 0; comp_algos[i].cfg_name; i++) { |
| 136 | if (!strcmp(algo, comp_algos[i].cfg_name)) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 137 | comp_algo = calloc(1, sizeof(*comp_algo)); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 138 | memmove(comp_algo, &comp_algos[i], sizeof(struct comp_algo)); |
| 139 | comp_algo->next = comp->algos; |
| 140 | comp->algos = comp_algo; |
| 141 | return 0; |
| 142 | } |
| 143 | } |
| 144 | return -1; |
| 145 | } |
| 146 | |
Willy Tarreau | e1cc4b5 | 2016-08-10 21:17:06 +0200 | [diff] [blame] | 147 | #if defined(USE_ZLIB) || defined(USE_SLZ) |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 148 | DECLARE_STATIC_POOL(pool_comp_ctx, "comp_ctx", sizeof(struct comp_ctx)); |
| 149 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 150 | /* |
| 151 | * Alloc the comp_ctx |
| 152 | */ |
| 153 | static inline int init_comp_ctx(struct comp_ctx **comp_ctx) |
| 154 | { |
| 155 | #ifdef USE_ZLIB |
| 156 | z_stream *strm; |
| 157 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 158 | 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] | 159 | return -1; |
| 160 | #endif |
| 161 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 162 | *comp_ctx = pool_alloc(pool_comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 163 | if (*comp_ctx == NULL) |
| 164 | return -1; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 165 | #if defined(USE_SLZ) |
| 166 | (*comp_ctx)->direct_ptr = NULL; |
| 167 | (*comp_ctx)->direct_len = 0; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 168 | (*comp_ctx)->queued = BUF_NULL; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 169 | #elif defined(USE_ZLIB) |
Olivier Houchard | 43da343 | 2019-03-08 18:50:27 +0100 | [diff] [blame] | 170 | _HA_ATOMIC_ADD(&zlib_used_memory, sizeof(struct comp_ctx)); |
| 171 | __ha_barrier_atomic_store(); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 172 | |
| 173 | strm = &(*comp_ctx)->strm; |
| 174 | strm->zalloc = alloc_zlib; |
| 175 | strm->zfree = free_zlib; |
| 176 | strm->opaque = *comp_ctx; |
| 177 | #endif |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Dealloc the comp_ctx |
| 183 | */ |
| 184 | static inline int deinit_comp_ctx(struct comp_ctx **comp_ctx) |
| 185 | { |
| 186 | if (!*comp_ctx) |
| 187 | return 0; |
| 188 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 189 | pool_free(pool_comp_ctx, *comp_ctx); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 190 | *comp_ctx = NULL; |
| 191 | |
| 192 | #ifdef USE_ZLIB |
Olivier Houchard | 43da343 | 2019-03-08 18:50:27 +0100 | [diff] [blame] | 193 | _HA_ATOMIC_SUB(&zlib_used_memory, sizeof(struct comp_ctx)); |
| 194 | __ha_barrier_atomic_store(); |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 195 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Willy Tarreau | e1cc4b5 | 2016-08-10 21:17:06 +0200 | [diff] [blame] | 198 | #endif |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 199 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 200 | |
| 201 | /**************************** |
| 202 | **** Identity algorithm **** |
| 203 | ****************************/ |
| 204 | |
| 205 | /* |
| 206 | * Init the identity algorithm |
| 207 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 208 | static int identity_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 209 | { |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Process data |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 215 | * Return size of consumed data or -1 on error |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 216 | */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 217 | 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] | 218 | { |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 219 | char *out_data = b_tail(out); |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 220 | int out_len = b_room(out); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 221 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 222 | if (out_len < in_len) |
| 223 | return -1; |
| 224 | |
| 225 | memcpy(out_data, in_data, in_len); |
| 226 | |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 227 | b_add(out, in_len); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 228 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 229 | return in_len; |
| 230 | } |
| 231 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 232 | static int identity_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 237 | static int identity_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 238 | { |
| 239 | return 0; |
| 240 | } |
| 241 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 242 | /* |
| 243 | * Deinit the algorithm |
| 244 | */ |
| 245 | static int identity_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 246 | { |
| 247 | return 0; |
| 248 | } |
| 249 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 250 | |
| 251 | #ifdef USE_SLZ |
| 252 | |
| 253 | /* SLZ's gzip format (RFC1952). Returns < 0 on error. */ |
| 254 | static int rfc1952_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_rfc1952_init(&(*comp_ctx)->strm, !!level); |
| 261 | } |
| 262 | |
| 263 | /* SLZ's raw deflate format (RFC1951). Returns < 0 on error. */ |
| 264 | static int rfc1951_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_rfc1951_init(&(*comp_ctx)->strm, !!level); |
| 271 | } |
| 272 | |
| 273 | /* SLZ's zlib format (RFC1950). Returns < 0 on error. */ |
| 274 | static int rfc1950_init(struct comp_ctx **comp_ctx, int level) |
| 275 | { |
| 276 | if (init_comp_ctx(comp_ctx) < 0) |
| 277 | return -1; |
| 278 | |
| 279 | (*comp_ctx)->cur_lvl = !!level; |
| 280 | return slz_rfc1950_init(&(*comp_ctx)->strm, !!level); |
| 281 | } |
| 282 | |
| 283 | /* Return the size of consumed data or -1. The output buffer is unused at this |
| 284 | * point, we only keep a reference to the input data or a copy of them if the |
| 285 | * reference is already used. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 286 | */ |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 287 | 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] | 288 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 289 | static THREAD_LOCAL struct buffer tmpbuf = BUF_NULL; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 290 | |
| 291 | if (in_len <= 0) |
| 292 | return 0; |
| 293 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 294 | if (comp_ctx->direct_ptr && b_is_null(&comp_ctx->queued)) { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 295 | /* data already being pointed to, we're in front of fragmented |
| 296 | * data and need a buffer now. We reuse the same buffer, as it's |
| 297 | * not used out of the scope of a series of add_data()*, end(). |
| 298 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 299 | if (unlikely(!tmpbuf.size)) { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 300 | /* this is the first time we need the compression buffer */ |
| 301 | if (b_alloc(&tmpbuf) == NULL) |
| 302 | return -1; /* no memory */ |
| 303 | } |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 304 | b_reset(&tmpbuf); |
| 305 | memcpy(b_tail(&tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len); |
| 306 | b_add(&tmpbuf, comp_ctx->direct_len); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 307 | comp_ctx->direct_ptr = NULL; |
| 308 | comp_ctx->direct_len = 0; |
| 309 | comp_ctx->queued = tmpbuf; |
| 310 | /* fall through buffer copy */ |
| 311 | } |
| 312 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 313 | if (!b_is_null(&comp_ctx->queued)) { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 314 | /* data already pending */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 315 | memcpy(b_tail(&comp_ctx->queued), in_data, in_len); |
| 316 | b_add(&comp_ctx->queued, in_len); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 317 | return in_len; |
| 318 | } |
| 319 | |
| 320 | comp_ctx->direct_ptr = in_data; |
| 321 | comp_ctx->direct_len = in_len; |
| 322 | return in_len; |
| 323 | } |
| 324 | |
| 325 | /* Compresses the data accumulated using add_data(), and optionally sends the |
| 326 | * format-specific trailer if <finish> is non-null. <out> is expected to have a |
| 327 | * large enough free non-wrapping space as verified by http_comp_buffer_init(). |
| 328 | * The number of bytes emitted is reported. |
| 329 | */ |
| 330 | static int rfc195x_flush_or_finish(struct comp_ctx *comp_ctx, struct buffer *out, int finish) |
| 331 | { |
| 332 | struct slz_stream *strm = &comp_ctx->strm; |
| 333 | const char *in_ptr; |
| 334 | int in_len; |
| 335 | int out_len; |
| 336 | |
| 337 | in_ptr = comp_ctx->direct_ptr; |
| 338 | in_len = comp_ctx->direct_len; |
| 339 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 340 | if (!b_is_null(&comp_ctx->queued)) { |
| 341 | in_ptr = b_head(&comp_ctx->queued); |
| 342 | in_len = b_data(&comp_ctx->queued); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 345 | out_len = b_data(out); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 346 | |
| 347 | if (in_ptr) |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 348 | b_add(out, slz_encode(strm, b_tail(out), in_ptr, in_len, !finish)); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 349 | |
| 350 | if (finish) |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 351 | b_add(out, slz_finish(strm, b_tail(out))); |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 352 | |
Olivier Houchard | 0b66284 | 2018-06-29 18:16:31 +0200 | [diff] [blame] | 353 | out_len = b_data(out) - out_len; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 354 | |
| 355 | /* very important, we must wipe the data we've just flushed */ |
| 356 | comp_ctx->direct_len = 0; |
| 357 | comp_ctx->direct_ptr = NULL; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 358 | comp_ctx->queued = BUF_NULL; |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 359 | |
| 360 | /* Verify compression rate limiting and CPU usage */ |
| 361 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
Willy Tarreau | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 362 | (ti->idle_pct < compress_min_idle)) { /* idle */ |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 363 | if (comp_ctx->cur_lvl > 0) |
| 364 | strm->level = --comp_ctx->cur_lvl; |
| 365 | } |
| 366 | else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel && comp_ctx->cur_lvl < 1) { |
| 367 | strm->level = ++comp_ctx->cur_lvl; |
| 368 | } |
| 369 | |
| 370 | /* and that's all */ |
| 371 | return out_len; |
| 372 | } |
| 373 | |
| 374 | static int rfc195x_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 375 | { |
| 376 | return rfc195x_flush_or_finish(comp_ctx, out, 0); |
| 377 | } |
| 378 | |
| 379 | static int rfc195x_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 380 | { |
| 381 | return rfc195x_flush_or_finish(comp_ctx, out, 1); |
| 382 | } |
| 383 | |
| 384 | /* we just need to free the comp_ctx here, nothing was allocated */ |
| 385 | static int rfc195x_end(struct comp_ctx **comp_ctx) |
| 386 | { |
| 387 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 391 | #elif defined(USE_ZLIB) /* ! USE_SLZ */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 392 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 393 | /* |
| 394 | * This is a tricky allocation function using the zlib. |
| 395 | * This is based on the allocation order in deflateInit2. |
| 396 | */ |
| 397 | static void *alloc_zlib(void *opaque, unsigned int items, unsigned int size) |
| 398 | { |
| 399 | struct comp_ctx *ctx = opaque; |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 400 | static THREAD_LOCAL char round = 0; /* order in deflateInit2 */ |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 401 | void *buf = NULL; |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 402 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 403 | |
William Lallemand | e3a7d99 | 2012-11-20 11:25:20 +0100 | [diff] [blame] | 404 | if (global.maxzlibmem > 0 && (global.maxzlibmem - zlib_used_memory) < (long)(items * size)) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 405 | goto end; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 406 | |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 407 | switch (round) { |
| 408 | case 0: |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 409 | if (zlib_pool_deflate_state == NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 410 | HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 411 | if (zlib_pool_deflate_state == NULL) |
| 412 | zlib_pool_deflate_state = create_pool("zlib_state", size * items, MEM_F_SHARED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 413 | HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 414 | } |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 415 | pool = zlib_pool_deflate_state; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 416 | ctx->zlib_deflate_state = buf = pool_alloc(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 417 | break; |
| 418 | |
| 419 | case 1: |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 420 | if (zlib_pool_window == NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 421 | HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 422 | if (zlib_pool_window == NULL) |
| 423 | zlib_pool_window = create_pool("zlib_window", size * items, MEM_F_SHARED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 424 | HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 425 | } |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 426 | pool = zlib_pool_window; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 427 | ctx->zlib_window = buf = pool_alloc(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | case 2: |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 431 | if (zlib_pool_prev == NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 432 | HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 433 | if (zlib_pool_prev == NULL) |
| 434 | zlib_pool_prev = create_pool("zlib_prev", size * items, MEM_F_SHARED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 435 | HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 436 | } |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 437 | pool = zlib_pool_prev; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 438 | ctx->zlib_prev = buf = pool_alloc(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 439 | break; |
| 440 | |
| 441 | case 3: |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 442 | if (zlib_pool_head == NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 443 | HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 444 | if (zlib_pool_head == NULL) |
| 445 | zlib_pool_head = create_pool("zlib_head", size * items, MEM_F_SHARED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 446 | HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 447 | } |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 448 | pool = zlib_pool_head; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 449 | ctx->zlib_head = buf = pool_alloc(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 450 | break; |
| 451 | |
| 452 | case 4: |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 453 | if (zlib_pool_pending_buf == NULL) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 454 | HA_SPIN_LOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 455 | if (zlib_pool_pending_buf == NULL) |
| 456 | zlib_pool_pending_buf = create_pool("zlib_pending_buf", size * items, MEM_F_SHARED); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 457 | HA_SPIN_UNLOCK(COMP_POOL_LOCK, &comp_pool_lock); |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 458 | } |
Willy Tarreau | 4f31fc2 | 2014-12-24 18:07:55 +0100 | [diff] [blame] | 459 | pool = zlib_pool_pending_buf; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 460 | ctx->zlib_pending_buf = buf = pool_alloc(pool); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 461 | break; |
| 462 | } |
Olivier Houchard | 43da343 | 2019-03-08 18:50:27 +0100 | [diff] [blame] | 463 | if (buf != NULL) { |
| 464 | _HA_ATOMIC_ADD(&zlib_used_memory, pool->size); |
| 465 | __ha_barrier_atomic_store(); |
| 466 | } |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 467 | |
| 468 | end: |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 469 | |
Willy Tarreau | 4690985 | 2012-11-15 14:57:56 +0100 | [diff] [blame] | 470 | /* deflateInit2() first allocates and checks the deflate_state, then if |
| 471 | * it succeeds, it allocates all other 4 areas at ones and checks them |
| 472 | * at the end. So we want to correctly count the rounds depending on when |
| 473 | * zlib is supposed to abort. |
| 474 | */ |
| 475 | if (buf || round) |
| 476 | round = (round + 1) % 5; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 477 | return buf; |
| 478 | } |
| 479 | |
| 480 | static void free_zlib(void *opaque, void *ptr) |
| 481 | { |
| 482 | struct comp_ctx *ctx = opaque; |
Willy Tarreau | b1fbd05 | 2012-11-10 17:49:37 +0100 | [diff] [blame] | 483 | struct pool_head *pool = NULL; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 484 | |
| 485 | if (ptr == ctx->zlib_window) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 486 | pool = zlib_pool_window; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 487 | else if (ptr == ctx->zlib_deflate_state) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 488 | pool = zlib_pool_deflate_state; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 489 | else if (ptr == ctx->zlib_prev) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 490 | pool = zlib_pool_prev; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 491 | else if (ptr == ctx->zlib_head) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 492 | pool = zlib_pool_head; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 493 | else if (ptr == ctx->zlib_pending_buf) |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 494 | pool = zlib_pool_pending_buf; |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 495 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 496 | pool_free(pool, ptr); |
Olivier Houchard | 43da343 | 2019-03-08 18:50:27 +0100 | [diff] [blame] | 497 | _HA_ATOMIC_SUB(&zlib_used_memory, pool->size); |
| 498 | __ha_barrier_atomic_store(); |
William Lallemand | 2b50247 | 2012-10-30 14:30:39 +0100 | [diff] [blame] | 499 | } |
| 500 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 501 | /************************** |
| 502 | **** gzip algorithm **** |
| 503 | ***************************/ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 504 | static int gzip_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 505 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 506 | z_stream *strm; |
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 | if (init_comp_ctx(comp_ctx) < 0) |
| 509 | return -1; |
William Lallemand | 9d5f548 | 2012-11-07 16:12:57 +0100 | [diff] [blame] | 510 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 511 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 512 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 513 | if (deflateInit2(strm, level, Z_DEFLATED, global_tune_zlibwindowsize + 16, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 514 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 515 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 519 | |
| 520 | return 0; |
| 521 | } |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 522 | |
| 523 | /* Raw deflate algorithm */ |
| 524 | static int raw_def_init(struct comp_ctx **comp_ctx, int level) |
| 525 | { |
| 526 | z_stream *strm; |
| 527 | |
| 528 | if (init_comp_ctx(comp_ctx) < 0) |
| 529 | return -1; |
| 530 | |
| 531 | strm = &(*comp_ctx)->strm; |
| 532 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 533 | if (deflateInit2(strm, level, Z_DEFLATED, -global_tune_zlibwindowsize, global_tune_zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) { |
Willy Tarreau | c91840a | 2015-03-28 17:00:39 +0100 | [diff] [blame] | 534 | deinit_comp_ctx(comp_ctx); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | (*comp_ctx)->cur_lvl = level; |
| 539 | return 0; |
| 540 | } |
| 541 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 542 | /************************** |
| 543 | **** Deflate algorithm **** |
| 544 | ***************************/ |
| 545 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 546 | static int deflate_init(struct comp_ctx **comp_ctx, int level) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 547 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 548 | z_stream *strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 549 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 550 | if (init_comp_ctx(comp_ctx) < 0) |
| 551 | return -1; |
| 552 | |
| 553 | strm = &(*comp_ctx)->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 554 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 555 | 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] | 556 | deinit_comp_ctx(comp_ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 557 | return -1; |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | (*comp_ctx)->cur_lvl = level; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 565 | /* Return the size of consumed data or -1 */ |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 566 | 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] | 567 | { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 568 | int ret; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 569 | z_stream *strm = &comp_ctx->strm; |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 570 | char *out_data = b_tail(out); |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 571 | int out_len = b_room(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 572 | |
| 573 | if (in_len <= 0) |
| 574 | return 0; |
| 575 | |
| 576 | |
| 577 | if (out_len <= 0) |
| 578 | return -1; |
| 579 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 580 | strm->next_in = (unsigned char *)in_data; |
| 581 | strm->avail_in = in_len; |
| 582 | strm->next_out = (unsigned char *)out_data; |
| 583 | strm->avail_out = out_len; |
| 584 | |
| 585 | ret = deflate(strm, Z_NO_FLUSH); |
| 586 | if (ret != Z_OK) |
| 587 | return -1; |
| 588 | |
| 589 | /* deflate update the available data out */ |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 590 | b_add(out, out_len - strm->avail_out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 591 | |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 592 | return in_len - strm->avail_in; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 595 | 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] | 596 | { |
| 597 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 598 | int out_len = 0; |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 599 | z_stream *strm = &comp_ctx->strm; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 600 | |
Willy Tarreau | d8b8b53 | 2016-08-08 16:41:01 +0200 | [diff] [blame] | 601 | strm->next_in = NULL; |
| 602 | strm->avail_in = 0; |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 603 | strm->next_out = (unsigned char *)b_tail(out); |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 604 | strm->avail_out = b_room(out); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 605 | |
| 606 | ret = deflate(strm, flag); |
| 607 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 608 | return -1; |
| 609 | |
Willy Tarreau | eac5259 | 2018-06-15 13:59:36 +0200 | [diff] [blame] | 610 | out_len = b_room(out) - strm->avail_out; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 611 | b_add(out, out_len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 612 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 613 | /* compression limit */ |
| 614 | if ((global.comp_rate_lim > 0 && (read_freq_ctr(&global.comp_bps_out) > global.comp_rate_lim)) || /* rate */ |
Willy Tarreau | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 615 | (ti->idle_pct < compress_min_idle)) { /* idle */ |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 616 | /* decrease level */ |
| 617 | if (comp_ctx->cur_lvl > 0) { |
| 618 | comp_ctx->cur_lvl--; |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 619 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
| 620 | } |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 621 | |
| 622 | } else if (comp_ctx->cur_lvl < global.tune.comp_maxlevel) { |
| 623 | /* increase level */ |
| 624 | comp_ctx->cur_lvl++ ; |
| 625 | deflateParams(&comp_ctx->strm, comp_ctx->cur_lvl, Z_DEFAULT_STRATEGY); |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 626 | } |
| 627 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 628 | return out_len; |
| 629 | } |
| 630 | |
Willy Tarreau | 9787efa | 2015-03-28 19:17:31 +0100 | [diff] [blame] | 631 | static int deflate_flush(struct comp_ctx *comp_ctx, struct buffer *out) |
| 632 | { |
| 633 | return deflate_flush_or_finish(comp_ctx, out, Z_SYNC_FLUSH); |
| 634 | } |
| 635 | |
| 636 | static int deflate_finish(struct comp_ctx *comp_ctx, struct buffer *out) |
| 637 | { |
| 638 | return deflate_flush_or_finish(comp_ctx, out, Z_FINISH); |
| 639 | } |
| 640 | |
Willy Tarreau | 9f640a1 | 2015-03-28 15:46:00 +0100 | [diff] [blame] | 641 | static int deflate_end(struct comp_ctx **comp_ctx) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 642 | { |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 643 | z_stream *strm = &(*comp_ctx)->strm; |
| 644 | int ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 645 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 646 | ret = deflateEnd(strm); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 647 | |
William Lallemand | 8b52bb3 | 2012-11-16 18:06:41 +0100 | [diff] [blame] | 648 | deinit_comp_ctx(comp_ctx); |
| 649 | |
| 650 | return ret; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 653 | /* config parser for global "tune.zlibmemlevel" */ |
| 654 | static int zlib_parse_global_memlevel(char **args, int section_type, struct proxy *curpx, |
| 655 | struct proxy *defpx, const char *file, int line, |
| 656 | char **err) |
| 657 | { |
| 658 | if (too_many_args(1, args, err, NULL)) |
| 659 | return -1; |
| 660 | |
| 661 | if (*(args[1]) == 0) { |
| 662 | memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | global_tune_zlibmemlevel = atoi(args[1]); |
| 667 | if (global_tune_zlibmemlevel < 1 || global_tune_zlibmemlevel > 9) { |
| 668 | memprintf(err, "'%s' expects a numeric value between 1 and 9.", args[0]); |
| 669 | return -1; |
| 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | /* config parser for global "tune.zlibwindowsize" */ |
| 676 | static int zlib_parse_global_windowsize(char **args, int section_type, struct proxy *curpx, |
| 677 | struct proxy *defpx, const char *file, int line, |
| 678 | char **err) |
| 679 | { |
| 680 | if (too_many_args(1, args, err, NULL)) |
| 681 | return -1; |
| 682 | |
| 683 | if (*(args[1]) == 0) { |
| 684 | memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | global_tune_zlibwindowsize = atoi(args[1]); |
| 689 | if (global_tune_zlibwindowsize < 8 || global_tune_zlibwindowsize > 15) { |
| 690 | memprintf(err, "'%s' expects a numeric value between 8 and 15.", args[0]); |
| 691 | return -1; |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 696 | #endif /* USE_ZLIB */ |
| 697 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 698 | |
| 699 | /* config keyword parsers */ |
| 700 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 701 | #ifdef USE_ZLIB |
| 702 | { CFG_GLOBAL, "tune.zlib.memlevel", zlib_parse_global_memlevel }, |
| 703 | { CFG_GLOBAL, "tune.zlib.windowsize", zlib_parse_global_windowsize }, |
| 704 | #endif |
| 705 | { 0, NULL, NULL } |
| 706 | }}; |
| 707 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 708 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 709 | |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 710 | __attribute__((constructor)) |
| 711 | static void __comp_fetch_init(void) |
| 712 | { |
Willy Tarreau | 418b8c0 | 2015-03-29 03:32:06 +0200 | [diff] [blame] | 713 | #ifdef USE_SLZ |
| 714 | slz_make_crc_table(); |
| 715 | slz_prepare_dist_table(); |
| 716 | #endif |
Willy Tarreau | e548974 | 2018-11-26 14:44:03 +0100 | [diff] [blame] | 717 | |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 718 | #if defined(USE_ZLIB) && defined(DEFAULT_MAXZLIBMEM) |
Willy Tarreau | 3bfcd10 | 2018-11-26 10:24:45 +0100 | [diff] [blame] | 719 | global.maxzlibmem = DEFAULT_MAXZLIBMEM * 1024U * 1024U; |
Willy Tarreau | 3687803 | 2016-12-22 19:46:17 +0100 | [diff] [blame] | 720 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static void comp_register_build_opts(void) |
| 724 | { |
| 725 | char *ptr = NULL; |
| 726 | int i; |
| 727 | |
Willy Tarreau | b97c6fb | 2016-12-21 19:30:30 +0100 | [diff] [blame] | 728 | #ifdef USE_ZLIB |
| 729 | memprintf(&ptr, "Built with zlib version : " ZLIB_VERSION); |
| 730 | memprintf(&ptr, "%s\nRunning on zlib version : %s", ptr, zlibVersion()); |
| 731 | #elif defined(USE_SLZ) |
| 732 | memprintf(&ptr, "Built with libslz for stateless compression."); |
Lukas Tribus | b732321 | 2017-01-11 14:24:35 +0000 | [diff] [blame] | 733 | #else |
| 734 | memprintf(&ptr, "Built without compression support (neither USE_ZLIB nor USE_SLZ are set)."); |
Willy Tarreau | b97c6fb | 2016-12-21 19:30:30 +0100 | [diff] [blame] | 735 | #endif |
| 736 | memprintf(&ptr, "%s\nCompression algorithms supported :", ptr); |
| 737 | |
| 738 | for (i = 0; comp_algos[i].cfg_name; i++) |
| 739 | memprintf(&ptr, "%s%s %s(\"%s\")", ptr, (i == 0 ? "" : ","), comp_algos[i].cfg_name, comp_algos[i].ua_name); |
| 740 | |
| 741 | if (i == 0) |
| 742 | memprintf(&ptr, "%s none", ptr); |
| 743 | |
| 744 | hap_register_build_opts(ptr, 1); |
William Lallemand | 727db8b | 2013-04-20 17:33:20 +0200 | [diff] [blame] | 745 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 746 | |
| 747 | INITCALL0(STG_REGISTER, comp_register_build_opts); |