dpdk: xstats vecor stuck at 0 elements
[vpp.git] / src / plugins / http_static / http_cache.c
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 hss_cache_lock (hss_cache_t *hc)
23 {
24   clib_spinlock_lock (&hc->cache_lock);
25 }
26
27 static void
28 hss_cache_unlock (hss_cache_t *hc)
29 {
30   clib_spinlock_unlock (&hc->cache_lock);
31 }
32
33 /** \brief Sanity-check the forward and reverse LRU lists
34  */
35 static inline void
36 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   last_timestamp = 1e70;
45   for (i = 1, index = hc->first_index; index != ~0;)
46     {
47       ce = pool_elt_at_index (hc->cache_pool, index);
48       /* Timestamps should be smaller (older) as we walk the fwd list */
49       if (ce->last_used > last_timestamp)
50         {
51           clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
52                         i, ce->last_used, last_timestamp);
53         }
54       index = ce->next_index;
55       last_timestamp = ce->last_used;
56       i++;
57     }
58
59   last_timestamp = 0.0;
60   for (i = 1, index = hc->last_index; index != ~0;)
61     {
62       ce = pool_elt_at_index (hc->cache_pool, index);
63       /* Timestamps should be larger (newer) as we walk the rev list */
64       if (ce->last_used < last_timestamp)
65         {
66           clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
67                         i, ce->last_used, last_timestamp);
68         }
69       index = ce->prev_index;
70       last_timestamp = ce->last_used;
71       i++;
72     }
73 #endif
74 }
75
76 /** \brief Remove a data cache entry from the LRU lists
77  */
78 static inline void
79 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   lru_validate (hc);
85
86   ce_index = ce - hc->cache_pool;
87
88   /* Deal with list heads */
89   if (ce_index == hc->first_index)
90     hc->first_index = ce->next_index;
91   if (ce_index == hc->last_index)
92     hc->last_index = ce->prev_index;
93
94   /* Fix next->prev */
95   if (ce->next_index != ~0)
96     {
97       next_ep = pool_elt_at_index (hc->cache_pool, ce->next_index);
98       next_ep->prev_index = ce->prev_index;
99     }
100   /* Fix prev->next */
101   if (ce->prev_index != ~0)
102     {
103       prev_ep = pool_elt_at_index (hc->cache_pool, ce->prev_index);
104       prev_ep->next_index = ce->next_index;
105     }
106   lru_validate (hc);
107 }
108
109 /** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
110  */
111 static inline void
112 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   lru_validate (hc);
118
119   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   if (hc->first_index != ~0)
126     {
127       next_ce = pool_elt_at_index (hc->cache_pool, hc->first_index);
128       next_ce->prev_index = ce_index;
129     }
130
131   ce->prev_index = ~0;
132
133   /* ep now the new head of the LRU forward list */
134   ce->next_index = hc->first_index;
135   hc->first_index = ce_index;
136
137   /* single session case: also the tail of the reverse LRU list */
138   if (hc->last_index == ~0)
139     hc->last_index = ce_index;
140   ce->last_used = now;
141
142   lru_validate (hc);
143 }
144
145 /** \brief Remove and re-add a cache entry from/to the LRU lists
146  */
147 static inline void
148 lru_update (hss_cache_t *hc, hss_cache_entry_t *ep, f64 now)
149 {
150   lru_remove (hc, ep);
151   lru_add (hc, ep, now);
152 }
153
154 static void
155 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   ce = pool_elt_at_index (hc->cache_pool, ce_index);
162   ce->inuse++;
163   *data = ce->data;
164   *data_len = vec_len (ce->data);
165
166   /* Update the cache entry, mark it in-use */
167   lru_update (hc, ce, vlib_time_now (vlib_get_main ()));
168
169   if (hc->debug_level > 1)
170     clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
171 }
172
173 /** \brief Detach cache entry from session
174  */
175 void
176 hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index)
177 {
178   hss_cache_entry_t *ce;
179
180   hss_cache_lock (hc);
181
182   ce = pool_elt_at_index (hc->cache_pool, ce_index);
183   ce->inuse--;
184
185   if (hc->debug_level > 1)
186     clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
187
188   hss_cache_unlock (hc);
189 }
190
191 static u32
192 hss_cache_lookup (hss_cache_t *hc, u8 *path)
193 {
194   BVT (clib_bihash_kv) kv;
195   int rv;
196
197   kv.key = (u64) path;
198   kv.value = ~0;
199
200   /* Value updated only if lookup succeeds */
201   rv = BV (clib_bihash_search) (&hc->name_to_data, &kv, &kv);
202   ASSERT (!rv || kv.value == ~0);
203
204   if (hc->debug_level > 1)
205     clib_warning ("lookup '%s' %s", kv.key, kv.value == ~0 ? "fail" : "found");
206
207   return kv.value;
208 }
209
210 u32
211 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   hss_cache_lock (hc);
218
219   ce_index = hss_cache_lookup (hc, path);
220   if (ce_index != ~0)
221     hss_cache_attach_entry (hc, ce_index, data, data_len);
222
223   hss_cache_unlock (hc);
224
225   return ce_index;
226 }
227
228 static void
229 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   free_index = hc->last_index;
236
237   while (free_index != ~0)
238     {
239       /* pick the LRU */
240       ce = pool_elt_at_index (hc->cache_pool, free_index);
241       /* Which could be in use... */
242       if (ce->inuse)
243         {
244           if (hc->debug_level > 1)
245             clib_warning ("index %d in use refcnt %d", free_index, ce->inuse);
246         }
247       free_index = ce->prev_index;
248       kv.key = (u64) (ce->filename);
249       kv.value = ~0ULL;
250       if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
251           0)
252         {
253           clib_warning ("LRU delete '%s' FAILED!", ce->filename);
254         }
255       else if (hc->debug_level > 1)
256         clib_warning ("LRU delete '%s' ok", ce->filename);
257
258       lru_remove (hc, ce);
259       hc->cache_size -= vec_len (ce->data);
260       hc->cache_evictions++;
261       vec_free (ce->filename);
262       vec_free (ce->data);
263
264       if (hc->debug_level > 1)
265         clib_warning ("pool put index %d", ce - hc->cache_pool);
266
267       pool_put (hc->cache_pool, ce);
268       if (hc->cache_size < hc->cache_limit)
269         break;
270     }
271 }
272
273 u32
274 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   hss_cache_lock (hc);
283
284   /* Need to recycle one (or more cache) entries? */
285   if (hc->cache_size > hc->cache_limit)
286     hss_cache_do_evictions (hc);
287
288   /* Read the file */
289   error = clib_file_contents ((char *) path, &file_data);
290   if (error)
291     {
292       clib_warning ("Error reading '%s'", path);
293       clib_error_report (error);
294       return ~0;
295     }
296
297   /* Create a cache entry for it */
298   pool_get_zero (hc->cache_pool, ce);
299   ce->filename = vec_dup (path);
300   ce->data = file_data;
301
302   /* Attach cache entry without additional lock */
303   ce->inuse++;
304   *data = file_data;
305   *data_len = vec_len (file_data);
306   lru_add (hc, ce, vlib_time_now (vlib_get_main ()));
307
308   hc->cache_size += vec_len (ce->data);
309   ce_index = ce - hc->cache_pool;
310
311   if (hc->debug_level > 1)
312     clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
313
314   /* Add to the lookup table */
315
316   kv.key = (u64) vec_dup (path);
317   kv.value = ce_index;
318
319   if (hc->debug_level > 1)
320     clib_warning ("add '%s' value %lld", kv.key, kv.value);
321
322   if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 1 /* is_add */) < 0)
323     {
324       clib_warning ("BUG: add failed!");
325     }
326
327   hss_cache_unlock (hc);
328
329   return ce_index;
330 }
331
332 u32
333 hss_cache_clear (hss_cache_t *hc)
334 {
335   u32 free_index, busy_items = 0;
336   hss_cache_entry_t *ce;
337   BVT (clib_bihash_kv) kv;
338
339   hss_cache_lock (hc);
340
341   /* Walk the LRU list to find active entries */
342   free_index = hc->last_index;
343   while (free_index != ~0)
344     {
345       ce = pool_elt_at_index (hc->cache_pool, free_index);
346       free_index = ce->prev_index;
347       /* Which could be in use... */
348       if (ce->inuse)
349         {
350           busy_items++;
351           free_index = ce->next_index;
352           continue;
353         }
354       kv.key = (u64) (ce->filename);
355       kv.value = ~0ULL;
356       if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
357           0)
358         {
359           clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
360         }
361
362       lru_remove (hc, ce);
363       hc->cache_size -= vec_len (ce->data);
364       hc->cache_evictions++;
365       vec_free (ce->filename);
366       vec_free (ce->data);
367       if (hc->debug_level > 1)
368         clib_warning ("pool put index %d", ce - hc->cache_pool);
369       pool_put (hc->cache_pool, ce);
370       free_index = hc->last_index;
371     }
372
373   hss_cache_unlock (hc);
374
375   return busy_items;
376 }
377
378 void
379 hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level)
380 {
381   clib_spinlock_init (&hc->cache_lock);
382
383   /* Init path-to-cache hash table */
384   BV (clib_bihash_init) (&hc->name_to_data, "http cache", 128, 32 << 20);
385
386   hc->cache_limit = cache_size;
387   hc->debug_level = debug_level;
388   hc->first_index = hc->last_index = ~0;
389 }
390
391 /** \brief format a file cache entry
392  */
393 static u8 *
394 format_hss_cache_entry (u8 *s, va_list *args)
395 {
396   hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
397   f64 now = va_arg (*args, f64);
398
399   /* Header */
400   if (ep == 0)
401     {
402       s = format (s, "%40s%12s%20s", "File", "Size", "Age");
403       return s;
404     }
405   s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
406               now - ep->last_used);
407   return s;
408 }
409
410 u8 *
411 format_hss_cache (u8 *s, va_list *args)
412 {
413   hss_cache_t *hc = va_arg (*args, hss_cache_t *);
414   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   if (verbose == 0)
421     {
422       s = format (s, "cache size %lld bytes, limit %lld bytes, evictions %lld",
423                   hc->cache_size, hc->cache_limit, hc->cache_evictions);
424       return 0;
425     }
426
427   vm = vlib_get_main ();
428   now = vlib_time_now (vm);
429
430   s = format (s, "%U", format_hss_cache_entry, 0 /* header */, now);
431
432   for (index = hc->first_index; index != ~0;)
433     {
434       ce = pool_elt_at_index (hc->cache_pool, index);
435       index = ce->next_index;
436       s = format (s, "%U", format_hss_cache_entry, ce, now);
437     }
438
439   s = format (s, "%40s%12lld", "Total Size", hc->cache_size);
440
441   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  */