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