William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cache management |
| 3 | * |
| 4 | * Copyright 2017 HAProxy Technologies |
| 5 | * William Lallemand <wlallemand@haproxy.com> |
| 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 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 13 | #include <eb32tree.h> |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 14 | #include <import/sha1.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 15 | |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 16 | #include <types/action.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 17 | #include <types/cli.h> |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 18 | #include <types/filters.h> |
| 19 | #include <types/proxy.h> |
| 20 | #include <types/shctx.h> |
| 21 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 22 | #include <proto/channel.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 23 | #include <proto/cli.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 24 | #include <proto/proxy.h> |
| 25 | #include <proto/hdr_idx.h> |
| 26 | #include <proto/filters.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 27 | #include <proto/http_rules.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/log.h> |
| 30 | #include <proto/stream.h> |
| 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/shctx.h> |
| 33 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 34 | |
| 35 | #include <common/cfgparse.h> |
| 36 | #include <common/hash.h> |
| 37 | |
| 38 | /* flt_cache_store */ |
| 39 | |
| 40 | static const char *cache_store_flt_id = "cache store filter"; |
| 41 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 42 | static struct pool_head *pool_head_cache_st = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 43 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 44 | struct applet http_cache_applet; |
| 45 | |
| 46 | struct flt_ops cache_ops; |
| 47 | |
| 48 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 49 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 50 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 51 | unsigned int maxage; /* max-age */ |
| 52 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 53 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 54 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * cache ctx for filters |
| 59 | */ |
| 60 | struct cache_st { |
| 61 | int hdrs_len; |
| 62 | struct shared_block *first_block; |
| 63 | }; |
| 64 | |
| 65 | struct cache_entry { |
| 66 | unsigned int latest_validation; /* latest validation date */ |
| 67 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 68 | unsigned int age; /* Origin server "Age" header value */ |
| 69 | unsigned int eoh; /* Origin server end of headers offset. */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 70 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 71 | char hash[20]; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 72 | unsigned char data[0]; |
| 73 | }; |
| 74 | |
| 75 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 76 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 77 | |
| 78 | static struct list caches = LIST_HEAD_INIT(caches); |
| 79 | static struct cache *tmp_cache_config = NULL; |
| 80 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 81 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 82 | { |
| 83 | struct eb32_node *node; |
| 84 | struct cache_entry *entry; |
| 85 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 86 | node = eb32_lookup(&cache->entries, (*(unsigned int *)hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 87 | if (!node) |
| 88 | return NULL; |
| 89 | |
| 90 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 91 | |
| 92 | /* if that's not the right node */ |
| 93 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 94 | return NULL; |
| 95 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 96 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 97 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 98 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 99 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 100 | entry->eb.key = 0; |
| 101 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 102 | return NULL; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 107 | { |
| 108 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 109 | } |
| 110 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 111 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 112 | { |
| 113 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 118 | static int |
| 119 | cache_store_init(struct proxy *px, struct flt_conf *f1conf) |
| 120 | { |
| 121 | return 0; |
| 122 | } |
| 123 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 124 | static int |
| 125 | cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 126 | { |
| 127 | if (!(chn->flags & CF_ISRESP)) |
| 128 | return 1; |
| 129 | |
| 130 | if (filter->ctx == NULL) { |
| 131 | struct cache_st *st; |
| 132 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 133 | st = pool_alloc_dirty(pool_head_cache_st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 134 | if (st == NULL) |
| 135 | return -1; |
| 136 | |
| 137 | st->hdrs_len = 0; |
| 138 | st->first_block = NULL; |
| 139 | filter->ctx = st; |
| 140 | } |
| 141 | |
| 142 | register_data_filter(s, chn, filter); |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static int |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 148 | cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 149 | { |
| 150 | struct cache_st *st = filter->ctx; |
| 151 | struct cache *cache = filter->config->conf; |
| 152 | struct shared_context *shctx = shctx_ptr(cache); |
| 153 | |
| 154 | if (!(chn->flags & CF_ISRESP)) |
| 155 | return 1; |
| 156 | |
| 157 | /* Everything should be released in the http_end filter, but we need to do it |
| 158 | * there too, in case of errors */ |
| 159 | |
| 160 | if (st && st->first_block) { |
| 161 | |
| 162 | shctx_lock(shctx); |
| 163 | shctx_row_dec_hot(shctx, st->first_block); |
| 164 | shctx_unlock(shctx); |
| 165 | |
| 166 | } |
| 167 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 168 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 169 | filter->ctx = NULL; |
| 170 | } |
| 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 177 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 178 | { |
| 179 | struct cache_st *st = filter->ctx; |
| 180 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 181 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 182 | return 1; |
| 183 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 184 | st->hdrs_len = msg->sov; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 185 | |
| 186 | return 1; |
| 187 | } |
| 188 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 189 | static inline void disable_cache_entry(struct cache_st *st, |
| 190 | struct filter *filter, struct shared_context *shctx) |
| 191 | { |
| 192 | struct cache_entry *object; |
| 193 | |
| 194 | object = (struct cache_entry *)st->first_block->data; |
| 195 | filter->ctx = NULL; /* disable cache */ |
| 196 | shctx_lock(shctx); |
| 197 | shctx_row_dec_hot(shctx, st->first_block); |
| 198 | object->eb.key = 0; |
| 199 | shctx_unlock(shctx); |
| 200 | pool_free(pool_head_cache_st, st); |
| 201 | } |
| 202 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 203 | static int |
| 204 | cache_store_http_forward_data(struct stream *s, struct filter *filter, |
| 205 | struct http_msg *msg, unsigned int len) |
| 206 | { |
| 207 | struct cache_st *st = filter->ctx; |
| 208 | struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf); |
| 209 | int ret; |
| 210 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 211 | ret = 0; |
| 212 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 213 | /* |
| 214 | * We need to skip the HTTP headers first, because we saved them in the |
| 215 | * http-response action. |
| 216 | */ |
| 217 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 218 | return len; |
| 219 | |
| 220 | if (!len) { |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 221 | /* Nothing to forward */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 222 | ret = len; |
| 223 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 224 | else if (st->hdrs_len >= len) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 225 | /* Forward part of headers */ |
| 226 | ret = len; |
| 227 | st->hdrs_len -= len; |
| 228 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 229 | else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 230 | /* Forward data */ |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 231 | if (filter->ctx && st->first_block) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 232 | int to_append, append; |
| 233 | struct shared_block *fb; |
| 234 | |
| 235 | to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len); |
| 236 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 237 | shctx_lock(shctx); |
| 238 | fb = shctx_row_reserve_hot(shctx, st->first_block, to_append); |
| 239 | if (!fb) { |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 240 | shctx_unlock(shctx); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 241 | disable_cache_entry(st, filter, shctx); |
| 242 | return len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 243 | } |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 244 | shctx_unlock(shctx); |
| 245 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 246 | /* Skip remaining headers to fill the cache */ |
| 247 | c_adv(msg->chn, st->hdrs_len); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 248 | append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 249 | (unsigned char *)ci_head(msg->chn), to_append); |
| 250 | ret = st->hdrs_len + to_append - append; |
| 251 | /* Rewind the buffer to forward all data */ |
| 252 | c_rew(msg->chn, st->hdrs_len); |
| 253 | st->hdrs_len = 0; |
| 254 | if (ret < 0) |
| 255 | disable_cache_entry(st, filter, shctx); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 256 | } |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 257 | else |
| 258 | ret = len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if ((ret != len) || |
| 262 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 263 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 270 | struct http_msg *msg) |
| 271 | { |
| 272 | struct cache_st *st = filter->ctx; |
| 273 | struct cache *cache = filter->config->conf; |
| 274 | struct shared_context *shctx = shctx_ptr(cache); |
| 275 | struct cache_entry *object; |
| 276 | |
| 277 | if (!(msg->chn->flags & CF_ISRESP)) |
| 278 | return 1; |
| 279 | |
| 280 | if (st && st->first_block) { |
| 281 | |
| 282 | object = (struct cache_entry *)st->first_block->data; |
| 283 | |
| 284 | /* does not need to test if the insertion worked, if it |
| 285 | * doesn't, the blocks will be reused anyway */ |
| 286 | |
| 287 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 288 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 289 | object->eb.key = 0; |
| 290 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 291 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 292 | shctx_row_dec_hot(shctx, st->first_block); |
| 293 | shctx_unlock(shctx); |
| 294 | |
| 295 | } |
| 296 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 297 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 298 | filter->ctx = NULL; |
| 299 | } |
| 300 | |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * This intends to be used when checking HTTP headers for some |
| 306 | * word=value directive. Return a pointer to the first character of value, if |
| 307 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 308 | */ |
| 309 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 310 | { |
| 311 | int st = 0; |
| 312 | |
| 313 | if (slen < wlen) |
| 314 | return 0; |
| 315 | |
| 316 | while (wlen) { |
| 317 | char c = *sample ^ *word; |
| 318 | if (c && c != ('A' ^ 'a')) |
| 319 | return NULL; |
| 320 | sample++; |
| 321 | word++; |
| 322 | slen--; |
| 323 | wlen--; |
| 324 | } |
| 325 | |
| 326 | while (slen) { |
| 327 | if (st == 0) { |
| 328 | if (*sample != '=') |
| 329 | return NULL; |
| 330 | sample++; |
| 331 | slen--; |
| 332 | st = 1; |
| 333 | continue; |
| 334 | } else { |
| 335 | return (char *)sample; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Return the maxage in seconds of an HTTP response. |
| 344 | * Compute the maxage using either: |
| 345 | * - the assigned max-age of the cache |
| 346 | * - the s-maxage directive |
| 347 | * - the max-age directive |
| 348 | * - (Expires - Data) headers |
| 349 | * - the default-max-age of the cache |
| 350 | * |
| 351 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 352 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 353 | { |
| 354 | struct http_txn *txn = s->txn; |
| 355 | struct hdr_ctx ctx; |
| 356 | |
| 357 | int smaxage = -1; |
| 358 | int maxage = -1; |
| 359 | |
| 360 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 361 | ctx.idx = 0; |
| 362 | |
| 363 | /* loop on the Cache-Control values */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 364 | while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 365 | char *directive = ctx.line + ctx.val; |
| 366 | char *value; |
| 367 | |
| 368 | value = directive_value(directive, ctx.vlen, "s-maxage", 8); |
| 369 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 370 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 371 | |
| 372 | chunk_strncat(chk, value, ctx.vlen - 8 + 1); |
| 373 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 374 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); |
| 378 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 379 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 380 | |
| 381 | chunk_strncat(chk, value, ctx.vlen - 7 + 1); |
| 382 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 383 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | /* TODO: Expires - Data */ |
| 388 | |
| 389 | |
| 390 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 391 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 392 | |
| 393 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 394 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 395 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 396 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 397 | |
| 398 | } |
| 399 | |
| 400 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 401 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 402 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 403 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 404 | |
| 405 | if (first == block && object->eb.key) |
| 406 | eb32_delete(&object->eb); |
| 407 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 408 | } |
| 409 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 410 | /* |
| 411 | * This fonction will store the headers of the response in a buffer and then |
| 412 | * register a filter to store the data |
| 413 | */ |
| 414 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
| 415 | struct session *sess, struct stream *s, int flags) |
| 416 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 417 | unsigned int age; |
| 418 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 419 | struct http_txn *txn = s->txn; |
| 420 | struct http_msg *msg = &txn->rsp; |
| 421 | struct filter *filter; |
| 422 | struct hdr_ctx ctx; |
| 423 | struct shared_block *first = NULL; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 424 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 425 | struct shared_context *shctx = shctx_ptr(cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 426 | struct cache_entry *object; |
| 427 | |
| 428 | |
| 429 | /* Don't cache if the response came from a cache */ |
| 430 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 431 | s->target == &http_cache_applet.obj_type) { |
| 432 | goto out; |
| 433 | } |
| 434 | |
| 435 | /* cache only HTTP/1.1 */ |
| 436 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 437 | goto out; |
| 438 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 439 | /* Do not cache too big objects. */ |
| 440 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 441 | msg->sov + msg->body_len > shctx->max_obj_size) |
| 442 | goto out; |
| 443 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 444 | /* cache only GET method */ |
| 445 | if (txn->meth != HTTP_METH_GET) |
| 446 | goto out; |
| 447 | |
| 448 | /* cache only 200 status code */ |
| 449 | if (txn->status != 200) |
| 450 | goto out; |
| 451 | |
| 452 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 453 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 454 | if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 455 | goto out; |
| 456 | |
Willy Tarreau | faf2909 | 2017-12-21 15:59:17 +0100 | [diff] [blame] | 457 | check_response_for_cacheability(s, &s->res); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 458 | |
Willy Tarreau | d4569d1 | 2017-12-22 18:03:04 +0100 | [diff] [blame] | 459 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 460 | goto out; |
| 461 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 462 | age = 0; |
| 463 | ctx.idx = 0; |
| 464 | if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
| 465 | if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) { |
| 466 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 467 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 468 | age = hdr_age; |
| 469 | } |
| 470 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 471 | } |
| 472 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 473 | shctx_lock(shctx); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 474 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 475 | if (!first) { |
| 476 | shctx_unlock(shctx); |
| 477 | goto out; |
| 478 | } |
| 479 | shctx_unlock(shctx); |
| 480 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 481 | /* the received memory is not initialized, we need at least to mark |
| 482 | * the object as not indexed yet. |
| 483 | */ |
| 484 | object = (struct cache_entry *)first->data; |
| 485 | object->eb.node.leaf_p = NULL; |
| 486 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 487 | object->age = age; |
| 488 | object->eoh = msg->eoh; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 489 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 490 | /* reserve space for the cache_entry structure */ |
| 491 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 492 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 493 | /* cache the headers in a http action because it allows to chose what |
| 494 | * to cache, for example you might want to cache a response before |
| 495 | * modifying some HTTP headers, or on the contrary after modifying |
| 496 | * those headers. |
| 497 | */ |
| 498 | |
| 499 | /* does not need to be locked because it's in the "hot" list, |
| 500 | * copy the headers */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 501 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 502 | goto out; |
| 503 | |
| 504 | /* register the buffer in the filter ctx for filling it with data*/ |
| 505 | if (!LIST_ISEMPTY(&s->strm_flt.filters)) { |
| 506 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 507 | if (filter->config->id == cache_store_flt_id && |
| 508 | filter->config->conf == rule->arg.act.p[0]) { |
| 509 | if (filter->ctx) { |
| 510 | struct cache_st *cache_ctx = filter->ctx; |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 511 | struct cache_entry *old; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 512 | |
| 513 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 514 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 515 | object->eb.key = (*(unsigned int *)&txn->cache_hash); |
| 516 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 517 | /* Insert the node later on caching success */ |
| 518 | |
| 519 | shctx_lock(shctx); |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 520 | |
| 521 | old = entry_exist(cache, txn->cache_hash); |
| 522 | if (old) { |
| 523 | eb32_delete(&old->eb); |
| 524 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 525 | } |
| 526 | shctx_unlock(shctx); |
| 527 | |
| 528 | /* store latest value and expiration time */ |
| 529 | object->latest_validation = now.tv_sec; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 530 | object->expire = now.tv_sec + http_calc_maxage(s, cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 531 | } |
| 532 | return ACT_RET_CONT; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | out: |
| 538 | /* if does not cache */ |
| 539 | if (first) { |
| 540 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 541 | first->len = 0; |
| 542 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 543 | shctx_row_dec_hot(shctx, first); |
| 544 | shctx_unlock(shctx); |
| 545 | } |
| 546 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 547 | return ACT_RET_CONT; |
| 548 | } |
| 549 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 550 | #define HTTP_CACHE_INIT 0 /* Initial state. */ |
| 551 | #define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */ |
| 552 | #define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */ |
| 553 | #define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 554 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 555 | static void http_cache_applet_release(struct appctx *appctx) |
| 556 | { |
| 557 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
| 558 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 559 | struct shared_block *first = block_ptr(cache_ptr); |
| 560 | |
| 561 | shctx_lock(shctx_ptr(cache)); |
| 562 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 563 | shctx_unlock(shctx_ptr(cache)); |
| 564 | } |
| 565 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 566 | /* |
| 567 | * Append an "Age" header into <chn> channel for this <ce> cache entry. |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 568 | * This is the responsibility of the caller to insure there is enough |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 569 | * data in the channel. |
| 570 | * |
| 571 | * Returns the number of bytes inserted if succeeded, 0 if failed. |
| 572 | */ |
| 573 | static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn) |
| 574 | { |
| 575 | unsigned int age; |
| 576 | |
| 577 | age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age; |
| 578 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 579 | age = CACHE_ENTRY_MAX_AGE; |
| 580 | |
| 581 | chunk_reset(&trash); |
| 582 | chunk_printf(&trash, "Age: %u", age); |
| 583 | |
| 584 | return ci_insert_line2(chn, ce->eoh, trash.area, trash.data); |
| 585 | } |
| 586 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 587 | static int cache_channel_row_data_get(struct appctx *appctx, int len) |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 588 | { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 589 | int ret, total; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 590 | struct stream_interface *si = appctx->owner; |
| 591 | struct channel *res = si_ic(si); |
| 592 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 593 | struct shared_context *shctx = shctx_ptr(cache); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 594 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 595 | struct shared_block *blk, *next = appctx->ctx.cache.next; |
| 596 | int offset; |
| 597 | |
| 598 | total = 0; |
| 599 | offset = 0; |
| 600 | |
| 601 | if (!next) { |
| 602 | offset = sizeof(struct cache_entry); |
| 603 | next = block_ptr(cache_ptr); |
| 604 | } |
| 605 | |
| 606 | blk = next; |
| 607 | list_for_each_entry_from(blk, &shctx->hot, list) { |
| 608 | int sz; |
| 609 | |
| 610 | if (len <= 0) |
| 611 | break; |
| 612 | |
| 613 | sz = MIN(len, shctx->block_size - offset); |
| 614 | |
| 615 | ret = ci_putblk(res, (const char *)blk->data + offset, sz); |
| 616 | if (unlikely(offset)) |
| 617 | offset = 0; |
| 618 | if (ret <= 0) { |
| 619 | if (ret == -3 || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 620 | si_rx_room_blk(si); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | total += sz; |
| 627 | len -= sz; |
| 628 | } |
| 629 | appctx->ctx.cache.next = blk; |
| 630 | |
| 631 | return total; |
| 632 | } |
| 633 | |
| 634 | static void http_cache_io_handler(struct appctx *appctx) |
| 635 | { |
| 636 | struct stream_interface *si = appctx->owner; |
| 637 | struct channel *res = si_ic(si); |
| 638 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 639 | struct shared_block *first = block_ptr(cache_ptr); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 640 | int *sent = &appctx->ctx.cache.sent; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 641 | |
| 642 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 643 | goto out; |
| 644 | |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 645 | /* Check if the input buffer is available. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 646 | if (res->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 647 | /* buf.size==0 means we failed to get a buffer and were |
| 648 | * already subscribed to a wait list to get a buffer. |
| 649 | */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 654 | appctx->st0 = HTTP_CACHE_END; |
| 655 | |
| 656 | /* buffer are aligned there, should be fine */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 657 | if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 658 | int len = first->len - *sent - sizeof(struct cache_entry); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 659 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 660 | if (len > 0) { |
| 661 | int ret; |
| 662 | |
| 663 | ret = cache_channel_row_data_get(appctx, len); |
| 664 | if (ret == -1) |
| 665 | appctx->st0 = HTTP_CACHE_END; |
| 666 | else |
| 667 | *sent += ret; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 668 | if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh && |
| 669 | cache_channel_append_age_header(cache_ptr, res)) |
| 670 | appctx->st0 = HTTP_CACHE_HEADER; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 671 | } |
| 672 | else { |
| 673 | *sent = 0; |
| 674 | appctx->st0 = HTTP_CACHE_FWD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 675 | } |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (appctx->st0 == HTTP_CACHE_FWD) { |
| 679 | /* eat the whole request */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 680 | co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 681 | res->flags |= CF_READ_NULL; |
| 682 | si_shutr(si); |
| 683 | } |
| 684 | |
| 685 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 686 | si_shutw(si); |
| 687 | out: |
| 688 | ; |
| 689 | } |
| 690 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 691 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 692 | struct act_rule *rule, char **err) |
| 693 | { |
| 694 | struct flt_conf *fconf; |
| 695 | int cur_arg = *orig_arg; |
| 696 | rule->action = ACT_CUSTOM; |
| 697 | rule->action_ptr = http_action_store_cache; |
| 698 | |
| 699 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 700 | memprintf(err, "expects a cache name"); |
| 701 | return ACT_RET_PRS_ERR; |
| 702 | } |
| 703 | |
| 704 | /* check if a cache filter was already registered with this cache |
| 705 | * name, if that's the case, must use it. */ |
| 706 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 707 | if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) { |
| 708 | rule->arg.act.p[0] = fconf->conf; |
| 709 | (*orig_arg)++; |
| 710 | /* filter already registered */ |
| 711 | return ACT_RET_PRS_OK; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 716 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 717 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 718 | err++; |
| 719 | goto err; |
| 720 | } |
| 721 | /* register a filter to fill the cache buffer */ |
| 722 | fconf = calloc(1, sizeof(*fconf)); |
| 723 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 724 | ha_alert("config: %s '%s': out of memory\n", |
| 725 | proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 726 | err++; |
| 727 | goto err; |
| 728 | } |
| 729 | fconf->id = cache_store_flt_id; |
| 730 | fconf->conf = rule->arg.act.p[0]; /* store the proxy name */ |
| 731 | fconf->ops = &cache_ops; |
| 732 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 733 | |
| 734 | (*orig_arg)++; |
| 735 | |
| 736 | return ACT_RET_PRS_OK; |
| 737 | |
| 738 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 739 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 740 | } |
| 741 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 742 | /* This produces a sha1 hash of the concatenation of the first |
| 743 | * occurrence of the Host header followed by the path component if it |
| 744 | * begins with a slash ('/'). */ |
| 745 | int sha1_hosturi(struct http_txn *txn) |
| 746 | { |
| 747 | struct hdr_ctx ctx; |
| 748 | |
| 749 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 750 | struct buffer *trash; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 751 | char *path; |
| 752 | char *end; |
| 753 | trash = get_trash_chunk(); |
| 754 | |
| 755 | /* retrive the host */ |
| 756 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 757 | if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 758 | return 0; |
| 759 | chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen); |
| 760 | |
| 761 | /* now retrieve the path */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 762 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 763 | path = http_txn_get_path(txn); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 764 | if (!path) |
| 765 | return 0; |
| 766 | chunk_strncat(trash, path, end - path); |
| 767 | |
| 768 | /* hash everything */ |
| 769 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 770 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 771 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 772 | |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 777 | |
| 778 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 779 | struct session *sess, struct stream *s, int flags) |
| 780 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 781 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 782 | struct cache_entry *res; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 783 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 784 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 785 | check_request_for_cacheability(s, &s->req); |
| 786 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 787 | return ACT_RET_CONT; |
| 788 | |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 789 | if (!sha1_hosturi(s->txn)) |
| 790 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 791 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 792 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 793 | return ACT_RET_CONT; |
| 794 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 795 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 796 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 797 | if (res) { |
| 798 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 799 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 800 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 801 | s->target = &http_cache_applet.obj_type; |
| 802 | if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 803 | appctx->st0 = HTTP_CACHE_INIT; |
| 804 | appctx->rule = rule; |
| 805 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 806 | appctx->ctx.cache.next = NULL; |
| 807 | appctx->ctx.cache.sent = 0; |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 808 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 809 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 810 | shctx_lock(shctx_ptr(cache)); |
| 811 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 812 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 813 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 814 | } |
| 815 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 816 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 817 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | |
| 821 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 822 | struct act_rule *rule, char **err) |
| 823 | { |
| 824 | int cur_arg = *orig_arg; |
| 825 | |
| 826 | rule->action = ACT_CUSTOM; |
| 827 | rule->action_ptr = http_action_req_cache_use; |
| 828 | |
| 829 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 830 | memprintf(err, "expects a cache name"); |
| 831 | return ACT_RET_PRS_ERR; |
| 832 | } |
| 833 | |
| 834 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 835 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 836 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 837 | err++; |
| 838 | goto err; |
| 839 | } |
| 840 | |
| 841 | (*orig_arg)++; |
| 842 | return ACT_RET_PRS_OK; |
| 843 | |
| 844 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 845 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 846 | |
| 847 | } |
| 848 | |
| 849 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 850 | { |
| 851 | int err_code = 0; |
| 852 | |
| 853 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 854 | |
| 855 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 856 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 857 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 858 | err_code |= ERR_ALERT | ERR_ABORT; |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 863 | err_code |= ERR_ABORT; |
| 864 | goto out; |
| 865 | } |
| 866 | |
| 867 | if (tmp_cache_config == NULL) { |
| 868 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 869 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 870 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 871 | err_code |= ERR_ALERT | ERR_ABORT; |
| 872 | goto out; |
| 873 | } |
| 874 | |
| 875 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 876 | if (strlen(args[1]) > 32) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 877 | ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 878 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 879 | err_code |= ERR_WARN; |
| 880 | } |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 881 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 882 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 883 | tmp_cache_config->maxobjsz = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 884 | } |
| 885 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 886 | unsigned long int maxsize; |
| 887 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 888 | |
| 889 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 890 | err_code |= ERR_ABORT; |
| 891 | goto out; |
| 892 | } |
| 893 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 894 | maxsize = strtoul(args[1], &err, 10); |
| 895 | if (err == args[1] || *err != '\0') { |
| 896 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 897 | file, linenum, args[1]); |
| 898 | err_code |= ERR_ABORT; |
| 899 | goto out; |
| 900 | } |
| 901 | |
| 902 | if (maxsize > (UINT_MAX >> 20)) { |
| 903 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 904 | file, linenum, args[1], UINT_MAX >> 20); |
| 905 | err_code |= ERR_ABORT; |
| 906 | goto out; |
| 907 | } |
| 908 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 909 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 910 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 911 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 912 | } else if (strcmp(args[0], "max-age") == 0) { |
| 913 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 914 | err_code |= ERR_ABORT; |
| 915 | goto out; |
| 916 | } |
| 917 | |
| 918 | if (!*args[1]) { |
| 919 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 920 | file, linenum, args[0]); |
| 921 | err_code |= ERR_WARN; |
| 922 | } |
| 923 | |
| 924 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 925 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 926 | unsigned int maxobjsz; |
| 927 | char *err; |
| 928 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 929 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 930 | err_code |= ERR_ABORT; |
| 931 | goto out; |
| 932 | } |
| 933 | |
| 934 | if (!*args[1]) { |
| 935 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 936 | file, linenum, args[0]); |
| 937 | err_code |= ERR_WARN; |
| 938 | } |
| 939 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 940 | maxobjsz = strtoul(args[1], &err, 10); |
| 941 | if (err == args[1] || *err != '\0') { |
| 942 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 943 | file, linenum, args[1]); |
| 944 | err_code |= ERR_ABORT; |
| 945 | goto out; |
| 946 | } |
| 947 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 948 | } |
| 949 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 950 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 951 | err_code |= ERR_ALERT | ERR_FATAL; |
| 952 | goto out; |
| 953 | } |
| 954 | out: |
| 955 | return err_code; |
| 956 | } |
| 957 | |
| 958 | /* once the cache section is parsed */ |
| 959 | |
| 960 | int cfg_post_parse_section_cache() |
| 961 | { |
| 962 | struct shared_context *shctx; |
| 963 | int err_code = 0; |
| 964 | int ret_shctx; |
| 965 | |
| 966 | if (tmp_cache_config) { |
| 967 | struct cache *cache; |
| 968 | |
| 969 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 970 | ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 971 | err_code |= ERR_FATAL | ERR_ALERT; |
| 972 | goto out; |
| 973 | } |
| 974 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 975 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 976 | /* Default max. file size is a 256th of the cache size. */ |
| 977 | tmp_cache_config->maxobjsz = |
| 978 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 979 | } |
| 980 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 981 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 982 | err_code |= ERR_FATAL | ERR_ALERT; |
| 983 | goto out; |
| 984 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 985 | |
| 986 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 987 | tmp_cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 988 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 989 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 990 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 991 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 992 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 993 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 994 | |
| 995 | err_code |= ERR_FATAL | ERR_ALERT; |
| 996 | goto out; |
| 997 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 998 | shctx->free_block = cache_free_blocks; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 999 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 1000 | cache = (struct cache *)shctx->data; |
| 1001 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1002 | LIST_ADDQ(&caches, &cache->list); |
| 1003 | } |
| 1004 | out: |
| 1005 | free(tmp_cache_config); |
| 1006 | tmp_cache_config = NULL; |
| 1007 | return err_code; |
| 1008 | |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Resolve the cache name to a pointer once the file is completely read. |
| 1013 | */ |
| 1014 | int cfg_cache_postparser() |
| 1015 | { |
| 1016 | struct act_rule *hresrule, *hrqrule; |
| 1017 | void *cache_ptr; |
| 1018 | struct cache *cache; |
| 1019 | struct proxy *curproxy = NULL; |
| 1020 | int err = 0; |
| 1021 | struct flt_conf *fconf; |
| 1022 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1023 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* resolve the http response cache name to a ptr in the action rule */ |
| 1026 | list_for_each_entry(hresrule, &curproxy->http_res_rules, list) { |
| 1027 | if (hresrule->action != ACT_CUSTOM || |
| 1028 | hresrule->action_ptr != http_action_store_cache) |
| 1029 | continue; |
| 1030 | |
| 1031 | cache_ptr = hresrule->arg.act.p[0]; |
| 1032 | |
| 1033 | list_for_each_entry(cache, &caches, list) { |
| 1034 | if (!strcmp(cache->id, cache_ptr)) { |
| 1035 | /* don't free there, it's still used in the filter conf */ |
| 1036 | cache_ptr = cache; |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | if (cache_ptr == hresrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1042 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n", |
| 1043 | curproxy->id, (char *)hresrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1044 | err++; |
| 1045 | } |
| 1046 | |
| 1047 | hresrule->arg.act.p[0] = cache_ptr; |
| 1048 | } |
| 1049 | |
| 1050 | /* resolve the http request cache name to a ptr in the action rule */ |
| 1051 | list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) { |
| 1052 | if (hrqrule->action != ACT_CUSTOM || |
| 1053 | hrqrule->action_ptr != http_action_req_cache_use) |
| 1054 | continue; |
| 1055 | |
| 1056 | cache_ptr = hrqrule->arg.act.p[0]; |
| 1057 | |
| 1058 | list_for_each_entry(cache, &caches, list) { |
| 1059 | if (!strcmp(cache->id, cache_ptr)) { |
| 1060 | free(cache_ptr); |
| 1061 | cache_ptr = cache; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | if (cache_ptr == hrqrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1067 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n", |
| 1068 | curproxy->id, (char *)hrqrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1069 | err++; |
| 1070 | } |
| 1071 | |
| 1072 | hrqrule->arg.act.p[0] = cache_ptr; |
| 1073 | } |
| 1074 | |
| 1075 | /* resolve the cache name to a ptr in the filter config */ |
| 1076 | list_for_each_entry(fconf, &curproxy->filter_configs, list) { |
| 1077 | |
William Lallemand | 9c54c53 | 2017-11-02 16:38:42 +0100 | [diff] [blame] | 1078 | if (fconf->id != cache_store_flt_id) |
| 1079 | continue; |
| 1080 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1081 | cache_ptr = fconf->conf; |
| 1082 | |
| 1083 | list_for_each_entry(cache, &caches, list) { |
| 1084 | if (!strcmp(cache->id, cache_ptr)) { |
| 1085 | /* there can be only one filter per cache, so we free it there */ |
| 1086 | free(cache_ptr); |
| 1087 | cache_ptr = cache; |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | if (cache_ptr == fconf->conf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1093 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 1094 | curproxy->id, (char *)fconf->conf); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1095 | err++; |
| 1096 | } |
| 1097 | fconf->conf = cache_ptr; |
| 1098 | } |
| 1099 | } |
| 1100 | return err; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | struct flt_ops cache_ops = { |
| 1105 | .init = cache_store_init, |
| 1106 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1107 | /* Handle channels activity */ |
| 1108 | .channel_start_analyze = cache_store_chn_start_analyze, |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 1109 | .channel_end_analyze = cache_store_chn_end_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1110 | |
| 1111 | /* Filter HTTP requests and responses */ |
| 1112 | .http_headers = cache_store_http_headers, |
| 1113 | .http_end = cache_store_http_end, |
| 1114 | |
| 1115 | .http_forward_data = cache_store_http_forward_data, |
| 1116 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1117 | }; |
| 1118 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1119 | static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1120 | { |
| 1121 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1122 | return 1; |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1128 | { |
| 1129 | struct cache* cache = appctx->ctx.cli.p0; |
| 1130 | struct stream_interface *si = appctx->owner; |
| 1131 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1132 | if (cache == NULL) { |
| 1133 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1134 | } |
| 1135 | |
| 1136 | list_for_each_entry_from(cache, &caches, list) { |
| 1137 | struct eb32_node *node = NULL; |
| 1138 | unsigned int next_key; |
| 1139 | struct cache_entry *entry; |
| 1140 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1141 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1142 | if (!next_key) { |
| 1143 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1144 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1145 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1149 | |
| 1150 | appctx->ctx.cli.p0 = cache; |
| 1151 | |
| 1152 | while (1) { |
| 1153 | |
| 1154 | shctx_lock(shctx_ptr(cache)); |
| 1155 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1156 | if (!node) { |
| 1157 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1158 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1159 | break; |
| 1160 | } |
| 1161 | |
| 1162 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1163 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, (*(unsigned int *)entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1164 | |
| 1165 | next_key = node->key + 1; |
| 1166 | appctx->ctx.cli.i0 = next_key; |
| 1167 | |
| 1168 | shctx_unlock(shctx_ptr(cache)); |
| 1169 | |
| 1170 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1171 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | } |
| 1177 | |
| 1178 | return 1; |
| 1179 | |
| 1180 | } |
| 1181 | |
| 1182 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1183 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1184 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1185 | }}; |
| 1186 | |
| 1187 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1188 | static struct action_kw_list http_res_actions = { |
| 1189 | .kw = { |
| 1190 | { "cache-store", parse_cache_store }, |
| 1191 | { NULL, NULL } |
| 1192 | } |
| 1193 | }; |
| 1194 | |
| 1195 | static struct action_kw_list http_req_actions = { |
| 1196 | .kw = { |
| 1197 | { "cache-use", parse_cache_use }, |
| 1198 | { NULL, NULL } |
| 1199 | } |
| 1200 | }; |
| 1201 | |
| 1202 | struct applet http_cache_applet = { |
| 1203 | .obj_type = OBJ_TYPE_APPLET, |
| 1204 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1205 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1206 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1207 | }; |
| 1208 | |
| 1209 | __attribute__((constructor)) |
| 1210 | static void __cache_init(void) |
| 1211 | { |
| 1212 | cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 1213 | cfg_register_postparser("cache", cfg_cache_postparser); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1214 | cli_register_kw(&cli_kws); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1215 | http_res_keywords_register(&http_res_actions); |
| 1216 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1217 | pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |