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) { |
| 221 | /* Nothing to foward */ |
| 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. |
| 568 | * This is the responsability of the caller to insure there is enough |
| 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 | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 620 | si_cant_put(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 | |
| 645 | /* Check if the input buffer is avalaible. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 646 | if (res->buf.size == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 647 | si_cant_put(si); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 648 | goto out; |
| 649 | } |
| 650 | |
| 651 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 652 | appctx->st0 = HTTP_CACHE_END; |
| 653 | |
| 654 | /* buffer are aligned there, should be fine */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 655 | 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] | 656 | int len = first->len - *sent - sizeof(struct cache_entry); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 657 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 658 | if (len > 0) { |
| 659 | int ret; |
| 660 | |
| 661 | ret = cache_channel_row_data_get(appctx, len); |
| 662 | if (ret == -1) |
| 663 | appctx->st0 = HTTP_CACHE_END; |
| 664 | else |
| 665 | *sent += ret; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 666 | if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh && |
| 667 | cache_channel_append_age_header(cache_ptr, res)) |
| 668 | appctx->st0 = HTTP_CACHE_HEADER; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 669 | } |
| 670 | else { |
| 671 | *sent = 0; |
| 672 | appctx->st0 = HTTP_CACHE_FWD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 673 | } |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (appctx->st0 == HTTP_CACHE_FWD) { |
| 677 | /* eat the whole request */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 678 | 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] | 679 | res->flags |= CF_READ_NULL; |
| 680 | si_shutr(si); |
| 681 | } |
| 682 | |
| 683 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 684 | si_shutw(si); |
| 685 | out: |
| 686 | ; |
| 687 | } |
| 688 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 689 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 690 | struct act_rule *rule, char **err) |
| 691 | { |
| 692 | struct flt_conf *fconf; |
| 693 | int cur_arg = *orig_arg; |
| 694 | rule->action = ACT_CUSTOM; |
| 695 | rule->action_ptr = http_action_store_cache; |
| 696 | |
| 697 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 698 | memprintf(err, "expects a cache name"); |
| 699 | return ACT_RET_PRS_ERR; |
| 700 | } |
| 701 | |
| 702 | /* check if a cache filter was already registered with this cache |
| 703 | * name, if that's the case, must use it. */ |
| 704 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 705 | if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) { |
| 706 | rule->arg.act.p[0] = fconf->conf; |
| 707 | (*orig_arg)++; |
| 708 | /* filter already registered */ |
| 709 | return ACT_RET_PRS_OK; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 714 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 715 | 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] | 716 | err++; |
| 717 | goto err; |
| 718 | } |
| 719 | /* register a filter to fill the cache buffer */ |
| 720 | fconf = calloc(1, sizeof(*fconf)); |
| 721 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 722 | ha_alert("config: %s '%s': out of memory\n", |
| 723 | proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 724 | err++; |
| 725 | goto err; |
| 726 | } |
| 727 | fconf->id = cache_store_flt_id; |
| 728 | fconf->conf = rule->arg.act.p[0]; /* store the proxy name */ |
| 729 | fconf->ops = &cache_ops; |
| 730 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 731 | |
| 732 | (*orig_arg)++; |
| 733 | |
| 734 | return ACT_RET_PRS_OK; |
| 735 | |
| 736 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 737 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 738 | } |
| 739 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 740 | /* This produces a sha1 hash of the concatenation of the first |
| 741 | * occurrence of the Host header followed by the path component if it |
| 742 | * begins with a slash ('/'). */ |
| 743 | int sha1_hosturi(struct http_txn *txn) |
| 744 | { |
| 745 | struct hdr_ctx ctx; |
| 746 | |
| 747 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 748 | struct buffer *trash; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 749 | char *path; |
| 750 | char *end; |
| 751 | trash = get_trash_chunk(); |
| 752 | |
| 753 | /* retrive the host */ |
| 754 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 755 | 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] | 756 | return 0; |
| 757 | chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen); |
| 758 | |
| 759 | /* now retrieve the path */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 760 | 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] | 761 | path = http_txn_get_path(txn); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 762 | if (!path) |
| 763 | return 0; |
| 764 | chunk_strncat(trash, path, end - path); |
| 765 | |
| 766 | /* hash everything */ |
| 767 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 768 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 769 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 770 | |
| 771 | return 1; |
| 772 | } |
| 773 | |
| 774 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 775 | |
| 776 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 777 | struct session *sess, struct stream *s, int flags) |
| 778 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 779 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 780 | struct cache_entry *res; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 781 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 782 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 783 | check_request_for_cacheability(s, &s->req); |
| 784 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 785 | return ACT_RET_CONT; |
| 786 | |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 787 | if (!sha1_hosturi(s->txn)) |
| 788 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 789 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 790 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 791 | return ACT_RET_CONT; |
| 792 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 793 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 794 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 795 | if (res) { |
| 796 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 797 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 798 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 799 | s->target = &http_cache_applet.obj_type; |
| 800 | if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 801 | appctx->st0 = HTTP_CACHE_INIT; |
| 802 | appctx->rule = rule; |
| 803 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 804 | appctx->ctx.cache.next = NULL; |
| 805 | appctx->ctx.cache.sent = 0; |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 806 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 807 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 808 | shctx_lock(shctx_ptr(cache)); |
| 809 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 810 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 811 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 812 | } |
| 813 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 814 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 815 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
| 819 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 820 | struct act_rule *rule, char **err) |
| 821 | { |
| 822 | int cur_arg = *orig_arg; |
| 823 | |
| 824 | rule->action = ACT_CUSTOM; |
| 825 | rule->action_ptr = http_action_req_cache_use; |
| 826 | |
| 827 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 828 | memprintf(err, "expects a cache name"); |
| 829 | return ACT_RET_PRS_ERR; |
| 830 | } |
| 831 | |
| 832 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 833 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 834 | 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] | 835 | err++; |
| 836 | goto err; |
| 837 | } |
| 838 | |
| 839 | (*orig_arg)++; |
| 840 | return ACT_RET_PRS_OK; |
| 841 | |
| 842 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 843 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 844 | |
| 845 | } |
| 846 | |
| 847 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 848 | { |
| 849 | int err_code = 0; |
| 850 | |
| 851 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 852 | |
| 853 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 854 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 855 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 856 | err_code |= ERR_ALERT | ERR_ABORT; |
| 857 | goto out; |
| 858 | } |
| 859 | |
| 860 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 861 | err_code |= ERR_ABORT; |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | if (tmp_cache_config == NULL) { |
| 866 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 867 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 868 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 869 | err_code |= ERR_ALERT | ERR_ABORT; |
| 870 | goto out; |
| 871 | } |
| 872 | |
| 873 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 874 | if (strlen(args[1]) > 32) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 875 | ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 876 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 877 | err_code |= ERR_WARN; |
| 878 | } |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 879 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 880 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 881 | tmp_cache_config->maxobjsz = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 882 | } |
| 883 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 884 | unsigned long int maxsize; |
| 885 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 886 | |
| 887 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 888 | err_code |= ERR_ABORT; |
| 889 | goto out; |
| 890 | } |
| 891 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 892 | maxsize = strtoul(args[1], &err, 10); |
| 893 | if (err == args[1] || *err != '\0') { |
| 894 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 895 | file, linenum, args[1]); |
| 896 | err_code |= ERR_ABORT; |
| 897 | goto out; |
| 898 | } |
| 899 | |
| 900 | if (maxsize > (UINT_MAX >> 20)) { |
| 901 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 902 | file, linenum, args[1], UINT_MAX >> 20); |
| 903 | err_code |= ERR_ABORT; |
| 904 | goto out; |
| 905 | } |
| 906 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 907 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 908 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 909 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 910 | } else if (strcmp(args[0], "max-age") == 0) { |
| 911 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 912 | err_code |= ERR_ABORT; |
| 913 | goto out; |
| 914 | } |
| 915 | |
| 916 | if (!*args[1]) { |
| 917 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 918 | file, linenum, args[0]); |
| 919 | err_code |= ERR_WARN; |
| 920 | } |
| 921 | |
| 922 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 923 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 924 | unsigned int maxobjsz; |
| 925 | char *err; |
| 926 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 927 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 928 | err_code |= ERR_ABORT; |
| 929 | goto out; |
| 930 | } |
| 931 | |
| 932 | if (!*args[1]) { |
| 933 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 934 | file, linenum, args[0]); |
| 935 | err_code |= ERR_WARN; |
| 936 | } |
| 937 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 938 | maxobjsz = strtoul(args[1], &err, 10); |
| 939 | if (err == args[1] || *err != '\0') { |
| 940 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 941 | file, linenum, args[1]); |
| 942 | err_code |= ERR_ABORT; |
| 943 | goto out; |
| 944 | } |
| 945 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 946 | } |
| 947 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 948 | 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] | 949 | err_code |= ERR_ALERT | ERR_FATAL; |
| 950 | goto out; |
| 951 | } |
| 952 | out: |
| 953 | return err_code; |
| 954 | } |
| 955 | |
| 956 | /* once the cache section is parsed */ |
| 957 | |
| 958 | int cfg_post_parse_section_cache() |
| 959 | { |
| 960 | struct shared_context *shctx; |
| 961 | int err_code = 0; |
| 962 | int ret_shctx; |
| 963 | |
| 964 | if (tmp_cache_config) { |
| 965 | struct cache *cache; |
| 966 | |
| 967 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 968 | 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] | 969 | err_code |= ERR_FATAL | ERR_ALERT; |
| 970 | goto out; |
| 971 | } |
| 972 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 973 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 974 | /* Default max. file size is a 256th of the cache size. */ |
| 975 | tmp_cache_config->maxobjsz = |
| 976 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 977 | } |
| 978 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 979 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 980 | err_code |= ERR_FATAL | ERR_ALERT; |
| 981 | goto out; |
| 982 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 983 | |
| 984 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 985 | tmp_cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 986 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 987 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 988 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 989 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 990 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 991 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 992 | |
| 993 | err_code |= ERR_FATAL | ERR_ALERT; |
| 994 | goto out; |
| 995 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 996 | shctx->free_block = cache_free_blocks; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 997 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 998 | cache = (struct cache *)shctx->data; |
| 999 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1000 | LIST_ADDQ(&caches, &cache->list); |
| 1001 | } |
| 1002 | out: |
| 1003 | free(tmp_cache_config); |
| 1004 | tmp_cache_config = NULL; |
| 1005 | return err_code; |
| 1006 | |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Resolve the cache name to a pointer once the file is completely read. |
| 1011 | */ |
| 1012 | int cfg_cache_postparser() |
| 1013 | { |
| 1014 | struct act_rule *hresrule, *hrqrule; |
| 1015 | void *cache_ptr; |
| 1016 | struct cache *cache; |
| 1017 | struct proxy *curproxy = NULL; |
| 1018 | int err = 0; |
| 1019 | struct flt_conf *fconf; |
| 1020 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1021 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1022 | |
| 1023 | /* resolve the http response cache name to a ptr in the action rule */ |
| 1024 | list_for_each_entry(hresrule, &curproxy->http_res_rules, list) { |
| 1025 | if (hresrule->action != ACT_CUSTOM || |
| 1026 | hresrule->action_ptr != http_action_store_cache) |
| 1027 | continue; |
| 1028 | |
| 1029 | cache_ptr = hresrule->arg.act.p[0]; |
| 1030 | |
| 1031 | list_for_each_entry(cache, &caches, list) { |
| 1032 | if (!strcmp(cache->id, cache_ptr)) { |
| 1033 | /* don't free there, it's still used in the filter conf */ |
| 1034 | cache_ptr = cache; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | if (cache_ptr == hresrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1040 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n", |
| 1041 | curproxy->id, (char *)hresrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1042 | err++; |
| 1043 | } |
| 1044 | |
| 1045 | hresrule->arg.act.p[0] = cache_ptr; |
| 1046 | } |
| 1047 | |
| 1048 | /* resolve the http request cache name to a ptr in the action rule */ |
| 1049 | list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) { |
| 1050 | if (hrqrule->action != ACT_CUSTOM || |
| 1051 | hrqrule->action_ptr != http_action_req_cache_use) |
| 1052 | continue; |
| 1053 | |
| 1054 | cache_ptr = hrqrule->arg.act.p[0]; |
| 1055 | |
| 1056 | list_for_each_entry(cache, &caches, list) { |
| 1057 | if (!strcmp(cache->id, cache_ptr)) { |
| 1058 | free(cache_ptr); |
| 1059 | cache_ptr = cache; |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | if (cache_ptr == hrqrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1065 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n", |
| 1066 | curproxy->id, (char *)hrqrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1067 | err++; |
| 1068 | } |
| 1069 | |
| 1070 | hrqrule->arg.act.p[0] = cache_ptr; |
| 1071 | } |
| 1072 | |
| 1073 | /* resolve the cache name to a ptr in the filter config */ |
| 1074 | list_for_each_entry(fconf, &curproxy->filter_configs, list) { |
| 1075 | |
William Lallemand | 9c54c53 | 2017-11-02 16:38:42 +0100 | [diff] [blame] | 1076 | if (fconf->id != cache_store_flt_id) |
| 1077 | continue; |
| 1078 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1079 | cache_ptr = fconf->conf; |
| 1080 | |
| 1081 | list_for_each_entry(cache, &caches, list) { |
| 1082 | if (!strcmp(cache->id, cache_ptr)) { |
| 1083 | /* there can be only one filter per cache, so we free it there */ |
| 1084 | free(cache_ptr); |
| 1085 | cache_ptr = cache; |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | if (cache_ptr == fconf->conf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1091 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 1092 | curproxy->id, (char *)fconf->conf); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1093 | err++; |
| 1094 | } |
| 1095 | fconf->conf = cache_ptr; |
| 1096 | } |
| 1097 | } |
| 1098 | return err; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | struct flt_ops cache_ops = { |
| 1103 | .init = cache_store_init, |
| 1104 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1105 | /* Handle channels activity */ |
| 1106 | .channel_start_analyze = cache_store_chn_start_analyze, |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 1107 | .channel_end_analyze = cache_store_chn_end_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1108 | |
| 1109 | /* Filter HTTP requests and responses */ |
| 1110 | .http_headers = cache_store_http_headers, |
| 1111 | .http_end = cache_store_http_end, |
| 1112 | |
| 1113 | .http_forward_data = cache_store_http_forward_data, |
| 1114 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1115 | }; |
| 1116 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1117 | 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] | 1118 | { |
| 1119 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1120 | return 1; |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1126 | { |
| 1127 | struct cache* cache = appctx->ctx.cli.p0; |
| 1128 | struct stream_interface *si = appctx->owner; |
| 1129 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1130 | if (cache == NULL) { |
| 1131 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1132 | } |
| 1133 | |
| 1134 | list_for_each_entry_from(cache, &caches, list) { |
| 1135 | struct eb32_node *node = NULL; |
| 1136 | unsigned int next_key; |
| 1137 | struct cache_entry *entry; |
| 1138 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1139 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1140 | if (!next_key) { |
| 1141 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1142 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1143 | si_cant_put(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1144 | return 0; |
| 1145 | } |
| 1146 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1147 | |
| 1148 | appctx->ctx.cli.p0 = cache; |
| 1149 | |
| 1150 | while (1) { |
| 1151 | |
| 1152 | shctx_lock(shctx_ptr(cache)); |
| 1153 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1154 | if (!node) { |
| 1155 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1156 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1161 | 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] | 1162 | |
| 1163 | next_key = node->key + 1; |
| 1164 | appctx->ctx.cli.i0 = next_key; |
| 1165 | |
| 1166 | shctx_unlock(shctx_ptr(cache)); |
| 1167 | |
| 1168 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1169 | si_cant_put(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | } |
| 1175 | |
| 1176 | return 1; |
| 1177 | |
| 1178 | } |
| 1179 | |
| 1180 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1181 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1182 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1183 | }}; |
| 1184 | |
| 1185 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1186 | static struct action_kw_list http_res_actions = { |
| 1187 | .kw = { |
| 1188 | { "cache-store", parse_cache_store }, |
| 1189 | { NULL, NULL } |
| 1190 | } |
| 1191 | }; |
| 1192 | |
| 1193 | static struct action_kw_list http_req_actions = { |
| 1194 | .kw = { |
| 1195 | { "cache-use", parse_cache_use }, |
| 1196 | { NULL, NULL } |
| 1197 | } |
| 1198 | }; |
| 1199 | |
| 1200 | struct applet http_cache_applet = { |
| 1201 | .obj_type = OBJ_TYPE_APPLET, |
| 1202 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1203 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1204 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | __attribute__((constructor)) |
| 1208 | static void __cache_init(void) |
| 1209 | { |
| 1210 | cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 1211 | cfg_register_postparser("cache", cfg_cache_postparser); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1212 | cli_register_kw(&cli_kws); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1213 | http_res_keywords_register(&http_res_actions); |
| 1214 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1215 | 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] | 1216 | } |
| 1217 | |