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