blob: d24115431a852895c45e16954cd3c5f3a363a036 [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écaillea2219f52018-10-22 16:59:13 +020053 unsigned int maxobjsz; /* max-object-size */
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 */
68 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010069 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010070 unsigned char data[0];
71};
72
73#define CACHE_BLOCKSIZE 1024
74
75static struct list caches = LIST_HEAD_INIT(caches);
76static struct cache *tmp_cache_config = NULL;
77
William Lallemandf528fff2017-11-23 19:43:17 +010078struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010079{
80 struct eb32_node *node;
81 struct cache_entry *entry;
82
William Lallemandf528fff2017-11-23 19:43:17 +010083 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010084 if (!node)
85 return NULL;
86
87 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010088
89 /* if that's not the right node */
90 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
91 return NULL;
92
William Lallemand08727662017-11-21 20:01:27 +010093 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010094 return entry;
William Lallemand08727662017-11-21 20:01:27 +010095 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010096 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +010097 entry->eb.key = 0;
98 }
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 return NULL;
100
101}
102
103static 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 Lallemand77c11972017-10-31 20:43:01 +0100108static 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 Lallemand41db4602017-10-30 11:15:51 +0100115static int
116cache_store_init(struct proxy *px, struct flt_conf *f1conf)
117{
118 return 0;
119}
120
William Lallemand4da3f8a2017-10-31 14:33:34 +0100121static int
122cache_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 Tarreaubafbe012017-11-24 17:34:44 +0100130 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100131 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
144static int
William Lallemand49dc0482017-11-24 14:33:54 +0100145cache_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 Tarreaubafbe012017-11-24 17:34:44 +0100165 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100166 filter->ctx = NULL;
167 }
168
169 return 1;
170}
171
172
173static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100174cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
175{
176 struct cache_st *st = filter->ctx;
177
William Lallemand4da3f8a2017-10-31 14:33:34 +0100178 if (!(msg->chn->flags & CF_ISRESP) || !st)
179 return 1;
180
William Lallemand9d5f54d2017-11-14 14:39:22 +0100181 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100182
183 return 1;
184}
185
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200186static 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 Lallemand4da3f8a2017-10-31 14:33:34 +0100200static int
201cache_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écaille8df65ae2018-10-22 18:01:48 +0200208 ret = 0;
209
William Lallemand4da3f8a2017-10-31 14:33:34 +0100210 /*
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 Lallemand10935bc2017-11-14 14:39:23 +0100221 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100222 /* Forward part of headers */
223 ret = len;
224 st->hdrs_len -= len;
225 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100226 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100227 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100228 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200229 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écaille8df65ae2018-10-22 18:01:48 +0200234 shctx_lock(shctx);
235 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
236 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100237 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200238 disable_cache_entry(st, filter, shctx);
239 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100240 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200241 shctx_unlock(shctx);
242
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200243 /* Skip remaining headers to fill the cache */
244 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200245 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 Lallemand4da3f8a2017-10-31 14:33:34 +0100253 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200254 else
255 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100256 }
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
265static int
266cache_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 Lallemand08727662017-11-21 20:01:27 +0100285 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
286 object->eb.key = 0;
287 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100288 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100289 shctx_row_dec_hot(shctx, st->first_block);
290 shctx_unlock(shctx);
291
292 }
293 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100294 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100295 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 */
306char *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 Lallemand49b44532017-11-24 18:53:43 +0100349int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100350{
351 struct http_txn *txn = s->txn;
352 struct hdr_ctx ctx;
353
354 int smaxage = -1;
355 int maxage = -1;
356
357
William Lallemand4da3f8a2017-10-31 14:33:34 +0100358 ctx.idx = 0;
359
360 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200361 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100362 char *directive = ctx.line + ctx.val;
363 char *value;
364
365 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
366 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100368
369 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
370 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200371 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100372 }
373
374 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
375 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200376 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100377
378 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
379 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200380 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100381 }
382 }
383
384 /* TODO: Expires - Data */
385
386
387 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100388 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100389
390 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100391 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392
William Lallemand49b44532017-11-24 18:53:43 +0100393 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100394
395}
396
397
William Lallemanda400a3a2017-11-20 19:13:12 +0100398static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
399{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200400 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 Lallemanda400a3a2017-11-20 19:13:12 +0100405}
406
William Lallemand41db4602017-10-30 11:15:51 +0100407/*
408 * This fonction will store the headers of the response in a buffer and then
409 * register a filter to store the data
410 */
411enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
412 struct session *sess, struct stream *s, int flags)
413{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100414 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 Lallemand49b44532017-11-24 18:53:43 +0100419 struct cache *cache = (struct cache *)rule->arg.act.p[0];
420 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100421 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écaillea2219f52018-10-22 16:59:13 +0200434 /* 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 Lallemand4da3f8a2017-10-31 14:33:34 +0100439 /* 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 Tarreau178b9872018-06-19 07:13:36 +0200449 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100450 goto out;
451
Willy Tarreaufaf29092017-12-21 15:59:17 +0100452 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100453
Willy Tarreaud4569d12017-12-22 18:03:04 +0100454 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100455 goto out;
456
William Lallemand4da3f8a2017-10-31 14:33:34 +0100457 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200458 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100459 if (!first) {
460 shctx_unlock(shctx);
461 goto out;
462 }
463 shctx_unlock(shctx);
464
Willy Tarreau1093a452018-04-06 19:02:25 +0200465 /* 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 Lallemand4da3f8a2017-10-31 14:33:34 +0100472 /* reserve space for the cache_entry structure */
473 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200474 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475 /* 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écaille0bec8072018-10-22 17:55:57 +0200483 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484 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 Tarreauc9bd34c2017-12-22 17:42:46 +0100493 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494
495 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496
William Lallemandf528fff2017-11-23 19:43:17 +0100497 object->eb.key = (*(unsigned int *)&txn->cache_hash);
498 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100499 /* Insert the node later on caching success */
500
501 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100502
503 old = entry_exist(cache, txn->cache_hash);
504 if (old) {
505 eb32_delete(&old->eb);
506 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507 }
508 shctx_unlock(shctx);
509
510 /* store latest value and expiration time */
511 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100512 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513 }
514 return ACT_RET_CONT;
515 }
516 }
517 }
518
519out:
520 /* if does not cache */
521 if (first) {
522 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100523 first->len = 0;
524 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 shctx_row_dec_hot(shctx, first);
526 shctx_unlock(shctx);
527 }
528
William Lallemand41db4602017-10-30 11:15:51 +0100529 return ACT_RET_CONT;
530}
531
William Lallemand77c11972017-10-31 20:43:01 +0100532#define HTTP_CACHE_INIT 0
533#define HTTP_CACHE_FWD 1
534#define HTTP_CACHE_END 2
535
William Lallemandecb73b12017-11-24 14:33:55 +0100536static 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écaille8df65ae2018-10-22 18:01:48 +0200547static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100548{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200549 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100550 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 Lallemand77c11972017-10-31 20:43:01 +0100553 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200554 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
594static 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 Lallemand77c11972017-10-31 20:43:01 +0100599 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200600 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100601
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 Tarreauc9fa0482018-07-10 17:43:27 +0200606 if (res->buf.size == 0) {
William Lallemand77c11972017-10-31 20:43:01 +0100607 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écaille8df65ae2018-10-22 18:01:48 +0200616 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100617
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200618 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 Lallemand77c11972017-10-31 20:43:01 +0100630 }
William Lallemand77c11972017-10-31 20:43:01 +0100631 }
632
633 if (appctx->st0 == HTTP_CACHE_FWD) {
634 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200635 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 +0100636 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);
642out:
643 ;
644}
645
William Lallemand41db4602017-10-30 11:15:51 +0100646enum 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 Faulet767a84b2017-11-24 16:50:31 +0100672 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100673 err++;
674 goto err;
675 }
676 /* register a filter to fill the cache buffer */
677 fconf = calloc(1, sizeof(*fconf));
678 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100679 ha_alert("config: %s '%s': out of memory\n",
680 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100681 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
693err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100694 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100695}
696
William Lallemandf528fff2017-11-23 19:43:17 +0100697/* 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 ('/'). */
700int sha1_hosturi(struct http_txn *txn)
701{
702 struct hdr_ctx ctx;
703
704 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200705 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100706 char *path;
707 char *end;
708 trash = get_trash_chunk();
709
710 /* retrive the host */
711 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200712 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100713 return 0;
714 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
715
716 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200717 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200718 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100719 if (!path)
720 return 0;
721 chunk_strncat(trash, path, end - path);
722
723 /* hash everything */
724 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100726 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
727
728 return 1;
729}
730
731
William Lallemand41db4602017-10-30 11:15:51 +0100732
733enum 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 Lallemand77c11972017-10-31 20:43:01 +0100736
William Lallemand77c11972017-10-31 20:43:01 +0100737 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100738 struct cache *cache = (struct cache *)rule->arg.act.p[0];
739
Willy Tarreau504455c2017-12-22 17:47:35 +0100740 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 Tarreau7704b1e2017-12-22 16:32:43 +0100744 if (!sha1_hosturi(s->txn))
745 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100746
Willy Tarreau504455c2017-12-22 17:47:35 +0100747 if (s->txn->flags & TX_CACHE_IGNORE)
748 return ACT_RET_CONT;
749
William Lallemanda400a3a2017-11-20 19:13:12 +0100750 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100751 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100752 if (res) {
753 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100754 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
755 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100756 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écaille8df65ae2018-10-22 18:01:48 +0200761 appctx->ctx.cache.next = NULL;
762 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100763 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100764 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100765 shctx_lock(shctx_ptr(cache));
766 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
767 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100768 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100769 }
770 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100771 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100772 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100773}
774
775
776enum 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 Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100792 err++;
793 goto err;
794 }
795
796 (*orig_arg)++;
797 return ACT_RET_PRS_OK;
798
799err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100800 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100801
802}
803
804int 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 Faulet767a84b2017-11-24 16:50:31 +0100811 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
812 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100813 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 Faulet767a84b2017-11-24 16:50:31 +0100825 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100826 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 Faulet767a84b2017-11-24 16:50:31 +0100832 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
833 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100834 err_code |= ERR_WARN;
835 }
William Lallemand49b44532017-11-24 18:53:43 +0100836 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100837 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200838 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100839 }
840 } else if (strcmp(args[0], "total-max-size") == 0) {
841 int maxsize;
842
843 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
844 err_code |= ERR_ABORT;
845 goto out;
846 }
847
848 /* size in megabytes */
849 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
850 tmp_cache_config->maxblocks = maxsize;
851
William Lallemand49b44532017-11-24 18:53:43 +0100852 } else if (strcmp(args[0], "max-age") == 0) {
853 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
854 err_code |= ERR_ABORT;
855 goto out;
856 }
857
858 if (!*args[1]) {
859 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
860 file, linenum, args[0]);
861 err_code |= ERR_WARN;
862 }
863
864 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200865 } else if (strcmp(args[0], "max-object-size") == 0) {
866 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
867 err_code |= ERR_ABORT;
868 goto out;
869 }
870
871 if (!*args[1]) {
872 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
873 file, linenum, args[0]);
874 err_code |= ERR_WARN;
875 }
876
877 tmp_cache_config->maxobjsz = atoi(args[1]);
878 }
879 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100880 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100881 err_code |= ERR_ALERT | ERR_FATAL;
882 goto out;
883 }
884out:
885 return err_code;
886}
887
888/* once the cache section is parsed */
889
890int cfg_post_parse_section_cache()
891{
892 struct shared_context *shctx;
893 int err_code = 0;
894 int ret_shctx;
895
896 if (tmp_cache_config) {
897 struct cache *cache;
898
899 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100900 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100901 err_code |= ERR_FATAL | ERR_ALERT;
902 goto out;
903 }
904
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200905 if (!tmp_cache_config->maxobjsz)
906 /* Default max. file size is a 256th of the cache size. */
907 tmp_cache_config->maxobjsz =
908 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
909
910 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
911 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100912
William Lallemand41db4602017-10-30 11:15:51 +0100913 if (ret_shctx < 0) {
914 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100915 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100916 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100917 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100918
919 err_code |= ERR_FATAL | ERR_ALERT;
920 goto out;
921 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100922 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100923 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
924 cache = (struct cache *)shctx->data;
925 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100926 LIST_ADDQ(&caches, &cache->list);
927 }
928out:
929 free(tmp_cache_config);
930 tmp_cache_config = NULL;
931 return err_code;
932
933}
934
935/*
936 * Resolve the cache name to a pointer once the file is completely read.
937 */
938int cfg_cache_postparser()
939{
940 struct act_rule *hresrule, *hrqrule;
941 void *cache_ptr;
942 struct cache *cache;
943 struct proxy *curproxy = NULL;
944 int err = 0;
945 struct flt_conf *fconf;
946
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100947 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +0100948
949 /* resolve the http response cache name to a ptr in the action rule */
950 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
951 if (hresrule->action != ACT_CUSTOM ||
952 hresrule->action_ptr != http_action_store_cache)
953 continue;
954
955 cache_ptr = hresrule->arg.act.p[0];
956
957 list_for_each_entry(cache, &caches, list) {
958 if (!strcmp(cache->id, cache_ptr)) {
959 /* don't free there, it's still used in the filter conf */
960 cache_ptr = cache;
961 break;
962 }
963 }
964
965 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100966 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
967 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100968 err++;
969 }
970
971 hresrule->arg.act.p[0] = cache_ptr;
972 }
973
974 /* resolve the http request cache name to a ptr in the action rule */
975 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
976 if (hrqrule->action != ACT_CUSTOM ||
977 hrqrule->action_ptr != http_action_req_cache_use)
978 continue;
979
980 cache_ptr = hrqrule->arg.act.p[0];
981
982 list_for_each_entry(cache, &caches, list) {
983 if (!strcmp(cache->id, cache_ptr)) {
984 free(cache_ptr);
985 cache_ptr = cache;
986 break;
987 }
988 }
989
990 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100991 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
992 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100993 err++;
994 }
995
996 hrqrule->arg.act.p[0] = cache_ptr;
997 }
998
999 /* resolve the cache name to a ptr in the filter config */
1000 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1001
William Lallemand9c54c532017-11-02 16:38:42 +01001002 if (fconf->id != cache_store_flt_id)
1003 continue;
1004
William Lallemand41db4602017-10-30 11:15:51 +01001005 cache_ptr = fconf->conf;
1006
1007 list_for_each_entry(cache, &caches, list) {
1008 if (!strcmp(cache->id, cache_ptr)) {
1009 /* there can be only one filter per cache, so we free it there */
1010 free(cache_ptr);
1011 cache_ptr = cache;
1012 break;
1013 }
1014 }
1015
1016 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001017 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1018 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001019 err++;
1020 }
1021 fconf->conf = cache_ptr;
1022 }
1023 }
1024 return err;
1025}
1026
1027
1028struct flt_ops cache_ops = {
1029 .init = cache_store_init,
1030
William Lallemand4da3f8a2017-10-31 14:33:34 +01001031 /* Handle channels activity */
1032 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001033 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001034
1035 /* Filter HTTP requests and responses */
1036 .http_headers = cache_store_http_headers,
1037 .http_end = cache_store_http_end,
1038
1039 .http_forward_data = cache_store_http_forward_data,
1040
William Lallemand41db4602017-10-30 11:15:51 +01001041};
1042
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001043static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001044{
1045 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1046 return 1;
1047
1048 return 0;
1049}
1050
1051static int cli_io_handler_show_cache(struct appctx *appctx)
1052{
1053 struct cache* cache = appctx->ctx.cli.p0;
1054 struct stream_interface *si = appctx->owner;
1055
William Lallemand1f49a362017-11-21 20:01:26 +01001056 if (cache == NULL) {
1057 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1058 }
1059
1060 list_for_each_entry_from(cache, &caches, list) {
1061 struct eb32_node *node = NULL;
1062 unsigned int next_key;
1063 struct cache_entry *entry;
1064
William Lallemand1f49a362017-11-21 20:01:26 +01001065 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001066 if (!next_key) {
1067 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1068 if (ci_putchk(si_ic(si), &trash) == -1) {
1069 si_applet_cant_put(si);
1070 return 0;
1071 }
1072 }
William Lallemand1f49a362017-11-21 20:01:26 +01001073
1074 appctx->ctx.cli.p0 = cache;
1075
1076 while (1) {
1077
1078 shctx_lock(shctx_ptr(cache));
1079 node = eb32_lookup_ge(&cache->entries, next_key);
1080 if (!node) {
1081 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001082 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001083 break;
1084 }
1085
1086 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001087 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 +01001088
1089 next_key = node->key + 1;
1090 appctx->ctx.cli.i0 = next_key;
1091
1092 shctx_unlock(shctx_ptr(cache));
1093
1094 if (ci_putchk(si_ic(si), &trash) == -1) {
1095 si_applet_cant_put(si);
1096 return 0;
1097 }
1098 }
1099
1100 }
1101
1102 return 1;
1103
1104}
1105
1106static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001107 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1108 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001109}};
1110
1111
William Lallemand41db4602017-10-30 11:15:51 +01001112static struct action_kw_list http_res_actions = {
1113 .kw = {
1114 { "cache-store", parse_cache_store },
1115 { NULL, NULL }
1116 }
1117};
1118
1119static struct action_kw_list http_req_actions = {
1120 .kw = {
1121 { "cache-use", parse_cache_use },
1122 { NULL, NULL }
1123 }
1124};
1125
1126struct applet http_cache_applet = {
1127 .obj_type = OBJ_TYPE_APPLET,
1128 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001129 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001130 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001131};
1132
1133__attribute__((constructor))
1134static void __cache_init(void)
1135{
1136 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1137 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001138 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001139 http_res_keywords_register(&http_res_actions);
1140 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001141 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001142}
1143