blob: fdb0f256f6bcdf25264d76f92d3e97cd73273edb [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
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 Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
25#include <proto/hdr_idx.h>
26#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#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 Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
36#include <common/hash.h>
37
38/* flt_cache_store */
39
40static const char *cache_store_flt_id = "cache store filter";
41
Willy Tarreaubafbe012017-11-24 17:34:44 +010042static struct pool_head *pool_head_cache_st = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +010043
William Lallemand41db4602017-10-30 11:15:51 +010044struct applet http_cache_applet;
45
46struct flt_ops cache_ops;
47
48struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010050 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 unsigned int maxage; /* max-age */
52 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020053 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010054 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010055};
56
57/*
58 * cache ctx for filters
59 */
60struct cache_st {
61 int hdrs_len;
62 struct shared_block *first_block;
63};
64
65struct cache_entry {
66 unsigned int latest_validation; /* latest validation date */
67 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020068 unsigned int age; /* Origin server "Age" header value */
69 unsigned int eoh; /* Origin server end of headers offset. */
William Lallemand41db4602017-10-30 11:15:51 +010070 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010071 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010072 unsigned char data[0];
73};
74
75#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010076#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010077
78static struct list caches = LIST_HEAD_INIT(caches);
79static struct cache *tmp_cache_config = NULL;
80
William Lallemandf528fff2017-11-23 19:43:17 +010081struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010082{
83 struct eb32_node *node;
84 struct cache_entry *entry;
85
William Lallemandf528fff2017-11-23 19:43:17 +010086 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010087 if (!node)
88 return NULL;
89
90 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010091
92 /* if that's not the right node */
93 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
94 return NULL;
95
William Lallemand08727662017-11-21 20:01:27 +010096 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010097 return entry;
William Lallemand08727662017-11-21 20:01:27 +010098 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100100 entry->eb.key = 0;
101 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100102 return NULL;
103
104}
105
106static inline struct shared_context *shctx_ptr(struct cache *cache)
107{
108 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
109}
110
William Lallemand77c11972017-10-31 20:43:01 +0100111static inline struct shared_block *block_ptr(struct cache_entry *entry)
112{
113 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
114}
115
116
117
William Lallemand41db4602017-10-30 11:15:51 +0100118static int
119cache_store_init(struct proxy *px, struct flt_conf *f1conf)
120{
121 return 0;
122}
123
William Lallemand4da3f8a2017-10-31 14:33:34 +0100124static int
125cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
126{
127 if (!(chn->flags & CF_ISRESP))
128 return 1;
129
130 if (filter->ctx == NULL) {
131 struct cache_st *st;
132
Willy Tarreaubafbe012017-11-24 17:34:44 +0100133 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100134 if (st == NULL)
135 return -1;
136
137 st->hdrs_len = 0;
138 st->first_block = NULL;
139 filter->ctx = st;
140 }
141
142 register_data_filter(s, chn, filter);
143
144 return 1;
145}
146
147static int
William Lallemand49dc0482017-11-24 14:33:54 +0100148cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
149{
150 struct cache_st *st = filter->ctx;
151 struct cache *cache = filter->config->conf;
152 struct shared_context *shctx = shctx_ptr(cache);
153
154 if (!(chn->flags & CF_ISRESP))
155 return 1;
156
157 /* Everything should be released in the http_end filter, but we need to do it
158 * there too, in case of errors */
159
160 if (st && st->first_block) {
161
162 shctx_lock(shctx);
163 shctx_row_dec_hot(shctx, st->first_block);
164 shctx_unlock(shctx);
165
166 }
167 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100168 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100169 filter->ctx = NULL;
170 }
171
172 return 1;
173}
174
175
176static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100177cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
178{
179 struct cache_st *st = filter->ctx;
180
William Lallemand4da3f8a2017-10-31 14:33:34 +0100181 if (!(msg->chn->flags & CF_ISRESP) || !st)
182 return 1;
183
William Lallemand9d5f54d2017-11-14 14:39:22 +0100184 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100185
186 return 1;
187}
188
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200189static inline void disable_cache_entry(struct cache_st *st,
190 struct filter *filter, struct shared_context *shctx)
191{
192 struct cache_entry *object;
193
194 object = (struct cache_entry *)st->first_block->data;
195 filter->ctx = NULL; /* disable cache */
196 shctx_lock(shctx);
197 shctx_row_dec_hot(shctx, st->first_block);
198 object->eb.key = 0;
199 shctx_unlock(shctx);
200 pool_free(pool_head_cache_st, st);
201}
202
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203static int
204cache_store_http_forward_data(struct stream *s, struct filter *filter,
205 struct http_msg *msg, unsigned int len)
206{
207 struct cache_st *st = filter->ctx;
208 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
209 int ret;
210
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200211 ret = 0;
212
William Lallemand4da3f8a2017-10-31 14:33:34 +0100213 /*
214 * We need to skip the HTTP headers first, because we saved them in the
215 * http-response action.
216 */
217 if (!(msg->chn->flags & CF_ISRESP) || !st)
218 return len;
219
220 if (!len) {
221 /* Nothing to foward */
222 ret = len;
223 }
William Lallemand10935bc2017-11-14 14:39:23 +0100224 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225 /* Forward part of headers */
226 ret = len;
227 st->hdrs_len -= len;
228 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100229 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100230 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100231 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200232 int to_append, append;
233 struct shared_block *fb;
234
235 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
236
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200237 shctx_lock(shctx);
238 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
239 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100240 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200241 disable_cache_entry(st, filter, shctx);
242 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100243 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200244 shctx_unlock(shctx);
245
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200246 /* Skip remaining headers to fill the cache */
247 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200248 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
249 (unsigned char *)ci_head(msg->chn), to_append);
250 ret = st->hdrs_len + to_append - append;
251 /* Rewind the buffer to forward all data */
252 c_rew(msg->chn, st->hdrs_len);
253 st->hdrs_len = 0;
254 if (ret < 0)
255 disable_cache_entry(st, filter, shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100256 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200257 else
258 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100259 }
260
261 if ((ret != len) ||
262 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
263 task_wakeup(s->task, TASK_WOKEN_MSG);
264
265 return ret;
266}
267
268static int
269cache_store_http_end(struct stream *s, struct filter *filter,
270 struct http_msg *msg)
271{
272 struct cache_st *st = filter->ctx;
273 struct cache *cache = filter->config->conf;
274 struct shared_context *shctx = shctx_ptr(cache);
275 struct cache_entry *object;
276
277 if (!(msg->chn->flags & CF_ISRESP))
278 return 1;
279
280 if (st && st->first_block) {
281
282 object = (struct cache_entry *)st->first_block->data;
283
284 /* does not need to test if the insertion worked, if it
285 * doesn't, the blocks will be reused anyway */
286
287 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100288 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
289 object->eb.key = 0;
290 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100291 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100292 shctx_row_dec_hot(shctx, st->first_block);
293 shctx_unlock(shctx);
294
295 }
296 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100297 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100298 filter->ctx = NULL;
299 }
300
301 return 1;
302}
303
304 /*
305 * This intends to be used when checking HTTP headers for some
306 * word=value directive. Return a pointer to the first character of value, if
307 * the word was not found or if there wasn't any value assigned ot it return NULL
308 */
309char *directive_value(const char *sample, int slen, const char *word, int wlen)
310{
311 int st = 0;
312
313 if (slen < wlen)
314 return 0;
315
316 while (wlen) {
317 char c = *sample ^ *word;
318 if (c && c != ('A' ^ 'a'))
319 return NULL;
320 sample++;
321 word++;
322 slen--;
323 wlen--;
324 }
325
326 while (slen) {
327 if (st == 0) {
328 if (*sample != '=')
329 return NULL;
330 sample++;
331 slen--;
332 st = 1;
333 continue;
334 } else {
335 return (char *)sample;
336 }
337 }
338
339 return NULL;
340}
341
342/*
343 * Return the maxage in seconds of an HTTP response.
344 * Compute the maxage using either:
345 * - the assigned max-age of the cache
346 * - the s-maxage directive
347 * - the max-age directive
348 * - (Expires - Data) headers
349 * - the default-max-age of the cache
350 *
351 */
William Lallemand49b44532017-11-24 18:53:43 +0100352int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100353{
354 struct http_txn *txn = s->txn;
355 struct hdr_ctx ctx;
356
357 int smaxage = -1;
358 int maxage = -1;
359
360
William Lallemand4da3f8a2017-10-31 14:33:34 +0100361 ctx.idx = 0;
362
363 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200364 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100365 char *directive = ctx.line + ctx.val;
366 char *value;
367
368 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
369 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200370 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100371
372 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
373 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200374 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100375 }
376
377 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
378 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200379 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100380
381 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
382 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200383 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100384 }
385 }
386
387 /* TODO: Expires - Data */
388
389
390 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100391 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392
393 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100394 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395
William Lallemand49b44532017-11-24 18:53:43 +0100396 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397
398}
399
400
William Lallemanda400a3a2017-11-20 19:13:12 +0100401static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
402{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200403 struct cache_entry *object = (struct cache_entry *)block->data;
404
405 if (first == block && object->eb.key)
406 eb32_delete(&object->eb);
407 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100408}
409
William Lallemand41db4602017-10-30 11:15:51 +0100410/*
411 * This fonction will store the headers of the response in a buffer and then
412 * register a filter to store the data
413 */
414enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
415 struct session *sess, struct stream *s, int flags)
416{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200417 unsigned int age;
418 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 struct http_txn *txn = s->txn;
420 struct http_msg *msg = &txn->rsp;
421 struct filter *filter;
422 struct hdr_ctx ctx;
423 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100424 struct cache *cache = (struct cache *)rule->arg.act.p[0];
425 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100426 struct cache_entry *object;
427
428
429 /* Don't cache if the response came from a cache */
430 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
431 s->target == &http_cache_applet.obj_type) {
432 goto out;
433 }
434
435 /* cache only HTTP/1.1 */
436 if (!(txn->req.flags & HTTP_MSGF_VER_11))
437 goto out;
438
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200439 /* Do not cache too big objects. */
440 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
441 msg->sov + msg->body_len > shctx->max_obj_size)
442 goto out;
443
William Lallemand4da3f8a2017-10-31 14:33:34 +0100444 /* cache only GET method */
445 if (txn->meth != HTTP_METH_GET)
446 goto out;
447
448 /* cache only 200 status code */
449 if (txn->status != 200)
450 goto out;
451
452 /* Does not manage Vary at the moment. We will need a secondary key later for that */
453 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200454 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100455 goto out;
456
Willy Tarreaufaf29092017-12-21 15:59:17 +0100457 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100458
Willy Tarreaud4569d12017-12-22 18:03:04 +0100459 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100460 goto out;
461
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200462 age = 0;
463 ctx.idx = 0;
464 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
465 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
466 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
467 hdr_age = CACHE_ENTRY_MAX_AGE;
468 age = hdr_age;
469 }
470 http_remove_header2(msg, &txn->hdr_idx, &ctx);
471 }
472
William Lallemand4da3f8a2017-10-31 14:33:34 +0100473 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200474 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475 if (!first) {
476 shctx_unlock(shctx);
477 goto out;
478 }
479 shctx_unlock(shctx);
480
Willy Tarreau1093a452018-04-06 19:02:25 +0200481 /* the received memory is not initialized, we need at least to mark
482 * the object as not indexed yet.
483 */
484 object = (struct cache_entry *)first->data;
485 object->eb.node.leaf_p = NULL;
486 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200487 object->age = age;
488 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200489
William Lallemand4da3f8a2017-10-31 14:33:34 +0100490 /* reserve space for the cache_entry structure */
491 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200492 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493 /* cache the headers in a http action because it allows to chose what
494 * to cache, for example you might want to cache a response before
495 * modifying some HTTP headers, or on the contrary after modifying
496 * those headers.
497 */
498
499 /* does not need to be locked because it's in the "hot" list,
500 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200501 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502 goto out;
503
504 /* register the buffer in the filter ctx for filling it with data*/
505 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
506 list_for_each_entry(filter, &s->strm_flt.filters, list) {
507 if (filter->config->id == cache_store_flt_id &&
508 filter->config->conf == rule->arg.act.p[0]) {
509 if (filter->ctx) {
510 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100511 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100512
513 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100514
William Lallemandf528fff2017-11-23 19:43:17 +0100515 object->eb.key = (*(unsigned int *)&txn->cache_hash);
516 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100517 /* Insert the node later on caching success */
518
519 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100520
521 old = entry_exist(cache, txn->cache_hash);
522 if (old) {
523 eb32_delete(&old->eb);
524 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 }
526 shctx_unlock(shctx);
527
528 /* store latest value and expiration time */
529 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100530 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100531 }
532 return ACT_RET_CONT;
533 }
534 }
535 }
536
537out:
538 /* if does not cache */
539 if (first) {
540 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100541 first->len = 0;
542 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543 shctx_row_dec_hot(shctx, first);
544 shctx_unlock(shctx);
545 }
546
William Lallemand41db4602017-10-30 11:15:51 +0100547 return ACT_RET_CONT;
548}
549
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200550#define HTTP_CACHE_INIT 0 /* Initial state. */
551#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
552#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
553#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100554
William Lallemandecb73b12017-11-24 14:33:55 +0100555static void http_cache_applet_release(struct appctx *appctx)
556{
557 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
558 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
559 struct shared_block *first = block_ptr(cache_ptr);
560
561 shctx_lock(shctx_ptr(cache));
562 shctx_row_dec_hot(shctx_ptr(cache), first);
563 shctx_unlock(shctx_ptr(cache));
564}
565
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200566/*
567 * Append an "Age" header into <chn> channel for this <ce> cache entry.
568 * This is the responsability of the caller to insure there is enough
569 * data in the channel.
570 *
571 * Returns the number of bytes inserted if succeeded, 0 if failed.
572 */
573static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
574{
575 unsigned int age;
576
577 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
578 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
579 age = CACHE_ENTRY_MAX_AGE;
580
581 chunk_reset(&trash);
582 chunk_printf(&trash, "Age: %u", age);
583
584 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
585}
586
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200587static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100588{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200589 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100590 struct stream_interface *si = appctx->owner;
591 struct channel *res = si_ic(si);
592 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100593 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200594 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
595 struct shared_block *blk, *next = appctx->ctx.cache.next;
596 int offset;
597
598 total = 0;
599 offset = 0;
600
601 if (!next) {
602 offset = sizeof(struct cache_entry);
603 next = block_ptr(cache_ptr);
604 }
605
606 blk = next;
607 list_for_each_entry_from(blk, &shctx->hot, list) {
608 int sz;
609
610 if (len <= 0)
611 break;
612
613 sz = MIN(len, shctx->block_size - offset);
614
615 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
616 if (unlikely(offset))
617 offset = 0;
618 if (ret <= 0) {
619 if (ret == -3 || ret == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100620 si_cant_put(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200621 break;
622 }
623 return -1;
624 }
625
626 total += sz;
627 len -= sz;
628 }
629 appctx->ctx.cache.next = blk;
630
631 return total;
632}
633
634static void http_cache_io_handler(struct appctx *appctx)
635{
636 struct stream_interface *si = appctx->owner;
637 struct channel *res = si_ic(si);
638 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100639 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200640 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100641
642 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
643 goto out;
644
645 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200646 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100647 /* buf.size==0 means we failed to get a buffer and were
648 * already subscribed to a wait list to get a buffer.
649 */
William Lallemand77c11972017-10-31 20:43:01 +0100650 goto out;
651 }
652
653 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
654 appctx->st0 = HTTP_CACHE_END;
655
656 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200657 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200658 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100659
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200660 if (len > 0) {
661 int ret;
662
663 ret = cache_channel_row_data_get(appctx, len);
664 if (ret == -1)
665 appctx->st0 = HTTP_CACHE_END;
666 else
667 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200668 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
669 cache_channel_append_age_header(cache_ptr, res))
670 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200671 }
672 else {
673 *sent = 0;
674 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100675 }
William Lallemand77c11972017-10-31 20:43:01 +0100676 }
677
678 if (appctx->st0 == HTTP_CACHE_FWD) {
679 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200680 co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code
William Lallemand77c11972017-10-31 20:43:01 +0100681 res->flags |= CF_READ_NULL;
682 si_shutr(si);
683 }
684
685 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
686 si_shutw(si);
687out:
688 ;
689}
690
William Lallemand41db4602017-10-30 11:15:51 +0100691enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
692 struct act_rule *rule, char **err)
693{
694 struct flt_conf *fconf;
695 int cur_arg = *orig_arg;
696 rule->action = ACT_CUSTOM;
697 rule->action_ptr = http_action_store_cache;
698
699 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
700 memprintf(err, "expects a cache name");
701 return ACT_RET_PRS_ERR;
702 }
703
704 /* check if a cache filter was already registered with this cache
705 * name, if that's the case, must use it. */
706 list_for_each_entry(fconf, &proxy->filter_configs, list) {
707 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
708 rule->arg.act.p[0] = fconf->conf;
709 (*orig_arg)++;
710 /* filter already registered */
711 return ACT_RET_PRS_OK;
712 }
713 }
714
715 rule->arg.act.p[0] = strdup(args[cur_arg]);
716 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100717 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100718 err++;
719 goto err;
720 }
721 /* register a filter to fill the cache buffer */
722 fconf = calloc(1, sizeof(*fconf));
723 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100724 ha_alert("config: %s '%s': out of memory\n",
725 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100726 err++;
727 goto err;
728 }
729 fconf->id = cache_store_flt_id;
730 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
731 fconf->ops = &cache_ops;
732 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
733
734 (*orig_arg)++;
735
736 return ACT_RET_PRS_OK;
737
738err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100739 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100740}
741
William Lallemandf528fff2017-11-23 19:43:17 +0100742/* This produces a sha1 hash of the concatenation of the first
743 * occurrence of the Host header followed by the path component if it
744 * begins with a slash ('/'). */
745int sha1_hosturi(struct http_txn *txn)
746{
747 struct hdr_ctx ctx;
748
749 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200750 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100751 char *path;
752 char *end;
753 trash = get_trash_chunk();
754
755 /* retrive the host */
756 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200757 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100758 return 0;
759 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
760
761 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200762 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200763 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100764 if (!path)
765 return 0;
766 chunk_strncat(trash, path, end - path);
767
768 /* hash everything */
769 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100771 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
772
773 return 1;
774}
775
776
William Lallemand41db4602017-10-30 11:15:51 +0100777
778enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
779 struct session *sess, struct stream *s, int flags)
780{
William Lallemand77c11972017-10-31 20:43:01 +0100781
William Lallemand77c11972017-10-31 20:43:01 +0100782 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100783 struct cache *cache = (struct cache *)rule->arg.act.p[0];
784
Willy Tarreau504455c2017-12-22 17:47:35 +0100785 check_request_for_cacheability(s, &s->req);
786 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
787 return ACT_RET_CONT;
788
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100789 if (!sha1_hosturi(s->txn))
790 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100791
Willy Tarreau504455c2017-12-22 17:47:35 +0100792 if (s->txn->flags & TX_CACHE_IGNORE)
793 return ACT_RET_CONT;
794
William Lallemanda400a3a2017-11-20 19:13:12 +0100795 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100796 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100797 if (res) {
798 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100799 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
800 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100801 s->target = &http_cache_applet.obj_type;
802 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
803 appctx->st0 = HTTP_CACHE_INIT;
804 appctx->rule = rule;
805 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200806 appctx->ctx.cache.next = NULL;
807 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100808 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100809 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100810 shctx_lock(shctx_ptr(cache));
811 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
812 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100813 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100814 }
815 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100816 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100817 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100818}
819
820
821enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
822 struct act_rule *rule, char **err)
823{
824 int cur_arg = *orig_arg;
825
826 rule->action = ACT_CUSTOM;
827 rule->action_ptr = http_action_req_cache_use;
828
829 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
830 memprintf(err, "expects a cache name");
831 return ACT_RET_PRS_ERR;
832 }
833
834 rule->arg.act.p[0] = strdup(args[cur_arg]);
835 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100836 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100837 err++;
838 goto err;
839 }
840
841 (*orig_arg)++;
842 return ACT_RET_PRS_OK;
843
844err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100845 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100846
847}
848
849int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
850{
851 int err_code = 0;
852
853 if (strcmp(args[0], "cache") == 0) { /* new cache section */
854
855 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100856 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
857 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100858 err_code |= ERR_ALERT | ERR_ABORT;
859 goto out;
860 }
861
862 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
863 err_code |= ERR_ABORT;
864 goto out;
865 }
866
867 if (tmp_cache_config == NULL) {
868 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
869 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100870 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100871 err_code |= ERR_ALERT | ERR_ABORT;
872 goto out;
873 }
874
875 strlcpy2(tmp_cache_config->id, args[1], 33);
876 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100877 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
878 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100879 err_code |= ERR_WARN;
880 }
William Lallemand49b44532017-11-24 18:53:43 +0100881 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100882 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200883 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100884 }
885 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200886 unsigned long int maxsize;
887 char *err;
William Lallemand41db4602017-10-30 11:15:51 +0100888
889 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
890 err_code |= ERR_ABORT;
891 goto out;
892 }
893
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200894 maxsize = strtoul(args[1], &err, 10);
895 if (err == args[1] || *err != '\0') {
896 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
897 file, linenum, args[1]);
898 err_code |= ERR_ABORT;
899 goto out;
900 }
901
902 if (maxsize > (UINT_MAX >> 20)) {
903 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
904 file, linenum, args[1], UINT_MAX >> 20);
905 err_code |= ERR_ABORT;
906 goto out;
907 }
908
William Lallemand41db4602017-10-30 11:15:51 +0100909 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200910 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +0100911 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +0100912 } else if (strcmp(args[0], "max-age") == 0) {
913 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
914 err_code |= ERR_ABORT;
915 goto out;
916 }
917
918 if (!*args[1]) {
919 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
920 file, linenum, args[0]);
921 err_code |= ERR_WARN;
922 }
923
924 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200925 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200926 unsigned int maxobjsz;
927 char *err;
928
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200929 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
930 err_code |= ERR_ABORT;
931 goto out;
932 }
933
934 if (!*args[1]) {
935 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
936 file, linenum, args[0]);
937 err_code |= ERR_WARN;
938 }
939
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200940 maxobjsz = strtoul(args[1], &err, 10);
941 if (err == args[1] || *err != '\0') {
942 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
943 file, linenum, args[1]);
944 err_code |= ERR_ABORT;
945 goto out;
946 }
947 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200948 }
949 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100950 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100951 err_code |= ERR_ALERT | ERR_FATAL;
952 goto out;
953 }
954out:
955 return err_code;
956}
957
958/* once the cache section is parsed */
959
960int cfg_post_parse_section_cache()
961{
962 struct shared_context *shctx;
963 int err_code = 0;
964 int ret_shctx;
965
966 if (tmp_cache_config) {
967 struct cache *cache;
968
969 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100970 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100971 err_code |= ERR_FATAL | ERR_ALERT;
972 goto out;
973 }
974
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200975 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200976 /* Default max. file size is a 256th of the cache size. */
977 tmp_cache_config->maxobjsz =
978 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200979 }
980 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
981 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
982 err_code |= ERR_FATAL | ERR_ALERT;
983 goto out;
984 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200985
986 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
987 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100988
Frédéric Lécaillebc584492018-10-25 20:18:59 +0200989 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100990 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100991 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100992 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100993 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100994
995 err_code |= ERR_FATAL | ERR_ALERT;
996 goto out;
997 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100998 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100999 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1000 cache = (struct cache *)shctx->data;
1001 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001002 LIST_ADDQ(&caches, &cache->list);
1003 }
1004out:
1005 free(tmp_cache_config);
1006 tmp_cache_config = NULL;
1007 return err_code;
1008
1009}
1010
1011/*
1012 * Resolve the cache name to a pointer once the file is completely read.
1013 */
1014int cfg_cache_postparser()
1015{
1016 struct act_rule *hresrule, *hrqrule;
1017 void *cache_ptr;
1018 struct cache *cache;
1019 struct proxy *curproxy = NULL;
1020 int err = 0;
1021 struct flt_conf *fconf;
1022
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001023 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +01001024
1025 /* resolve the http response cache name to a ptr in the action rule */
1026 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
1027 if (hresrule->action != ACT_CUSTOM ||
1028 hresrule->action_ptr != http_action_store_cache)
1029 continue;
1030
1031 cache_ptr = hresrule->arg.act.p[0];
1032
1033 list_for_each_entry(cache, &caches, list) {
1034 if (!strcmp(cache->id, cache_ptr)) {
1035 /* don't free there, it's still used in the filter conf */
1036 cache_ptr = cache;
1037 break;
1038 }
1039 }
1040
1041 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001042 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
1043 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001044 err++;
1045 }
1046
1047 hresrule->arg.act.p[0] = cache_ptr;
1048 }
1049
1050 /* resolve the http request cache name to a ptr in the action rule */
1051 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
1052 if (hrqrule->action != ACT_CUSTOM ||
1053 hrqrule->action_ptr != http_action_req_cache_use)
1054 continue;
1055
1056 cache_ptr = hrqrule->arg.act.p[0];
1057
1058 list_for_each_entry(cache, &caches, list) {
1059 if (!strcmp(cache->id, cache_ptr)) {
1060 free(cache_ptr);
1061 cache_ptr = cache;
1062 break;
1063 }
1064 }
1065
1066 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001067 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1068 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001069 err++;
1070 }
1071
1072 hrqrule->arg.act.p[0] = cache_ptr;
1073 }
1074
1075 /* resolve the cache name to a ptr in the filter config */
1076 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1077
William Lallemand9c54c532017-11-02 16:38:42 +01001078 if (fconf->id != cache_store_flt_id)
1079 continue;
1080
William Lallemand41db4602017-10-30 11:15:51 +01001081 cache_ptr = fconf->conf;
1082
1083 list_for_each_entry(cache, &caches, list) {
1084 if (!strcmp(cache->id, cache_ptr)) {
1085 /* there can be only one filter per cache, so we free it there */
1086 free(cache_ptr);
1087 cache_ptr = cache;
1088 break;
1089 }
1090 }
1091
1092 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001093 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1094 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001095 err++;
1096 }
1097 fconf->conf = cache_ptr;
1098 }
1099 }
1100 return err;
1101}
1102
1103
1104struct flt_ops cache_ops = {
1105 .init = cache_store_init,
1106
William Lallemand4da3f8a2017-10-31 14:33:34 +01001107 /* Handle channels activity */
1108 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001109 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001110
1111 /* Filter HTTP requests and responses */
1112 .http_headers = cache_store_http_headers,
1113 .http_end = cache_store_http_end,
1114
1115 .http_forward_data = cache_store_http_forward_data,
1116
William Lallemand41db4602017-10-30 11:15:51 +01001117};
1118
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001119static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001120{
1121 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1122 return 1;
1123
1124 return 0;
1125}
1126
1127static int cli_io_handler_show_cache(struct appctx *appctx)
1128{
1129 struct cache* cache = appctx->ctx.cli.p0;
1130 struct stream_interface *si = appctx->owner;
1131
William Lallemand1f49a362017-11-21 20:01:26 +01001132 if (cache == NULL) {
1133 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1134 }
1135
1136 list_for_each_entry_from(cache, &caches, list) {
1137 struct eb32_node *node = NULL;
1138 unsigned int next_key;
1139 struct cache_entry *entry;
1140
William Lallemand1f49a362017-11-21 20:01:26 +01001141 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001142 if (!next_key) {
1143 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1144 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001145 si_cant_put(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001146 return 0;
1147 }
1148 }
William Lallemand1f49a362017-11-21 20:01:26 +01001149
1150 appctx->ctx.cli.p0 = cache;
1151
1152 while (1) {
1153
1154 shctx_lock(shctx_ptr(cache));
1155 node = eb32_lookup_ge(&cache->entries, next_key);
1156 if (!node) {
1157 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001158 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001159 break;
1160 }
1161
1162 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001163 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 Lallemand1f49a362017-11-21 20:01:26 +01001164
1165 next_key = node->key + 1;
1166 appctx->ctx.cli.i0 = next_key;
1167
1168 shctx_unlock(shctx_ptr(cache));
1169
1170 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001171 si_cant_put(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001172 return 0;
1173 }
1174 }
1175
1176 }
1177
1178 return 1;
1179
1180}
1181
1182static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001183 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1184 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001185}};
1186
1187
William Lallemand41db4602017-10-30 11:15:51 +01001188static struct action_kw_list http_res_actions = {
1189 .kw = {
1190 { "cache-store", parse_cache_store },
1191 { NULL, NULL }
1192 }
1193};
1194
1195static struct action_kw_list http_req_actions = {
1196 .kw = {
1197 { "cache-use", parse_cache_use },
1198 { NULL, NULL }
1199 }
1200};
1201
1202struct applet http_cache_applet = {
1203 .obj_type = OBJ_TYPE_APPLET,
1204 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001205 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001206 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001207};
1208
1209__attribute__((constructor))
1210static void __cache_init(void)
1211{
1212 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1213 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001214 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001215 http_res_keywords_register(&http_res_actions);
1216 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001217 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001218}
1219