Line data Source code
1 : /*
2 : * Copyright (c) 2022 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <http_static/http_cache.h>
17 : #include <vppinfra/bihash_template.c>
18 : #include <vppinfra/unix.h>
19 : #include <vlib/vlib.h>
20 :
21 : static void
22 0 : hss_cache_lock (hss_cache_t *hc)
23 : {
24 0 : clib_spinlock_lock (&hc->cache_lock);
25 0 : }
26 :
27 : static void
28 0 : hss_cache_unlock (hss_cache_t *hc)
29 : {
30 0 : clib_spinlock_unlock (&hc->cache_lock);
31 0 : }
32 :
33 : /** \brief Sanity-check the forward and reverse LRU lists
34 : */
35 : static inline void
36 0 : lru_validate (hss_cache_t *hc)
37 : {
38 : #if CLIB_DEBUG > 0
39 : f64 last_timestamp;
40 : u32 index;
41 : int i;
42 : hss_cache_entry_t *ce;
43 :
44 0 : last_timestamp = 1e70;
45 0 : for (i = 1, index = hc->first_index; index != ~0;)
46 : {
47 0 : ce = pool_elt_at_index (hc->cache_pool, index);
48 : /* Timestamps should be smaller (older) as we walk the fwd list */
49 0 : if (ce->last_used > last_timestamp)
50 : {
51 0 : clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
52 : i, ce->last_used, last_timestamp);
53 : }
54 0 : index = ce->next_index;
55 0 : last_timestamp = ce->last_used;
56 0 : i++;
57 : }
58 :
59 0 : last_timestamp = 0.0;
60 0 : for (i = 1, index = hc->last_index; index != ~0;)
61 : {
62 0 : ce = pool_elt_at_index (hc->cache_pool, index);
63 : /* Timestamps should be larger (newer) as we walk the rev list */
64 0 : if (ce->last_used < last_timestamp)
65 : {
66 0 : clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
67 : i, ce->last_used, last_timestamp);
68 : }
69 0 : index = ce->prev_index;
70 0 : last_timestamp = ce->last_used;
71 0 : i++;
72 : }
73 : #endif
74 0 : }
75 :
76 : /** \brief Remove a data cache entry from the LRU lists
77 : */
78 : static inline void
79 0 : lru_remove (hss_cache_t *hc, hss_cache_entry_t *ce)
80 : {
81 : hss_cache_entry_t *next_ep, *prev_ep;
82 : u32 ce_index;
83 :
84 0 : lru_validate (hc);
85 :
86 0 : ce_index = ce - hc->cache_pool;
87 :
88 : /* Deal with list heads */
89 0 : if (ce_index == hc->first_index)
90 0 : hc->first_index = ce->next_index;
91 0 : if (ce_index == hc->last_index)
92 0 : hc->last_index = ce->prev_index;
93 :
94 : /* Fix next->prev */
95 0 : if (ce->next_index != ~0)
96 : {
97 0 : next_ep = pool_elt_at_index (hc->cache_pool, ce->next_index);
98 0 : next_ep->prev_index = ce->prev_index;
99 : }
100 : /* Fix prev->next */
101 0 : if (ce->prev_index != ~0)
102 : {
103 0 : prev_ep = pool_elt_at_index (hc->cache_pool, ce->prev_index);
104 0 : prev_ep->next_index = ce->next_index;
105 : }
106 0 : lru_validate (hc);
107 0 : }
108 :
109 : /** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
110 : */
111 : static inline void
112 0 : lru_add (hss_cache_t *hc, hss_cache_entry_t *ce, f64 now)
113 : {
114 : hss_cache_entry_t *next_ce;
115 : u32 ce_index;
116 :
117 0 : lru_validate (hc);
118 :
119 0 : ce_index = ce - hc->cache_pool;
120 :
121 : /*
122 : * Re-add at the head of the forward LRU list,
123 : * tail of the reverse LRU list
124 : */
125 0 : if (hc->first_index != ~0)
126 : {
127 0 : next_ce = pool_elt_at_index (hc->cache_pool, hc->first_index);
128 0 : next_ce->prev_index = ce_index;
129 : }
130 :
131 0 : ce->prev_index = ~0;
132 :
133 : /* ep now the new head of the LRU forward list */
134 0 : ce->next_index = hc->first_index;
135 0 : hc->first_index = ce_index;
136 :
137 : /* single session case: also the tail of the reverse LRU list */
138 0 : if (hc->last_index == ~0)
139 0 : hc->last_index = ce_index;
140 0 : ce->last_used = now;
141 :
142 0 : lru_validate (hc);
143 0 : }
144 :
145 : /** \brief Remove and re-add a cache entry from/to the LRU lists
146 : */
147 : static inline void
148 0 : lru_update (hss_cache_t *hc, hss_cache_entry_t *ep, f64 now)
149 : {
150 0 : lru_remove (hc, ep);
151 0 : lru_add (hc, ep, now);
152 0 : }
153 :
154 : static void
155 0 : hss_cache_attach_entry (hss_cache_t *hc, u32 ce_index, u8 **data,
156 : u64 *data_len)
157 : {
158 : hss_cache_entry_t *ce;
159 :
160 : /* Expect ce_index to be validated outside */
161 0 : ce = pool_elt_at_index (hc->cache_pool, ce_index);
162 0 : ce->inuse++;
163 0 : *data = ce->data;
164 0 : *data_len = vec_len (ce->data);
165 :
166 : /* Update the cache entry, mark it in-use */
167 0 : lru_update (hc, ce, vlib_time_now (vlib_get_main ()));
168 :
169 0 : if (hc->debug_level > 1)
170 0 : clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
171 0 : }
172 :
173 : /** \brief Detach cache entry from session
174 : */
175 : void
176 0 : hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index)
177 : {
178 : hss_cache_entry_t *ce;
179 :
180 0 : hss_cache_lock (hc);
181 :
182 0 : ce = pool_elt_at_index (hc->cache_pool, ce_index);
183 0 : ce->inuse--;
184 :
185 0 : if (hc->debug_level > 1)
186 0 : clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
187 :
188 0 : hss_cache_unlock (hc);
189 0 : }
190 :
191 : static u32
192 0 : hss_cache_lookup (hss_cache_t *hc, u8 *path)
193 : {
194 : BVT (clib_bihash_kv) kv;
195 : int rv;
196 :
197 0 : kv.key = (u64) path;
198 0 : kv.value = ~0;
199 :
200 : /* Value updated only if lookup succeeds */
201 0 : rv = BV (clib_bihash_search) (&hc->name_to_data, &kv, &kv);
202 0 : ASSERT (!rv || kv.value == ~0);
203 :
204 0 : if (hc->debug_level > 1)
205 0 : clib_warning ("lookup '%s' %s", kv.key, kv.value == ~0 ? "fail" : "found");
206 :
207 0 : return kv.value;
208 : }
209 :
210 : u32
211 0 : hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
212 : u64 *data_len)
213 : {
214 : u32 ce_index;
215 :
216 : /* Make sure nobody removes the entry while we look it up */
217 0 : hss_cache_lock (hc);
218 :
219 0 : ce_index = hss_cache_lookup (hc, path);
220 0 : if (ce_index != ~0)
221 0 : hss_cache_attach_entry (hc, ce_index, data, data_len);
222 :
223 0 : hss_cache_unlock (hc);
224 :
225 0 : return ce_index;
226 : }
227 :
228 : static void
229 0 : hss_cache_do_evictions (hss_cache_t *hc)
230 : {
231 : BVT (clib_bihash_kv) kv;
232 : hss_cache_entry_t *ce;
233 : u32 free_index;
234 :
235 0 : free_index = hc->last_index;
236 :
237 0 : while (free_index != ~0)
238 : {
239 : /* pick the LRU */
240 0 : ce = pool_elt_at_index (hc->cache_pool, free_index);
241 : /* Which could be in use... */
242 0 : if (ce->inuse)
243 : {
244 0 : if (hc->debug_level > 1)
245 0 : clib_warning ("index %d in use refcnt %d", free_index, ce->inuse);
246 : }
247 0 : free_index = ce->prev_index;
248 0 : kv.key = (u64) (ce->filename);
249 0 : kv.value = ~0ULL;
250 0 : if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
251 : 0)
252 : {
253 0 : clib_warning ("LRU delete '%s' FAILED!", ce->filename);
254 : }
255 0 : else if (hc->debug_level > 1)
256 0 : clib_warning ("LRU delete '%s' ok", ce->filename);
257 :
258 0 : lru_remove (hc, ce);
259 0 : hc->cache_size -= vec_len (ce->data);
260 0 : hc->cache_evictions++;
261 0 : vec_free (ce->filename);
262 0 : vec_free (ce->data);
263 :
264 0 : if (hc->debug_level > 1)
265 0 : clib_warning ("pool put index %d", ce - hc->cache_pool);
266 :
267 0 : pool_put (hc->cache_pool, ce);
268 0 : if (hc->cache_size < hc->cache_limit)
269 0 : break;
270 : }
271 0 : }
272 :
273 : u32
274 0 : hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data, u64 *data_len)
275 : {
276 : BVT (clib_bihash_kv) kv;
277 : hss_cache_entry_t *ce;
278 : clib_error_t *error;
279 : u8 *file_data;
280 : u32 ce_index;
281 :
282 0 : hss_cache_lock (hc);
283 :
284 : /* Need to recycle one (or more cache) entries? */
285 0 : if (hc->cache_size > hc->cache_limit)
286 0 : hss_cache_do_evictions (hc);
287 :
288 : /* Read the file */
289 0 : error = clib_file_contents ((char *) path, &file_data);
290 0 : if (error)
291 : {
292 0 : clib_warning ("Error reading '%s'", path);
293 0 : clib_error_report (error);
294 0 : return ~0;
295 : }
296 :
297 : /* Create a cache entry for it */
298 0 : pool_get_zero (hc->cache_pool, ce);
299 0 : ce->filename = vec_dup (path);
300 0 : ce->data = file_data;
301 :
302 : /* Attach cache entry without additional lock */
303 0 : ce->inuse++;
304 0 : *data = file_data;
305 0 : *data_len = vec_len (file_data);
306 0 : lru_add (hc, ce, vlib_time_now (vlib_get_main ()));
307 :
308 0 : hc->cache_size += vec_len (ce->data);
309 0 : ce_index = ce - hc->cache_pool;
310 :
311 0 : if (hc->debug_level > 1)
312 0 : clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
313 :
314 : /* Add to the lookup table */
315 :
316 0 : kv.key = (u64) vec_dup (path);
317 0 : kv.value = ce_index;
318 :
319 0 : if (hc->debug_level > 1)
320 0 : clib_warning ("add '%s' value %lld", kv.key, kv.value);
321 :
322 0 : if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 1 /* is_add */) < 0)
323 : {
324 0 : clib_warning ("BUG: add failed!");
325 : }
326 :
327 0 : hss_cache_unlock (hc);
328 :
329 0 : return ce_index;
330 : }
331 :
332 : u32
333 0 : hss_cache_clear (hss_cache_t *hc)
334 : {
335 0 : u32 free_index, busy_items = 0;
336 : hss_cache_entry_t *ce;
337 : BVT (clib_bihash_kv) kv;
338 :
339 0 : hss_cache_lock (hc);
340 :
341 : /* Walk the LRU list to find active entries */
342 0 : free_index = hc->last_index;
343 0 : while (free_index != ~0)
344 : {
345 0 : ce = pool_elt_at_index (hc->cache_pool, free_index);
346 0 : free_index = ce->prev_index;
347 : /* Which could be in use... */
348 0 : if (ce->inuse)
349 : {
350 0 : busy_items++;
351 0 : free_index = ce->next_index;
352 0 : continue;
353 : }
354 0 : kv.key = (u64) (ce->filename);
355 0 : kv.value = ~0ULL;
356 0 : if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
357 : 0)
358 : {
359 0 : clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
360 : }
361 :
362 0 : lru_remove (hc, ce);
363 0 : hc->cache_size -= vec_len (ce->data);
364 0 : hc->cache_evictions++;
365 0 : vec_free (ce->filename);
366 0 : vec_free (ce->data);
367 0 : if (hc->debug_level > 1)
368 0 : clib_warning ("pool put index %d", ce - hc->cache_pool);
369 0 : pool_put (hc->cache_pool, ce);
370 0 : free_index = hc->last_index;
371 : }
372 :
373 0 : hss_cache_unlock (hc);
374 :
375 0 : return busy_items;
376 : }
377 :
378 : void
379 0 : hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level)
380 : {
381 0 : clib_spinlock_init (&hc->cache_lock);
382 :
383 : /* Init path-to-cache hash table */
384 0 : BV (clib_bihash_init) (&hc->name_to_data, "http cache", 128, 32 << 20);
385 :
386 0 : hc->cache_limit = cache_size;
387 0 : hc->debug_level = debug_level;
388 0 : hc->first_index = hc->last_index = ~0;
389 0 : }
390 :
391 : /** \brief format a file cache entry
392 : */
393 : static u8 *
394 0 : format_hss_cache_entry (u8 *s, va_list *args)
395 : {
396 0 : hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
397 0 : f64 now = va_arg (*args, f64);
398 :
399 : /* Header */
400 0 : if (ep == 0)
401 : {
402 0 : s = format (s, "%40s%12s%20s", "File", "Size", "Age");
403 0 : return s;
404 : }
405 0 : s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
406 0 : now - ep->last_used);
407 0 : return s;
408 : }
409 :
410 : u8 *
411 0 : format_hss_cache (u8 *s, va_list *args)
412 : {
413 0 : hss_cache_t *hc = va_arg (*args, hss_cache_t *);
414 0 : u32 verbose = va_arg (*args, u32);
415 : hss_cache_entry_t *ce;
416 : vlib_main_t *vm;
417 : u32 index;
418 : f64 now;
419 :
420 0 : if (verbose == 0)
421 : {
422 0 : s = format (s, "cache size %lld bytes, limit %lld bytes, evictions %lld",
423 : hc->cache_size, hc->cache_limit, hc->cache_evictions);
424 0 : return 0;
425 : }
426 :
427 0 : vm = vlib_get_main ();
428 0 : now = vlib_time_now (vm);
429 :
430 0 : s = format (s, "%U", format_hss_cache_entry, 0 /* header */, now);
431 :
432 0 : for (index = hc->first_index; index != ~0;)
433 : {
434 0 : ce = pool_elt_at_index (hc->cache_pool, index);
435 0 : index = ce->next_index;
436 0 : s = format (s, "%U", format_hss_cache_entry, ce, now);
437 : }
438 :
439 0 : s = format (s, "%40s%12lld", "Total Size", hc->cache_size);
440 :
441 0 : return s;
442 : }
443 :
444 : /*
445 : * fd.io coding-style-patch-verification: ON
446 : *
447 : * Local Variables:
448 : * eval: (c-set-style "gnu")
449 : * End:
450 : */
|