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