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