blob: 8dd460681eef24fc1c4682ab8e43924debd338c7 [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>
14
15#include <proto/channel.h>
16#include <proto/proxy.h>
17#include <proto/hdr_idx.h>
18#include <proto/filters.h>
19#include <proto/proto_http.h>
20#include <proto/log.h>
21#include <proto/stream.h>
22#include <proto/stream_interface.h>
23#include <proto/shctx.h>
24
25#include <types/action.h>
26#include <types/filters.h>
27#include <types/proxy.h>
28#include <types/shctx.h>
29
30#include <common/cfgparse.h>
31#include <common/hash.h>
32
33/* flt_cache_store */
34
35static const char *cache_store_flt_id = "cache store filter";
36
William Lallemand4da3f8a2017-10-31 14:33:34 +010037static struct pool_head *pool2_cache_st = NULL;
38
William Lallemand41db4602017-10-30 11:15:51 +010039struct applet http_cache_applet;
40
41struct flt_ops cache_ops;
42
43struct cache {
44 char id[33]; /* cache name */
45 unsigned int maxage; /* max-age */
46 unsigned int maxblocks;
47 struct list list; /* cache linked list */
48 struct eb_root entries; /* head of cache entries based on keys */
49};
50
51/*
52 * cache ctx for filters
53 */
54struct cache_st {
55 int hdrs_len;
56 struct shared_block *first_block;
57};
58
59struct cache_entry {
60 unsigned int latest_validation; /* latest validation date */
61 unsigned int expire; /* expiration date */
62 struct eb32_node eb; /* ebtree node used to hold the cache object */
63 unsigned char data[0];
64};
65
66#define CACHE_BLOCKSIZE 1024
67
68static struct list caches = LIST_HEAD_INIT(caches);
69static struct cache *tmp_cache_config = NULL;
70
William Lallemand4da3f8a2017-10-31 14:33:34 +010071struct cache_entry *entry_exist(struct cache *cache, struct cache_entry *new_entry)
72{
73 struct eb32_node *node;
74 struct cache_entry *entry;
75
76 node = eb32_lookup(&cache->entries, new_entry->eb.key);
77 if (!node)
78 return NULL;
79
80 entry = eb32_entry(node, struct cache_entry, eb);
81 if (entry->expire > now.tv_sec)
82 return entry;
83 else
84 eb32_delete(node);
85 return NULL;
86
87}
88
89static inline struct shared_context *shctx_ptr(struct cache *cache)
90{
91 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
92}
93
William Lallemand77c11972017-10-31 20:43:01 +010094static inline struct shared_block *block_ptr(struct cache_entry *entry)
95{
96 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
97}
98
99
100
William Lallemand41db4602017-10-30 11:15:51 +0100101static int
102cache_store_init(struct proxy *px, struct flt_conf *f1conf)
103{
104 return 0;
105}
106
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107static int
108cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
109{
110 if (!(chn->flags & CF_ISRESP))
111 return 1;
112
113 if (filter->ctx == NULL) {
114 struct cache_st *st;
115
116 st = pool_alloc_dirty(pool2_cache_st);
117 if (st == NULL)
118 return -1;
119
120 st->hdrs_len = 0;
121 st->first_block = NULL;
122 filter->ctx = st;
123 }
124
125 register_data_filter(s, chn, filter);
126
127 return 1;
128}
129
130static int
131cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
132{
133 struct cache_st *st = filter->ctx;
134
135 /* end of headers, exclude the final \r\n allow to forward the final
136 * \r\n in the data filter */
137 if (!(msg->chn->flags & CF_ISRESP) || !st)
138 return 1;
139
William Lallemand9d5f54d2017-11-14 14:39:22 +0100140 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100141
142 return 1;
143}
144
145static int
146cache_store_http_forward_data(struct stream *s, struct filter *filter,
147 struct http_msg *msg, unsigned int len)
148{
149 struct cache_st *st = filter->ctx;
150 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
151 int ret;
152
153 /*
154 * We need to skip the HTTP headers first, because we saved them in the
155 * http-response action.
156 */
157 if (!(msg->chn->flags & CF_ISRESP) || !st)
158 return len;
159
160 if (!len) {
161 /* Nothing to foward */
162 ret = len;
163 }
William Lallemand10935bc2017-11-14 14:39:23 +0100164 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100165 /* Forward part of headers */
166 ret = len;
167 st->hdrs_len -= len;
168 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100169 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100170 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100171 if (filter->ctx && st->first_block) {
172 /* disable buffering if too much data (never greater than a buffer size */
William Lallemand10935bc2017-11-14 14:39:23 +0100173 if (len - st->hdrs_len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) {
William Lallemande1533f52017-11-14 14:39:24 +0100174 disable_cache:
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100175 filter->ctx = NULL; /* disable cache */
176 shctx_lock(shctx);
177 shctx_row_dec_hot(shctx, st->first_block);
178 shctx_unlock(shctx);
179 pool_free2(pool2_cache_st, st);
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100180 } else {
William Lallemand10935bc2017-11-14 14:39:23 +0100181 /* Skip remaining headers to fill the cache */
182 b_adv(msg->chn->buf, st->hdrs_len);
183 ret = shctx_row_data_append(shctx,
184 st->first_block,
185 (unsigned char *)bi_ptr(msg->chn->buf),
186 MIN(bi_contig_data(msg->chn->buf), len - st->hdrs_len));
187 /* Rewind the buffer to forward all data */
188 b_rew(msg->chn->buf, st->hdrs_len);
William Lallemande1533f52017-11-14 14:39:24 +0100189 if (ret)
190 goto disable_cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100191 }
192 }
William Lallemand10935bc2017-11-14 14:39:23 +0100193 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100194 }
195
196 if ((ret != len) ||
197 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
198 task_wakeup(s->task, TASK_WOKEN_MSG);
199
200 return ret;
201}
202
203static int
204cache_store_http_end(struct stream *s, struct filter *filter,
205 struct http_msg *msg)
206{
207 struct cache_st *st = filter->ctx;
208 struct cache *cache = filter->config->conf;
209 struct shared_context *shctx = shctx_ptr(cache);
210 struct cache_entry *object;
211
212 if (!(msg->chn->flags & CF_ISRESP))
213 return 1;
214
215 if (st && st->first_block) {
216
217 object = (struct cache_entry *)st->first_block->data;
218
219 /* does not need to test if the insertion worked, if it
220 * doesn't, the blocks will be reused anyway */
221
222 shctx_lock(shctx);
223 eb32_insert(&cache->entries, &object->eb);
224 shctx_unlock(shctx);
225
226 /* remove from the hotlist */
227 shctx_lock(shctx);
228 shctx_row_dec_hot(shctx, st->first_block);
229 shctx_unlock(shctx);
230
231 }
232 if (st) {
233 pool_free2(pool2_cache_st, st);
234 filter->ctx = NULL;
235 }
236
237 return 1;
238}
239
240 /*
241 * This intends to be used when checking HTTP headers for some
242 * word=value directive. Return a pointer to the first character of value, if
243 * the word was not found or if there wasn't any value assigned ot it return NULL
244 */
245char *directive_value(const char *sample, int slen, const char *word, int wlen)
246{
247 int st = 0;
248
249 if (slen < wlen)
250 return 0;
251
252 while (wlen) {
253 char c = *sample ^ *word;
254 if (c && c != ('A' ^ 'a'))
255 return NULL;
256 sample++;
257 word++;
258 slen--;
259 wlen--;
260 }
261
262 while (slen) {
263 if (st == 0) {
264 if (*sample != '=')
265 return NULL;
266 sample++;
267 slen--;
268 st = 1;
269 continue;
270 } else {
271 return (char *)sample;
272 }
273 }
274
275 return NULL;
276}
277
278/*
279 * Return the maxage in seconds of an HTTP response.
280 * Compute the maxage using either:
281 * - the assigned max-age of the cache
282 * - the s-maxage directive
283 * - the max-age directive
284 * - (Expires - Data) headers
285 * - the default-max-age of the cache
286 *
287 */
288int http_calc_maxage(struct stream *s)
289{
290 struct http_txn *txn = s->txn;
291 struct hdr_ctx ctx;
292
293 int smaxage = -1;
294 int maxage = -1;
295
296
297 /* TODO: forced maxage configuration */
298
299 ctx.idx = 0;
300
301 /* loop on the Cache-Control values */
302 while (http_find_header2("Cache-Control", 13, s->res.buf->p, &txn->hdr_idx, &ctx)) {
303 char *directive = ctx.line + ctx.val;
304 char *value;
305
306 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
307 if (value) {
308 struct chunk *chk = get_trash_chunk();
309
310 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
311 chunk_strncat(chk, "", 1);
312 maxage = atoi(chk->str);
313 }
314
315 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
316 if (value) {
317 struct chunk *chk = get_trash_chunk();
318
319 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
320 chunk_strncat(chk, "", 1);
321 smaxage = atoi(chk->str);
322 }
323 }
324
325 /* TODO: Expires - Data */
326
327
328 if (smaxage > 0)
329 return smaxage;
330
331 if (maxage > 0)
332 return maxage;
333
334 /* TODO: return default value */
335
336 return 60;
337
338}
339
340
William Lallemanda400a3a2017-11-20 19:13:12 +0100341static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
342{
343 if (first == block) {
344 struct cache_entry *object = (struct cache_entry *)first->data;
345 eb32_delete(&object->eb);
346 }
347}
348
William Lallemand41db4602017-10-30 11:15:51 +0100349/*
350 * This fonction will store the headers of the response in a buffer and then
351 * register a filter to store the data
352 */
353enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
354 struct session *sess, struct stream *s, int flags)
355{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100356 struct http_txn *txn = s->txn;
357 struct http_msg *msg = &txn->rsp;
358 struct filter *filter;
359 struct hdr_ctx ctx;
360 struct shared_block *first = NULL;
361 struct shared_context *shctx = shctx_ptr((struct cache *)rule->arg.act.p[0]);
362 struct cache_entry *object;
363
364
365 /* Don't cache if the response came from a cache */
366 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
367 s->target == &http_cache_applet.obj_type) {
368 goto out;
369 }
370
371 /* cache only HTTP/1.1 */
372 if (!(txn->req.flags & HTTP_MSGF_VER_11))
373 goto out;
374
William Lallemand18f133a2017-11-08 11:25:15 +0100375 /* does not cache if Content-Length unknown */
376 if (!(msg->flags & HTTP_MSGF_CNT_LEN))
377 goto out;
378
William Lallemand4da3f8a2017-10-31 14:33:34 +0100379 /* cache only GET method */
380 if (txn->meth != HTTP_METH_GET)
381 goto out;
382
383 /* cache only 200 status code */
384 if (txn->status != 200)
385 goto out;
386
387 /* Does not manage Vary at the moment. We will need a secondary key later for that */
388 ctx.idx = 0;
389 if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx))
390 goto out;
391
392 /* we need to put this flag before using check_response_for_cacheability */
393 txn->flags |= TX_CACHEABLE;
394
395 if (txn->status != 101)
396 check_response_for_cacheability(s, &s->res);
397
398 if (!(txn->flags & TX_CACHEABLE))
399 goto out;
400
William Lallemand9d5f54d2017-11-14 14:39:22 +0100401 if ((msg->sov + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100402 goto out;
403
404 shctx_lock(shctx);
405
William Lallemand9d5f54d2017-11-14 14:39:22 +0100406 first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->sov + msg->body_len);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100407 if (!first) {
408 shctx_unlock(shctx);
409 goto out;
410 }
411 shctx_unlock(shctx);
412
413 /* reserve space for the cache_entry structure */
414 first->len = sizeof(struct cache_entry);
415
416 /* cache the headers in a http action because it allows to chose what
417 * to cache, for example you might want to cache a response before
418 * modifying some HTTP headers, or on the contrary after modifying
419 * those headers.
420 */
421
422 /* does not need to be locked because it's in the "hot" list,
423 * copy the headers */
William Lallemand9d5f54d2017-11-14 14:39:22 +0100424 if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100425 goto out;
426
427 /* register the buffer in the filter ctx for filling it with data*/
428 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
429 list_for_each_entry(filter, &s->strm_flt.filters, list) {
430 if (filter->config->id == cache_store_flt_id &&
431 filter->config->conf == rule->arg.act.p[0]) {
432 if (filter->ctx) {
433 struct cache_st *cache_ctx = filter->ctx;
434
435 cache_ctx->first_block = first;
436 object = (struct cache_entry *)first->data;
437
438 object->eb.key = hash_djb2(txn->uri, strlen(txn->uri));
439 /* Insert the node later on caching success */
440
441 shctx_lock(shctx);
442 if (entry_exist((struct cache *)rule->arg.act.p[0], object)) {
443 shctx_unlock(shctx);
444 if (filter->ctx) {
445 pool_free2(pool2_cache_st, filter->ctx);
446 filter->ctx = NULL;
447 }
448 goto out;
449 }
450 shctx_unlock(shctx);
451
452 /* store latest value and expiration time */
453 object->latest_validation = now.tv_sec;
454 object->expire = now.tv_sec + http_calc_maxage(s);
455
456 }
457 return ACT_RET_CONT;
458 }
459 }
460 }
461
462out:
463 /* if does not cache */
464 if (first) {
465 shctx_lock(shctx);
466 shctx_row_dec_hot(shctx, first);
467 shctx_unlock(shctx);
468 }
469
William Lallemand41db4602017-10-30 11:15:51 +0100470 return ACT_RET_CONT;
471}
472
William Lallemand77c11972017-10-31 20:43:01 +0100473#define HTTP_CACHE_INIT 0
474#define HTTP_CACHE_FWD 1
475#define HTTP_CACHE_END 2
476
477static void http_cache_io_handler(struct appctx *appctx)
478{
479 struct stream_interface *si = appctx->owner;
480 struct channel *res = si_ic(si);
481 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
482 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
483 struct shared_context *shctx = shctx_ptr(cache);
484 struct shared_block *first = block_ptr(cache_ptr);
485
486 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
487 goto out;
488
489 /* Check if the input buffer is avalaible. */
490 if (res->buf->size == 0) {
491 si_applet_cant_put(si);
492 goto out;
493 }
494
495 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
496 appctx->st0 = HTTP_CACHE_END;
497
498 /* buffer are aligned there, should be fine */
499 if (appctx->st0 == HTTP_CACHE_INIT) {
500 int len = first->len - sizeof(struct cache_entry);
501 if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) {
502 fprintf(stderr, "cache error too big: %d\n", first->len - (int)sizeof(struct cache_entry));
503 si_applet_cant_put(si);
504 goto out;
505 }
506 res->buf->i += len;
507 res->total += len;
508 appctx->st0 = HTTP_CACHE_FWD;
509 }
510
511 if (appctx->st0 == HTTP_CACHE_FWD) {
512 /* eat the whole request */
513 co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code
514 res->flags |= CF_READ_NULL;
515 si_shutr(si);
516 }
517
518 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
519 si_shutw(si);
520out:
521 ;
522}
523
William Lallemand41db4602017-10-30 11:15:51 +0100524enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
525 struct act_rule *rule, char **err)
526{
527 struct flt_conf *fconf;
528 int cur_arg = *orig_arg;
529 rule->action = ACT_CUSTOM;
530 rule->action_ptr = http_action_store_cache;
531
532 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
533 memprintf(err, "expects a cache name");
534 return ACT_RET_PRS_ERR;
535 }
536
537 /* check if a cache filter was already registered with this cache
538 * name, if that's the case, must use it. */
539 list_for_each_entry(fconf, &proxy->filter_configs, list) {
540 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
541 rule->arg.act.p[0] = fconf->conf;
542 (*orig_arg)++;
543 /* filter already registered */
544 return ACT_RET_PRS_OK;
545 }
546 }
547
548 rule->arg.act.p[0] = strdup(args[cur_arg]);
549 if (!rule->arg.act.p[0]) {
550 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
551 err++;
552 goto err;
553 }
554 /* register a filter to fill the cache buffer */
555 fconf = calloc(1, sizeof(*fconf));
556 if (!fconf) {
557 Alert("config: %s '%s': out of memory\n",
558 proxy_type_str(proxy), proxy->id);
559 err++;
560 goto err;
561 }
562 fconf->id = cache_store_flt_id;
563 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
564 fconf->ops = &cache_ops;
565 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
566
567 (*orig_arg)++;
568
569 return ACT_RET_PRS_OK;
570
571err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100572 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100573}
574
575
576enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
577 struct session *sess, struct stream *s, int flags)
578{
William Lallemand77c11972017-10-31 20:43:01 +0100579
580 struct cache_entry search_entry;
581 struct cache_entry *res;
582
583 struct cache *cache = (struct cache *)rule->arg.act.p[0];
584
585 search_entry.eb.key = hash_djb2(s->txn->uri, strlen(s->txn->uri));
William Lallemanda400a3a2017-11-20 19:13:12 +0100586 shctx_lock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100587 res = entry_exist(cache, &search_entry);
588 if (res) {
589 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100590 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
591 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100592 s->target = &http_cache_applet.obj_type;
593 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
594 appctx->st0 = HTTP_CACHE_INIT;
595 appctx->rule = rule;
596 appctx->ctx.cache.entry = res;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100597 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100598 } else {
Olivier Houchardfccf8402017-11-01 14:04:02 +0100599 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100600 }
601 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100602 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100603 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100604}
605
606
607enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
608 struct act_rule *rule, char **err)
609{
610 int cur_arg = *orig_arg;
611
612 rule->action = ACT_CUSTOM;
613 rule->action_ptr = http_action_req_cache_use;
614
615 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
616 memprintf(err, "expects a cache name");
617 return ACT_RET_PRS_ERR;
618 }
619
620 rule->arg.act.p[0] = strdup(args[cur_arg]);
621 if (!rule->arg.act.p[0]) {
622 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
623 err++;
624 goto err;
625 }
626
627 (*orig_arg)++;
628 return ACT_RET_PRS_OK;
629
630err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100631 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100632
633}
634
635int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
636{
637 int err_code = 0;
638
639 if (strcmp(args[0], "cache") == 0) { /* new cache section */
640
641 if (!*args[1]) {
642 Alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
643 file, linenum, args[0]);
644 err_code |= ERR_ALERT | ERR_ABORT;
645 goto out;
646 }
647
648 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
649 err_code |= ERR_ABORT;
650 goto out;
651 }
652
653 if (tmp_cache_config == NULL) {
654 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
655 if (!tmp_cache_config) {
656 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
657 err_code |= ERR_ALERT | ERR_ABORT;
658 goto out;
659 }
660
661 strlcpy2(tmp_cache_config->id, args[1], 33);
662 if (strlen(args[1]) > 32) {
663 Warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
664 file, linenum, tmp_cache_config->id);
665 err_code |= ERR_WARN;
666 }
667
668 tmp_cache_config->maxblocks = 0;
669 }
670 } else if (strcmp(args[0], "total-max-size") == 0) {
671 int maxsize;
672
673 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
674 err_code |= ERR_ABORT;
675 goto out;
676 }
677
678 /* size in megabytes */
679 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
680 tmp_cache_config->maxblocks = maxsize;
681
682 } else if (*args[0] != 0) {
683 Alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
684 err_code |= ERR_ALERT | ERR_FATAL;
685 goto out;
686 }
687out:
688 return err_code;
689}
690
691/* once the cache section is parsed */
692
693int cfg_post_parse_section_cache()
694{
695 struct shared_context *shctx;
696 int err_code = 0;
697 int ret_shctx;
698
699 if (tmp_cache_config) {
700 struct cache *cache;
701
702 if (tmp_cache_config->maxblocks <= 0) {
703 Alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
704 err_code |= ERR_FATAL | ERR_ALERT;
705 goto out;
706 }
707
708 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100709
William Lallemand41db4602017-10-30 11:15:51 +0100710 if (ret_shctx < 0) {
711 if (ret_shctx == SHCTX_E_INIT_LOCK)
712 Alert("Unable to initialize the lock for the cache.\n");
713 else
714 Alert("Unable to allocate cache.\n");
715
716 err_code |= ERR_FATAL | ERR_ALERT;
717 goto out;
718 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100719 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100720 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
721 cache = (struct cache *)shctx->data;
722 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100723 LIST_ADDQ(&caches, &cache->list);
724 }
725out:
726 free(tmp_cache_config);
727 tmp_cache_config = NULL;
728 return err_code;
729
730}
731
732/*
733 * Resolve the cache name to a pointer once the file is completely read.
734 */
735int cfg_cache_postparser()
736{
737 struct act_rule *hresrule, *hrqrule;
738 void *cache_ptr;
739 struct cache *cache;
740 struct proxy *curproxy = NULL;
741 int err = 0;
742 struct flt_conf *fconf;
743
744 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
745
746 /* resolve the http response cache name to a ptr in the action rule */
747 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
748 if (hresrule->action != ACT_CUSTOM ||
749 hresrule->action_ptr != http_action_store_cache)
750 continue;
751
752 cache_ptr = hresrule->arg.act.p[0];
753
754 list_for_each_entry(cache, &caches, list) {
755 if (!strcmp(cache->id, cache_ptr)) {
756 /* don't free there, it's still used in the filter conf */
757 cache_ptr = cache;
758 break;
759 }
760 }
761
762 if (cache_ptr == hresrule->arg.act.p[0]) {
763 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
764 curproxy->id, (char *)hresrule->arg.act.p[0]);
765 err++;
766 }
767
768 hresrule->arg.act.p[0] = cache_ptr;
769 }
770
771 /* resolve the http request cache name to a ptr in the action rule */
772 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
773 if (hrqrule->action != ACT_CUSTOM ||
774 hrqrule->action_ptr != http_action_req_cache_use)
775 continue;
776
777 cache_ptr = hrqrule->arg.act.p[0];
778
779 list_for_each_entry(cache, &caches, list) {
780 if (!strcmp(cache->id, cache_ptr)) {
781 free(cache_ptr);
782 cache_ptr = cache;
783 break;
784 }
785 }
786
787 if (cache_ptr == hrqrule->arg.act.p[0]) {
788 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
789 curproxy->id, (char *)hrqrule->arg.act.p[0]);
790 err++;
791 }
792
793 hrqrule->arg.act.p[0] = cache_ptr;
794 }
795
796 /* resolve the cache name to a ptr in the filter config */
797 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
798
William Lallemand9c54c532017-11-02 16:38:42 +0100799 if (fconf->id != cache_store_flt_id)
800 continue;
801
William Lallemand41db4602017-10-30 11:15:51 +0100802 cache_ptr = fconf->conf;
803
804 list_for_each_entry(cache, &caches, list) {
805 if (!strcmp(cache->id, cache_ptr)) {
806 /* there can be only one filter per cache, so we free it there */
807 free(cache_ptr);
808 cache_ptr = cache;
809 break;
810 }
811 }
812
813 if (cache_ptr == fconf->conf) {
814 Alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
815 curproxy->id, (char *)fconf->conf);
816 err++;
817 }
818 fconf->conf = cache_ptr;
819 }
820 }
821 return err;
822}
823
824
825struct flt_ops cache_ops = {
826 .init = cache_store_init,
827
William Lallemand4da3f8a2017-10-31 14:33:34 +0100828 /* Handle channels activity */
829 .channel_start_analyze = cache_store_chn_start_analyze,
830
831 /* Filter HTTP requests and responses */
832 .http_headers = cache_store_http_headers,
833 .http_end = cache_store_http_end,
834
835 .http_forward_data = cache_store_http_forward_data,
836
William Lallemand41db4602017-10-30 11:15:51 +0100837};
838
839static struct action_kw_list http_res_actions = {
840 .kw = {
841 { "cache-store", parse_cache_store },
842 { NULL, NULL }
843 }
844};
845
846static struct action_kw_list http_req_actions = {
847 .kw = {
848 { "cache-use", parse_cache_use },
849 { NULL, NULL }
850 }
851};
852
853struct applet http_cache_applet = {
854 .obj_type = OBJ_TYPE_APPLET,
855 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +0100856 .fct = http_cache_io_handler,
William Lallemand41db4602017-10-30 11:15:51 +0100857 .release = NULL,
858};
859
860__attribute__((constructor))
861static void __cache_init(void)
862{
863 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
864 cfg_register_postparser("cache", cfg_cache_postparser);
865 http_res_keywords_register(&http_res_actions);
866 http_req_keywords_register(&http_req_actions);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100867 pool2_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +0100868}
869