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