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