2 * Copyright (c) 2017-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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <http_static/http_static.h>
17 #include <vppinfra/bihash_template.c>
18 #include <vppinfra/unix.h>
20 #include <sys/types.h>
24 /** @file static_server.c
25 * Static http server, sufficient to serve .html / .css / .js content.
27 /*? %%clicmd:group_label Static HTTP Server %% ?*/
29 #define HSS_FIFO_THRESH (16 << 10)
36 clib_spinlock_lock (&hss_main.cache_lock);
40 hss_cache_unlock (void)
42 clib_spinlock_unlock (&hss_main.cache_lock);
45 static hss_session_t *
46 hss_session_alloc (u32 thread_index)
48 hss_main_t *hsm = &hss_main;
51 pool_get_zero (hsm->sessions[thread_index], hs);
52 hs->session_index = hs - hsm->sessions[thread_index];
53 hs->thread_index = thread_index;
54 hs->cache_pool_index = ~0;
58 static hss_session_t *
59 hss_session_get (u32 thread_index, u32 hs_index)
61 hss_main_t *hsm = &hss_main;
62 if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
64 return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
68 hss_session_free (hss_session_t *hs)
70 hss_main_t *hsm = &hss_main;
72 pool_put (hsm->sessions[hs->thread_index], hs);
76 u32 save_thread_index;
77 save_thread_index = hs->thread_index;
78 /* Poison the entry, preserve timer state and thread index */
79 memset (hs, 0xfa, sizeof (*hs));
80 hs->thread_index = save_thread_index;
84 /** \brief Detach cache entry from session
87 hss_detach_cache_entry (hss_session_t *hs)
89 hss_main_t *hsm = &hss_main;
90 hss_cache_entry_t *ep;
93 * Decrement cache pool entry reference count
94 * Note that if e.g. a file lookup fails, the cache pool index
97 if (hs->cache_pool_index != ~0)
99 ep = pool_elt_at_index (hsm->cache_pool, hs->cache_pool_index);
101 if (hsm->debug_level > 1)
102 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
105 hs->cache_pool_index = ~0;
114 /** \brief Disconnect a session
117 hss_session_disconnect_transport (hss_session_t *hs)
119 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
120 a->handle = hs->vpp_session_handle;
121 a->app_index = hss_main.app_index;
122 vnet_disconnect_session (a);
125 /** \brief Sanity-check the forward and reverse LRU lists
128 lru_validate (hss_main_t *hsm)
134 hss_cache_entry_t *ep;
136 last_timestamp = 1e70;
137 for (i = 1, index = hsm->first_index; index != ~0;)
139 ep = pool_elt_at_index (hsm->cache_pool, index);
140 index = ep->next_index;
141 /* Timestamps should be smaller (older) as we walk the fwd list */
142 if (ep->last_used > last_timestamp)
144 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
145 ep - hsm->cache_pool, i,
146 ep->last_used, last_timestamp);
148 last_timestamp = ep->last_used;
152 last_timestamp = 0.0;
153 for (i = 1, index = hsm->last_index; index != ~0;)
155 ep = pool_elt_at_index (hsm->cache_pool, index);
156 index = ep->prev_index;
157 /* Timestamps should be larger (newer) as we walk the rev list */
158 if (ep->last_used < last_timestamp)
160 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
161 ep - hsm->cache_pool, i,
162 ep->last_used, last_timestamp);
164 last_timestamp = ep->last_used;
170 /** \brief Remove a data cache entry from the LRU lists
173 lru_remove (hss_main_t *hsm, hss_cache_entry_t *ep)
175 hss_cache_entry_t *next_ep, *prev_ep;
180 ep_index = ep - hsm->cache_pool;
182 /* Deal with list heads */
183 if (ep_index == hsm->first_index)
184 hsm->first_index = ep->next_index;
185 if (ep_index == hsm->last_index)
186 hsm->last_index = ep->prev_index;
189 if (ep->next_index != ~0)
191 next_ep = pool_elt_at_index (hsm->cache_pool, ep->next_index);
192 next_ep->prev_index = ep->prev_index;
195 if (ep->prev_index != ~0)
197 prev_ep = pool_elt_at_index (hsm->cache_pool, ep->prev_index);
198 prev_ep->next_index = ep->next_index;
203 /** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
206 lru_add (hss_main_t *hsm, hss_cache_entry_t *ep, f64 now)
208 hss_cache_entry_t *next_ep;
213 ep_index = ep - hsm->cache_pool;
216 * Re-add at the head of the forward LRU list,
217 * tail of the reverse LRU list
219 if (hsm->first_index != ~0)
221 next_ep = pool_elt_at_index (hsm->cache_pool, hsm->first_index);
222 next_ep->prev_index = ep_index;
227 /* ep now the new head of the LRU forward list */
228 ep->next_index = hsm->first_index;
229 hsm->first_index = ep_index;
231 /* single session case: also the tail of the reverse LRU list */
232 if (hsm->last_index == ~0)
233 hsm->last_index = ep_index;
239 /** \brief Remove and re-add a cache entry from/to the LRU lists
242 lru_update (hss_main_t *hsm, hss_cache_entry_t *ep, f64 now)
244 lru_remove (hsm, ep);
245 lru_add (hsm, ep, now);
249 start_send_data (hss_session_t *hs, http_status_code_t status)
255 ts = session_get (hs->vpp_session_index, hs->thread_index);
257 msg.type = HTTP_MSG_REPLY;
259 msg.content_type = HTTP_CONTENT_TEXT_HTML;
260 msg.data.len = hs->data_len;
262 if (hs->data_len > hss_main.use_ptr_thresh)
264 msg.data.type = HTTP_MSG_DATA_PTR;
265 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
266 ASSERT (rv == sizeof (msg));
268 uword data = pointer_to_uword (hs->data);
269 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
270 ASSERT (rv == sizeof (sizeof (data)));
275 msg.data.type = HTTP_MSG_DATA_INLINE;
277 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
278 ASSERT (rv == sizeof (msg));
283 rv = svm_fifo_enqueue (ts->tx_fifo, hs->data_len, hs->data);
285 if (rv != hs->data_len)
287 hs->data_offset = rv;
288 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
293 if (svm_fifo_set_event (ts->tx_fifo))
294 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
298 hss_session_send_data (hss_url_handler_args_t *args)
302 hs = hss_session_get (args->sh.thread_index, args->sh.session_index);
304 if (hs->data && hs->free_data)
307 hs->data = args->data;
308 hs->data_len = args->data_len;
309 hs->free_data = args->free_vec_data;
310 start_send_data (hs, args->sc);
314 try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
317 http_status_code_t sc = HTTP_STATUS_OK;
318 hss_url_handler_args_t args = {};
319 uword *p, *url_table;
322 if (!hsm->enable_url_handlers || !request)
325 /* Look for built-in GET / POST handlers */
327 (rt == HTTP_REQ_GET) ? hsm->get_url_handlers : hsm->post_url_handlers;
329 p = hash_get_mem (url_table, request);
335 hs->cache_pool_index = ~0;
337 if (hsm->debug_level > 0)
338 clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
341 args.request = request;
342 args.sh.thread_index = hs->thread_index;
343 args.sh.session_index = hs->session_index;
345 rv = ((hss_url_handler_fn) p[0]) (&args);
347 /* Wait for data from handler */
348 if (rv == HSS_URL_HANDLER_ASYNC)
351 if (rv == HSS_URL_HANDLER_ERROR)
353 clib_warning ("builtin handler %llx hit on %s '%s' but failed!", p[0],
354 (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
355 sc = HTTP_STATUS_NOT_FOUND;
358 hs->data = args.data;
359 hs->data_len = args.data_len;
360 hs->free_data = args.free_vec_data;
362 start_send_data (hs, sc);
365 hss_session_disconnect_transport (hs);
371 handle_request (hss_session_t *hs, http_req_method_t rt, u8 *request)
373 http_status_code_t sc = HTTP_STATUS_OK;
374 hss_main_t *hsm = &hss_main;
375 struct stat _sb, *sb = &_sb;
379 if (!try_url_handler (hsm, hs, rt, request))
384 sc = HTTP_STATUS_NOT_FOUND;
389 * Construct the file to open
390 * Browsers are capable of sporadically including a leading '/'
393 path = format (0, "%s%c", hsm->www_root, 0);
394 else if (request[0] == '/')
395 path = format (0, "%s%s%c", hsm->www_root, request, 0);
397 path = format (0, "%s/%s%c", hsm->www_root, request, 0);
399 if (hsm->debug_level > 0)
400 clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", path);
402 /* Try to find the file. 2x special cases to find index.html */
403 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
404 || sb->st_size < 20 /* file too small */
405 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
407 u32 save_length = vec_len (path) - 1;
408 /* Try appending "index.html"... */
409 _vec_len (path) -= 1;
410 path = format (path, "index.html%c", 0);
411 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
412 || sb->st_size < 20 /* file too small */
413 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
415 _vec_len (path) = save_length;
416 path = format (path, "/index.html%c", 0);
418 /* Send a redirect, otherwise the browser will confuse itself */
419 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
420 || sb->st_size < 20 /* file too small */
421 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
423 sc = HTTP_STATUS_NOT_FOUND;
428 transport_endpoint_t endpoint;
429 transport_proto_t proto;
436 * To make this bit work correctly, we need to know our local
437 * IP address, etc. and send it in the redirect...
441 vec_delete (path, vec_len (hsm->www_root) - 1, 0);
443 ts = session_get (hs->vpp_session_index, hs->thread_index);
444 session_get_endpoint (ts, &endpoint, 1 /* is_local */);
446 local_port = clib_net_to_host_u16 (endpoint.port);
448 proto = session_type_transport_proto (ts->session_type);
450 if ((proto == TRANSPORT_PROTO_TCP && local_port != 80)
451 || (proto == TRANSPORT_PROTO_TLS && local_port != 443))
454 port_str = format (0, ":%u", (u32) local_port);
457 redirect = format (0, "HTTP/1.1 301 Moved Permanently\r\n"
458 "Location: http%s://%U%s%s\r\n\r\n",
459 proto == TRANSPORT_PROTO_TLS ? "s" : "",
460 format_ip46_address, &endpoint.ip,
462 print_port ? port_str : (u8 *) "", path);
463 if (hsm->debug_level > 0)
464 clib_warning ("redirect: %s", redirect);
474 /* find or read the file if we haven't done so yet. */
477 BVT (clib_bihash_kv) kv;
478 hss_cache_entry_t *ce;
482 /* First, try the cache */
483 kv.key = (u64) hs->path;
484 if (BV (clib_bihash_search) (&hsm->name_to_data, &kv, &kv) == 0)
486 if (hsm->debug_level > 1)
487 clib_warning ("lookup '%s' returned %lld", kv.key, kv.value);
491 /* found the data.. */
492 ce = pool_elt_at_index (hsm->cache_pool, kv.value);
494 /* Update the cache entry, mark it in-use */
495 lru_update (hsm, ce, vlib_time_now (vlib_get_main ()));
496 hs->cache_pool_index = ce - hsm->cache_pool;
498 if (hsm->debug_level > 1)
499 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
508 if (hsm->debug_level > 1)
509 clib_warning ("lookup '%s' failed", kv.key, kv.value);
510 /* Need to recycle one (or more cache) entries? */
511 if (hsm->cache_size > hsm->cache_limit)
513 int free_index = hsm->last_index;
515 while (free_index != ~0)
518 ce = pool_elt_at_index (hsm->cache_pool, free_index);
519 free_index = ce->prev_index;
520 /* Which could be in use... */
523 if (hsm->debug_level > 1)
524 clib_warning ("index %d in use refcnt %d",
525 ce - hsm->cache_pool, ce->inuse);
527 kv.key = (u64) (ce->filename);
529 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
530 0 /* is_add */ ) < 0)
532 clib_warning ("LRU delete '%s' FAILED!", ce->filename);
534 else if (hsm->debug_level > 1)
535 clib_warning ("LRU delete '%s' ok", ce->filename);
537 lru_remove (hsm, ce);
538 hsm->cache_size -= vec_len (ce->data);
539 hsm->cache_evictions++;
540 vec_free (ce->filename);
542 if (hsm->debug_level > 1)
543 clib_warning ("pool put index %d", ce - hsm->cache_pool);
544 pool_put (hsm->cache_pool, ce);
545 if (hsm->cache_size < hsm->cache_limit)
551 error = clib_file_contents ((char *) (hs->path), &hs->data);
554 clib_warning ("Error reading '%s'", hs->path);
555 clib_error_report (error);
556 sc = HTTP_STATUS_INTERNAL_ERROR;
560 /* Create a cache entry for it */
561 pool_get_zero (hsm->cache_pool, ce);
562 ce->filename = vec_dup (hs->path);
564 hs->cache_pool_index = ce - hsm->cache_pool;
566 if (hsm->debug_level > 1)
567 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
569 lru_add (hsm, ce, vlib_time_now (vlib_get_main ()));
570 kv.key = (u64) vec_dup (hs->path);
571 kv.value = ce - hsm->cache_pool;
572 /* Add to the lookup table */
573 if (hsm->debug_level > 1)
574 clib_warning ("add '%s' value %lld", kv.key, kv.value);
576 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
577 1 /* is_add */ ) < 0)
579 clib_warning ("BUG: add failed!");
581 hsm->cache_size += vec_len (ce->data);
590 start_send_data (hs, sc);
592 hss_session_disconnect_transport (hs);
598 hss_ts_rx_callback (session_t *ts)
605 hs = hss_session_get (ts->thread_index, ts->opaque);
607 /* Read the http message header */
608 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
609 ASSERT (rv == sizeof (msg));
611 if (msg.type != HTTP_MSG_REQUEST ||
612 (msg.method_type != HTTP_REQ_GET && msg.method_type != HTTP_REQ_POST))
615 start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
622 vec_validate (request, msg.data.len - 1);
623 rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
624 ASSERT (rv == msg.data.len);
627 /* Find and send data */
628 handle_request (hs, msg.method_type, request);
636 hss_ts_tx_callback (session_t *ts)
642 hs = hss_session_get (ts->thread_index, ts->opaque);
643 if (!hs || !hs->data)
646 to_send = hs->data_len - hs->data_offset;
647 rv = svm_fifo_enqueue (ts->tx_fifo, to_send, hs->data + hs->data_offset);
651 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
657 hs->data_offset += rv;
658 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
661 if (svm_fifo_set_event (ts->tx_fifo))
662 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
667 /** \brief Session accept callback
670 hss_ts_accept_callback (session_t *ts)
675 hs = hss_session_alloc (ts->thread_index);
677 hs->vpp_session_index = ts->session_index;
678 hs->vpp_session_handle = session_handle (ts);
680 /* The application sets a threshold for it's fifo to get notified when
681 * additional data can be enqueued. We want to keep the TX fifo reasonably
682 * full, however avoid entering a state where the
683 * fifo is full all the time and small chunks of data are being enqueued
684 * each time. If the fifo is small (under 16K) we set
685 * the threshold to it's size, meaning a notification will be given when the
688 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HSS_FIFO_THRESH);
689 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
691 ts->opaque = hs->session_index;
692 ts->session_state = SESSION_STATE_READY;
697 hss_ts_disconnect_callback (session_t *ts)
699 hss_main_t *hsm = &hss_main;
700 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
702 a->handle = session_handle (ts);
703 a->app_index = hsm->app_index;
704 vnet_disconnect_session (a);
708 hss_ts_reset_callback (session_t *ts)
710 hss_main_t *hsm = &hss_main;
711 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
713 a->handle = session_handle (ts);
714 a->app_index = hsm->app_index;
715 vnet_disconnect_session (a);
719 hss_ts_connected_callback (u32 app_index, u32 api_context, session_t *ts,
722 clib_warning ("called...");
727 hss_add_segment_callback (u32 client_index, u64 segment_handle)
733 hss_ts_cleanup (session_t *s, session_cleanup_ntf_t ntf)
737 if (ntf == SESSION_CLEANUP_TRANSPORT)
740 hs = hss_session_get (s->thread_index, s->opaque);
744 hss_detach_cache_entry (hs);
745 hss_session_free (hs);
748 static session_cb_vft_t hss_cb_vft = {
749 .session_accept_callback = hss_ts_accept_callback,
750 .session_disconnect_callback = hss_ts_disconnect_callback,
751 .session_connected_callback = hss_ts_connected_callback,
752 .add_segment_callback = hss_add_segment_callback,
753 .builtin_app_rx_callback = hss_ts_rx_callback,
754 .builtin_app_tx_callback = hss_ts_tx_callback,
755 .session_reset_callback = hss_ts_reset_callback,
756 .session_cleanup_callback = hss_ts_cleanup,
762 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
763 hss_main_t *hsm = &hss_main;
764 u64 options[APP_OPTIONS_N_OPTIONS];
765 vnet_app_attach_args_t _a, *a = &_a;
766 u32 segment_size = 128 << 20;
768 clib_memset (a, 0, sizeof (*a));
769 clib_memset (options, 0, sizeof (options));
771 if (hsm->private_segment_size)
772 segment_size = hsm->private_segment_size;
774 a->api_client_index = ~0;
775 a->name = format (0, "http_static_server");
776 a->session_cb_vft = &hss_cb_vft;
777 a->options = options;
778 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
779 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
780 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
781 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
782 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
783 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
784 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
785 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
786 a->options[APP_OPTIONS_TLS_ENGINE] = CRYPTO_ENGINE_OPENSSL;
788 if (vnet_application_attach (a))
791 clib_warning ("failed to attach server");
795 hsm->app_index = a->app_index;
797 clib_memset (ck_pair, 0, sizeof (*ck_pair));
798 ck_pair->cert = (u8 *) test_srv_crt_rsa;
799 ck_pair->key = (u8 *) test_srv_key_rsa;
800 ck_pair->cert_len = test_srv_crt_rsa_len;
801 ck_pair->key_len = test_srv_key_rsa_len;
802 vnet_app_add_cert_key_pair (ck_pair);
803 hsm->ckpair_index = ck_pair->index;
809 hss_transport_needs_crypto (transport_proto_t proto)
811 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
812 proto == TRANSPORT_PROTO_QUIC;
818 hss_main_t *hsm = &hss_main;
819 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
820 vnet_listen_args_t _a, *a = &_a;
821 char *uri = "tcp://0.0.0.0/80";
825 clib_memset (a, 0, sizeof (*a));
826 a->app_index = hsm->app_index;
829 uri = (char *) hsm->uri;
831 if (parse_uri (uri, &sep))
834 need_crypto = hss_transport_needs_crypto (a->sep_ext.transport_proto);
836 sep.transport_proto = TRANSPORT_PROTO_HTTP;
837 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
841 session_endpoint_alloc_ext_cfg (&a->sep_ext,
842 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
843 a->sep_ext.ext_cfg->crypto.ckpair_index = hsm->ckpair_index;
846 rv = vnet_listen (a);
849 clib_mem_free (a->sep_ext.ext_cfg);
855 hss_url_handlers_init (hss_main_t *hsm)
857 if (!hsm->get_url_handlers)
859 hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
860 hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
863 hss_builtinurl_json_handlers_init ();
867 hss_create (vlib_main_t *vm)
869 vlib_thread_main_t *vtm = vlib_get_thread_main ();
870 hss_main_t *hsm = &hss_main;
873 num_threads = 1 /* main thread */ + vtm->n_threads;
874 vec_validate (hsm->sessions, num_threads - 1);
876 clib_spinlock_init (&hsm->cache_lock);
880 clib_warning ("failed to attach server");
885 clib_warning ("failed to start listening");
889 /* Init path-to-cache hash table */
890 BV (clib_bihash_init) (&hsm->name_to_data, "http cache", 128, 32 << 20);
892 if (hsm->enable_url_handlers)
893 hss_url_handlers_init (hsm);
898 static clib_error_t *
899 hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
900 vlib_cli_command_t *cmd)
902 unformat_input_t _line_input, *line_input = &_line_input;
903 hss_main_t *hsm = &hss_main;
904 clib_error_t *error = 0;
908 if (hsm->app_index != (u32) ~0)
909 return clib_error_return (0, "http server already running...");
911 hsm->prealloc_fifos = 0;
912 hsm->private_segment_size = 0;
914 /* 10mb cache limit, before LRU occurs */
915 hsm->cache_limit = 10 << 20;
917 /* Get a line of input. */
918 if (!unformat_user (input, unformat_line_input, line_input))
921 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
923 if (unformat (line_input, "www-root %s", &hsm->www_root))
926 if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
928 else if (unformat (line_input, "private-segment-size %U",
929 unformat_memory_size, &seg_size))
930 hsm->private_segment_size = seg_size;
931 else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
932 hsm->fifo_size <<= 10;
933 else if (unformat (line_input, "cache-size %U", unformat_memory_size,
936 else if (unformat (line_input, "uri %s", &hsm->uri))
938 else if (unformat (line_input, "debug %d", &hsm->debug_level))
940 else if (unformat (line_input, "debug"))
941 hsm->debug_level = 1;
942 else if (unformat (line_input, "ptr-thresh %U", unformat_memory_size,
943 &hsm->use_ptr_thresh))
945 else if (unformat (line_input, "url-handlers"))
946 hsm->enable_url_handlers = 1;
949 error = clib_error_return (0, "unknown input `%U'",
950 format_unformat_error, line_input);
954 unformat_free (line_input);
961 if (hsm->www_root == 0 && !hsm->enable_url_handlers)
963 error = clib_error_return (0, "Must set www-root or url-handlers");
967 if (hsm->cache_limit < (128 << 10))
969 error = clib_error_return (0, "cache-size must be at least 128kb");
970 vec_free (hsm->www_root);
974 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
976 if ((rv = hss_create (vm)))
978 error = clib_error_return (0, "server_create returned %d", rv);
979 vec_free (hsm->www_root);
988 * Enable the static http server
991 * This command enables the static http server. Only the www-root
992 * parameter is required
994 * http static server www-root /tmp/www uri tcp://0.0.0.0/80 cache-size 2m
996 * @cliexcmd{http static server www-root <path> [prealloc-fios <nn>]
997 * [private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]}
999 VLIB_CLI_COMMAND (hss_create_command, static) = {
1000 .path = "http static server",
1002 "http static server www-root <path> [prealloc-fifos <nn>]\n"
1003 "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
1004 "[ptr-thresh <nn>] [url-handlers] [debug [nn]]\n",
1005 .function = hss_create_command_fn,
1008 /** \brief format a file cache entry
1011 format_hss_cache_entry (u8 *s, va_list *args)
1013 hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
1014 f64 now = va_arg (*args, f64);
1019 s = format (s, "%40s%12s%20s", "File", "Size", "Age");
1022 s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
1023 now - ep->last_used);
1028 format_hss_session (u8 *s, va_list *args)
1030 hss_session_t *hs = va_arg (*args, hss_session_t *);
1031 int __clib_unused verbose = va_arg (*args, int);
1033 s = format (s, "\n path %s, data length %u, data_offset %u",
1034 hs->path ? hs->path : (u8 *) "[none]", hs->data_len,
1039 static clib_error_t *
1040 hss_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
1041 vlib_cli_command_t *cmd)
1043 hss_main_t *hsm = &hss_main;
1044 hss_cache_entry_t *ep, **entries = 0;
1047 int show_sessions = 0;
1051 if (hsm->www_root == 0)
1052 return clib_error_return (0, "Static server disabled");
1054 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1056 if (unformat (input, "verbose %d", &verbose))
1058 else if (unformat (input, "verbose"))
1060 else if (unformat (input, "cache"))
1062 else if (unformat (input, "sessions"))
1068 if ((show_cache + show_sessions) == 0)
1069 return clib_error_return (0, "specify one or more of cache, sessions");
1076 (vm, "www_root %s, cache size %lld bytes, limit %lld bytes, "
1078 hsm->www_root, hsm->cache_size, hsm->cache_limit,
1079 hsm->cache_evictions);
1083 now = vlib_time_now (vm);
1085 vlib_cli_output (vm, "%U", format_hss_cache_entry, 0 /* header */, now);
1087 for (index = hsm->first_index; index != ~0;)
1089 ep = pool_elt_at_index (hsm->cache_pool, index);
1090 index = ep->next_index;
1091 vlib_cli_output (vm, "%U", format_hss_cache_entry, ep, now);
1094 vlib_cli_output (vm, "%40s%12lld", "Total Size", hsm->cache_size);
1101 u32 *session_indices = 0;
1107 for (i = 0; i < vec_len (hsm->sessions); i++)
1109 pool_foreach (hs, hsm->sessions[i])
1111 vec_add1 (session_indices, hs - hsm->sessions[i]);
1114 for (j = 0; j < vec_len (session_indices); j++)
1117 vm, "%U", format_hss_session,
1118 pool_elt_at_index (hsm->sessions[i], session_indices[j]),
1121 vec_reset_length (session_indices);
1123 hss_cache_unlock ();
1124 vec_free (session_indices);
1130 * Display static http server cache statistics
1133 * This command shows the contents of the static http server cache
1135 * show http static server
1137 * @cliexcmd{show http static server sessions cache [verbose [nn]]}
1139 VLIB_CLI_COMMAND (hss_show_command, static) = {
1140 .path = "show http static server",
1141 .short_help = "show http static server sessions cache [verbose [<nn>]]",
1142 .function = hss_show_command_fn,
1145 static clib_error_t *
1146 hss_clear_cache_command_fn (vlib_main_t *vm, unformat_input_t *input,
1147 vlib_cli_command_t *cmd)
1149 hss_main_t *hsm = &hss_main;
1150 hss_cache_entry_t *ce;
1153 BVT (clib_bihash_kv) kv;
1155 if (hsm->www_root == 0)
1156 return clib_error_return (0, "Static server disabled");
1160 /* Walk the LRU list to find active entries */
1161 free_index = hsm->last_index;
1162 while (free_index != ~0)
1164 ce = pool_elt_at_index (hsm->cache_pool, free_index);
1165 free_index = ce->prev_index;
1166 /* Which could be in use... */
1170 free_index = ce->next_index;
1173 kv.key = (u64) (ce->filename);
1175 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
1176 0 /* is_add */ ) < 0)
1178 clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
1181 lru_remove (hsm, ce);
1182 hsm->cache_size -= vec_len (ce->data);
1183 hsm->cache_evictions++;
1184 vec_free (ce->filename);
1185 vec_free (ce->data);
1186 if (hsm->debug_level > 1)
1187 clib_warning ("pool put index %d", ce - hsm->cache_pool);
1188 pool_put (hsm->cache_pool, ce);
1189 free_index = hsm->last_index;
1191 hss_cache_unlock ();
1193 vlib_cli_output (vm, "Note: %d busy items still in cache...", busy_items);
1195 vlib_cli_output (vm, "Cache cleared...");
1200 * Clear the static http server cache, to force the server to
1201 * reload content from backing files
1204 * This command clear the static http server cache
1206 * clear http static cache
1208 * @cliexcmd{clear http static cache}
1210 VLIB_CLI_COMMAND (clear_hss_cache_command, static) = {
1211 .path = "clear http static cache",
1212 .short_help = "clear http static cache",
1213 .function = hss_clear_cache_command_fn,
1216 static clib_error_t *
1217 hss_main_init (vlib_main_t *vm)
1219 hss_main_t *hsm = &hss_main;
1221 hsm->app_index = ~0;
1222 hsm->vlib_main = vm;
1223 hsm->first_index = hsm->last_index = ~0;
1228 VLIB_INIT_FUNCTION (hss_main_init);
1231 * fd.io coding-style-patch-verification: ON
1234 * eval: (c-set-style "gnu")