summaryrefslogtreecommitdiffstats
path: root/src/plugins/http_static/http_cache.c
blob: 2e63e335d471a117f84132740913226bd96c11ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright (c) 2022 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <http_static/http_cache.h>
#include <vppinfra/bihash_template.c>
#include <vppinfra/unix.h>
#include <vlib/vlib.h>
#include <sys/stat.h>
#include <vppinfra/time_range.h>

static void
hss_cache_lock (hss_cache_t *hc)
{
  clib_spinlock_lock (&hc->cache_lock);
}

static void
hss_cache_unlock (hss_cache_t *hc)
{
  clib_spinlock_unlock (&hc->cache_lock);
}

/** \brief Sanity-check the forward and reverse LRU lists
 */
static inline void
lru_validate (hss_cache_t *hc)
{
#if CLIB_DEBUG > 0
  f64 last_timestamp;
  u32 index;
  int i;
  hss_cache_entry_t *ce;

  last_timestamp = 1e70;
  for (i = 1, index = hc->first_index; index != ~0;)
    {
      ce = pool_elt_at_index (hc->cache_pool, index);
      /* Timestamps should be smaller (older) as we walk the fwd list */
      if (ce->last_used > last_timestamp)
	{
	  clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
			i, ce->last_used, last_timestamp);
	}
      index = ce->next_index;
      last_timestamp = ce->last_used;
      i++;
    }

  last_timestamp = 0.0;
  for (i = 1, index = hc->last_index; index != ~0;)
    {
      ce = pool_elt_at_index (hc->cache_pool, index);
      /* Timestamps should be larger (newer) as we walk the rev list */
      if (ce->last_used < last_timestamp)
	{
	  clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
			i, ce->last_used, last_timestamp);
	}
      index = ce->prev_index;
      last_timestamp = ce->last_used;
      i++;
    }
#endif
}

/** \brief Remove a data cache entry from the LRU lists
 */
static inline void
lru_remove (hss_cache_t *hc, hss_cache_entry_t *ce)
{
  hss_cache_entry_t *next_ep, *prev_ep;
  u32 ce_index;

  lru_validate (hc);

  ce_index = ce - hc->cache_pool;

  /* Deal with list heads */
  if (ce_index == hc->first_index)
    hc->first_index = ce->next_index;
  if (ce_index == hc->last_index)
    hc->last_index = ce->prev_index;

  /* Fix next->prev */
  if (ce->next_index != ~0)
    {
      next_ep = pool_elt_at_index (hc->cache_pool, ce->next_index);
      next_ep->prev_index = ce->prev_index;
    }
  /* Fix prev->next */
  if (ce->prev_index != ~0)
    {
      prev_ep = pool_elt_at_index (hc->cache_pool, ce->prev_index);
      prev_ep->next_index = ce->next_index;
    }
  lru_validate (hc);
}

/** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
 */
static inline void
lru_add (hss_cache_t *hc, hss_cache_entry_t *ce, f64 now)
{
  hss_cache_entry_t *next_ce;
  u32 ce_index;

  lru_validate (hc);

  ce_index = ce - hc->cache_pool;

  /*
   * Re-add at the head of the forward LRU list,
   * tail of the reverse LRU list
   */
  if (hc->first_index != ~0)
    {
      next_ce = pool_elt_at_index (hc->cache_pool, hc->first_index);
      next_ce->prev_index = ce_index;
    }

  ce->prev_index = ~0;

  /* ep now the new head of the LRU forward list */
  ce->next_index = hc->first_index;
  hc->first_index = ce_index;

  /* single session case: also the tail of the reverse LRU list */
  if (hc->last_index == ~0)
    hc->last_index = ce_index;
  ce->last_used = now;

  lru_validate (hc);
}

/** \brief Remove and re-add a cache entry from/to the LRU lists
 */
static inline void
lru_update (hss_cache_t *hc, hss_cache_entry_t *ep, f64 now)
{
  lru_remove (hc, ep);
  lru_add (hc, ep, now);
}

static void
hss_cache_attach_entry (hss_cache_t *hc, u32 ce_index, u8 **data,
			u64 *data_len, u8 **last_modified)
{
  hss_cache_entry_t *ce;

  /* Expect ce_index to be validated outside */
  ce = pool_elt_at_index (hc->cache_pool, ce_index);
  ce->inuse++;
  *data = ce->data;
  *data_len = vec_len (ce->data);
  *last_modified = ce->last_modified;

  /* Update the cache entry, mark it in-use */
  lru_update (hc, ce, vlib_time_now (vlib_get_main ()));

  if (hc->debug_level > 1)
    clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
}

/** \brief Detach cache entry from session
 */
void
hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index)
{
  hss_cache_entry_t *ce;

  hss_cache_lock (hc);

  ce = pool_elt_at_index (hc->cache_pool, ce_index);
  ce->inuse--;

  if (hc->debug_level > 1)
    clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);

  hss_cache_unlock (hc);
}

static u32
hss_cache_lookup (hss_cache_t *hc, u8 *path)
{
  BVT (clib_bihash_kv) kv;
  int rv;

  kv.key = (u64) path;
  kv.value = ~0;

  /* Value updated only if lookup succeeds */
  rv = BV (clib_bihash_search) (&hc->name_to_data, &kv, &kv);
  ASSERT (!rv || kv.value == ~0);

  if (hc->debug_level > 1)
    clib_warning ("lookup '%s' %s", kv.key, kv.value == ~0 ? "fail" : "found");

  return kv.value;
}

u32
hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
			     u64 *data_len, u8 **last_modified)
{
  u32 ce_index;
  /* Make sure nobody removes the entry while we look it up */
  hss_cache_lock (hc);

  ce_index = hss_cache_lookup (hc, path);
  if (ce_index != ~0)
    hss_cache_attach_entry (hc, ce_index, data, data_len, last_modified);

  hss_cache_unlock (hc);

  return ce_index;
}

static void
hss_cache_do_evictions (hss_cache_t *hc)
{
  BVT (clib_bihash_kv) kv;
  hss_cache_entry_t *ce;
  u32 free_index;

  free_index = hc->last_index;

  while (free_index != ~0)
    {
      /* pick the LRU */
      ce = pool_elt_at_index (hc->cache_pool, free_index);
      /* Which could be in use... */
      if (ce->inuse)
	{
	  if (hc->debug_level > 1)
	    clib_warning ("index %d in use refcnt %d", free_index, ce->inuse);
	}
      free_index = ce->prev_index;
      kv.key = (u64) (ce->filename);
      kv.value = ~0ULL;
      if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
	  0)
	{
	  clib_warning ("LRU delete '%s' FAILED!", ce->filename);
	}
      else if (hc->debug_level > 1)
	clib_warning ("LRU delete '%s' ok", ce->filename);

      lru_remove (hc, ce);
      hc->cache_size -= vec_len (ce->data);
      hc->cache_evictions++;
      vec_free (ce->filename);
      vec_free (ce->data);
      vec_free (ce->last_modified);

      if (hc->debug_level > 1)
	clib_warning ("pool put index %d", ce - hc->cache_pool);

      pool_put (hc->cache_pool, ce);
      if (hc->cache_size < hc->cache_limit)
	break;
    }
}

u32
hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data, u64 *data_len,
			  u8 **last_modified)
{
  BVT (clib_bihash_kv) kv;
  hss_cache_entry_t *ce;
  clib_error_t *error;
  u8 *file_data;
  u32 ce_index;
  struct stat dm;

  hss_cache_lock (hc);

  /* Need to recycle one (or more cache) entries? */
  if (hc->cache_size > hc->cache_limit)
    hss_cache_do_evictions (hc);

  /* Read the file */
  error = clib_file_contents ((char *) path, &file_data);
  if (error)
    {
      clib_warning ("Error reading '%s'", path);
      clib_error_report (error);
      return ~0;
    }

  /* Create a cache entry for it */
  pool_get_zero (hc->cache_pool, ce);
  ce->filename = vec_dup (path);
  ce->data = file_data;
  if (stat ((char *) path, &dm) == 0)
    {
      ce->last_modified =
	format (0, "%U GMT", format_clib_timebase_time, (f64) dm.st_mtime);
    }

  /* Attach cache entry without additional lock */
  ce->inuse++;
  *data = file_data;
  *data_len = vec_len (file_data);
  *last_modified = ce->last_modified;
  lru_add (hc, ce, vlib_time_now (vlib_get_main ()));

  hc->cache_size += vec_len (ce->data);
  ce_index = ce - hc->cache_pool;

  if (hc->debug_level > 1)
    clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);

  /* Add to the lookup table */

  kv.key = (u64) vec_dup (path);
  kv.value = ce_index;

  if (hc->debug_level > 1)
    clib_warning ("add '%s' value %lld", kv.key, kv.value);

  if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 1 /* is_add */) < 0)
    {
      clib_warning ("BUG: add failed!");
    }

  hss_cache_unlock (hc);

  return ce_index;
}

u32
hss_cache_clear (hss_cache_t *hc)
{
  u32 free_index, busy_items = 0;
  hss_cache_entry_t *ce;
  BVT (clib_bihash_kv) kv;

  hss_cache_lock (hc);

  /* Walk the LRU list to find active entries */
  free_index = hc->last_index;
  while (free_index != ~0)
    {
      ce = pool_elt_at_index (hc->cache_pool, free_index);
      free_index = ce->prev_index;
      /* Which could be in use... */
      if (ce->inuse)
	{
	  busy_items++;
	  free_index = ce->next_index;
	  continue;
	}
      kv.key = (u64) (ce->filename);
      kv.value = ~0ULL;
      if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
	  0)
	{
	  clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
	}

      lru_remove (hc, ce);
      hc->cache_size -= vec_len (ce->data);
      hc->cache_evictions++;
      vec_free (ce->filename);
      vec_free (ce->data);
      vec_free (ce->last_modified);
      if (hc->debug_level > 1)
	clib_warning ("pool put index %d", ce - hc->cache_pool);
      pool_put (hc->cache_pool, ce);
      free_index = hc->last_index;
    }

  hss_cache_unlock (hc);

  return busy_items;
}

void
hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level)
{
  clib_spinlock_init (&hc->cache_lock);

  /* Init path-to-cache hash table */
  BV (clib_bihash_init) (&hc->name_to_data, "http cache", 128, 32 << 20);

  hc->cache_limit = cache_size;
  hc->debug_level = debug_level;
  hc->first_index = hc->last_index = ~0;
}

/** \brief format a file cache entry
 */
static u8 *
format_hss_cache_entry (u8 *s, va_list *args)
{
  hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
  f64 now = va_arg (*args, f64);

  /* Header */
  if (ep == 0)
    {
      s = format (s, "%40s%12s%20s", "File", "Size", "Age");
      return s;
    }
  s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
	      now - ep->last_used);
  return s;
}

u8 *
format_hss_cache (u8 *s, va_list *args)
{
  hss_cache_t *hc = va_arg (*args, hss_cache_t *);
  u32 verbose = va_arg (*args, u32);
  hss_cache_entry_t *ce;
  vlib_main_t *vm;
  u32 index;
  f64 now;

  if (verbose == 0)
    {
      s = format (s, "cache size %lld bytes, limit %lld bytes, evictions %lld",
		  hc->cache_size, hc->cache_limit, hc->cache_evictions);
      return s;
    }

  vm = vlib_get_main ();
  now = vlib_time_now (vm);

  s = format (s, "%U\n", format_hss_cache_entry, 0 /* header */, now);

  for (index = hc->first_index; index != ~0;)
    {
      ce = pool_elt_at_index (hc->cache_pool, index);
      index = ce->next_index;
      s = format (s, "%U\n", format_hss_cache_entry, ce, now);
    }

  s = format (s, "%40s%12lld", "Total Size", hc->cache_size);

  return s;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */