2 * Copyright (c) 2017-2019 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 <vnet/vnet.h>
17 #include <vnet/session/application.h>
18 #include <vnet/session/application_interface.h>
19 #include <vnet/session/session.h>
20 #include <vppinfra/unix.h>
21 #include <sys/types.h>
24 #include <http_static/http_static.h>
26 #include <vppinfra/bihash_template.c>
27 #include <http/http.h>
29 /** @file static_server.c
30 * Static http server, sufficient to
31 * serve .html / .css / .js content.
33 /*? %%clicmd:group_label Static HTTP Server %% ?*/
35 #define HSS_FIFO_THRESH (16 << 10)
42 clib_spinlock_lock (&hss_main.cache_lock);
46 hss_cache_unlock (void)
48 clib_spinlock_unlock (&hss_main.cache_lock);
51 static http_session_t *
52 hss_session_alloc (u32 thread_index)
54 hss_main_t *hsm = &hss_main;
57 pool_get_zero (hsm->sessions[thread_index], hs);
58 hs->session_index = hs - hsm->sessions[thread_index];
59 hs->thread_index = thread_index;
60 hs->timer_handle = ~0;
61 hs->cache_pool_index = ~0;
65 static http_session_t *
66 hss_session_get (u32 thread_index, u32 hs_index)
68 hss_main_t *hsm = &hss_main;
69 if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
71 return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
75 hss_session_free (http_session_t *hs)
77 hss_main_t *hsm = &hss_main;
79 pool_put (hsm->sessions[hs->thread_index], hs);
83 u32 save_thread_index;
84 save_thread_index = hs->thread_index;
85 /* Poison the entry, preserve timer state and thread index */
86 memset (hs, 0xfa, sizeof (*hs));
87 hs->timer_handle = ~0;
88 hs->thread_index = save_thread_index;
92 /** \brief Detach cache entry from session
95 hss_detach_cache_entry (http_session_t *hs)
97 hss_main_t *hsm = &hss_main;
98 file_data_cache_t *ep;
101 * Decrement cache pool entry reference count
102 * Note that if e.g. a file lookup fails, the cache pool index
105 if (hs->cache_pool_index != ~0)
107 ep = pool_elt_at_index (hsm->cache_pool, hs->cache_pool_index);
109 if (hsm->debug_level > 1)
110 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
113 hs->cache_pool_index = ~0;
122 /** \brief Disconnect a session
125 hss_session_disconnect_transport (http_session_t *hs)
127 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
128 a->handle = hs->vpp_session_handle;
129 a->app_index = hss_main.app_index;
130 vnet_disconnect_session (a);
133 /** \brief Sanity-check the forward and reverse LRU lists
136 lru_validate (hss_main_t *hsm)
142 file_data_cache_t *ep;
144 last_timestamp = 1e70;
145 for (i = 1, index = hsm->first_index; index != ~0;)
147 ep = pool_elt_at_index (hsm->cache_pool, index);
148 index = ep->next_index;
149 /* Timestamps should be smaller (older) as we walk the fwd list */
150 if (ep->last_used > last_timestamp)
152 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
153 ep - hsm->cache_pool, i,
154 ep->last_used, last_timestamp);
156 last_timestamp = ep->last_used;
160 last_timestamp = 0.0;
161 for (i = 1, index = hsm->last_index; index != ~0;)
163 ep = pool_elt_at_index (hsm->cache_pool, index);
164 index = ep->prev_index;
165 /* Timestamps should be larger (newer) as we walk the rev list */
166 if (ep->last_used < last_timestamp)
168 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
169 ep - hsm->cache_pool, i,
170 ep->last_used, last_timestamp);
172 last_timestamp = ep->last_used;
178 /** \brief Remove a data cache entry from the LRU lists
181 lru_remove (hss_main_t *hsm, file_data_cache_t *ep)
183 file_data_cache_t *next_ep, *prev_ep;
188 ep_index = ep - hsm->cache_pool;
190 /* Deal with list heads */
191 if (ep_index == hsm->first_index)
192 hsm->first_index = ep->next_index;
193 if (ep_index == hsm->last_index)
194 hsm->last_index = ep->prev_index;
197 if (ep->next_index != ~0)
199 next_ep = pool_elt_at_index (hsm->cache_pool, ep->next_index);
200 next_ep->prev_index = ep->prev_index;
203 if (ep->prev_index != ~0)
205 prev_ep = pool_elt_at_index (hsm->cache_pool, ep->prev_index);
206 prev_ep->next_index = ep->next_index;
211 /** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
214 lru_add (hss_main_t *hsm, file_data_cache_t *ep, f64 now)
216 file_data_cache_t *next_ep;
221 ep_index = ep - hsm->cache_pool;
224 * Re-add at the head of the forward LRU list,
225 * tail of the reverse LRU list
227 if (hsm->first_index != ~0)
229 next_ep = pool_elt_at_index (hsm->cache_pool, hsm->first_index);
230 next_ep->prev_index = ep_index;
235 /* ep now the new head of the LRU forward list */
236 ep->next_index = hsm->first_index;
237 hsm->first_index = ep_index;
239 /* single session case: also the tail of the reverse LRU list */
240 if (hsm->last_index == ~0)
241 hsm->last_index = ep_index;
247 /** \brief Remove and re-add a cache entry from/to the LRU lists
250 lru_update (hss_main_t *hsm, file_data_cache_t *ep, f64 now)
252 lru_remove (hsm, ep);
253 lru_add (hsm, ep, now);
256 /** \brief Register a builtin GET or POST handler
258 __clib_export void http_static_server_register_builtin_handler
259 (void *fp, char *url, int request_type)
261 hss_main_t *hsm = &hss_main;
262 uword *p, *builtin_table;
264 builtin_table = (request_type == HTTP_BUILTIN_METHOD_GET)
265 ? hsm->get_url_handlers : hsm->post_url_handlers;
267 p = hash_get_mem (builtin_table, url);
271 clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored",
272 (request_type == HTTP_BUILTIN_METHOD_GET) ?
273 "GET" : "POST", url);
277 hash_set_mem (builtin_table, url, (uword) fp);
280 * Need to update the hash table pointer in http_static_server_main
281 * in case we just expanded it...
283 if (request_type == HTTP_BUILTIN_METHOD_GET)
284 hsm->get_url_handlers = builtin_table;
286 hsm->post_url_handlers = builtin_table;
290 start_send_data (http_session_t *hs, http_status_code_t status)
296 ts = session_get (hs->vpp_session_index, hs->thread_index);
298 msg.type = HTTP_MSG_REPLY;
300 msg.content_type = HTTP_CONTENT_TEXT_HTML;
301 msg.data.len = vec_len (hs->data);
303 if (vec_len (hs->data) > hss_main.use_ptr_thresh)
305 msg.data.type = HTTP_MSG_DATA_PTR;
306 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
307 ASSERT (rv == sizeof (msg));
309 uword data = pointer_to_uword (hs->data);
310 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
311 ASSERT (rv == sizeof (sizeof (data)));
316 msg.data.type = HTTP_MSG_DATA_INLINE;
318 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
319 ASSERT (rv == sizeof (msg));
324 rv = svm_fifo_enqueue (ts->tx_fifo, vec_len (hs->data), hs->data);
326 if (rv != vec_len (hs->data))
328 hs->data_offset = rv;
329 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
334 if (svm_fifo_set_event (ts->tx_fifo))
335 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
339 find_data (http_session_t *hs, http_req_method_t rt, u8 *request)
341 hss_main_t *hsm = &hss_main;
343 struct stat _sb, *sb = &_sb;
345 u8 request_type = HTTP_BUILTIN_METHOD_GET;
346 uword *p, *builtin_table;
347 http_status_code_t sc = HTTP_STATUS_OK;
349 request_type = rt == HTTP_REQ_GET ? request_type : HTTP_BUILTIN_METHOD_POST;
352 * Construct the file to open
353 * Browsers are capable of sporadically including a leading '/'
355 if (request[0] == '/')
356 path = format (0, "%s%s%c", hsm->www_root, request, 0);
358 path = format (0, "%s/%s%c", hsm->www_root, request, 0);
360 if (hsm->debug_level > 0)
361 clib_warning ("%s '%s'", (request_type) == HTTP_BUILTIN_METHOD_GET ?
362 "GET" : "POST", path);
364 /* Look for built-in GET / POST handlers */
365 builtin_table = (request_type == HTTP_BUILTIN_METHOD_GET) ?
366 hsm->get_url_handlers : hsm->post_url_handlers;
368 p = hash_get_mem (builtin_table, request);
373 int (*fp) (http_builtin_method_type_t, u8 *, http_session_t *);
376 rv = (*fp) (request_type, request, hs);
379 clib_warning ("builtin handler %llx hit on %s '%s' but failed!",
380 p[0], (request_type == HTTP_BUILTIN_METHOD_GET) ?
381 "GET" : "POST", request);
383 sc = HTTP_STATUS_NOT_FOUND;
388 /* Try to find the file. 2x special cases to find index.html */
389 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
390 || sb->st_size < 20 /* file too small */
391 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
393 u32 save_length = vec_len (path) - 1;
394 /* Try appending "index.html"... */
395 _vec_len (path) -= 1;
396 path = format (path, "index.html%c", 0);
397 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
398 || sb->st_size < 20 /* file too small */
399 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
401 _vec_len (path) = save_length;
402 path = format (path, "/index.html%c", 0);
404 /* Send a redirect, otherwise the browser will confuse itself */
405 if (stat ((char *) path, sb) < 0 /* cant even stat the file */
406 || sb->st_size < 20 /* file too small */
407 || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
409 sc = HTTP_STATUS_NOT_FOUND;
414 transport_endpoint_t endpoint;
415 transport_proto_t proto;
422 * To make this bit work correctly, we need to know our local
423 * IP address, etc. and send it in the redirect...
427 vec_delete (path, vec_len (hsm->www_root) - 1, 0);
429 ts = session_get (hs->vpp_session_index, hs->thread_index);
430 session_get_endpoint (ts, &endpoint, 1 /* is_local */);
432 local_port = clib_net_to_host_u16 (endpoint.port);
434 proto = session_type_transport_proto (ts->session_type);
436 if ((proto == TRANSPORT_PROTO_TCP && local_port != 80)
437 || (proto == TRANSPORT_PROTO_TLS && local_port != 443))
440 port_str = format (0, ":%u", (u32) local_port);
443 redirect = format (0, "HTTP/1.1 301 Moved Permanently\r\n"
444 "Location: http%s://%U%s%s\r\n\r\n",
445 proto == TRANSPORT_PROTO_TLS ? "s" : "",
446 format_ip46_address, &endpoint.ip,
448 print_port ? port_str : (u8 *) "", path);
449 if (hsm->debug_level > 0)
450 clib_warning ("redirect: %s", redirect);
460 /* find or read the file if we haven't done so yet. */
463 BVT (clib_bihash_kv) kv;
464 file_data_cache_t *dp;
468 /* First, try the cache */
469 kv.key = (u64) hs->path;
470 if (BV (clib_bihash_search) (&hsm->name_to_data, &kv, &kv) == 0)
472 if (hsm->debug_level > 1)
473 clib_warning ("lookup '%s' returned %lld", kv.key, kv.value);
477 /* found the data.. */
478 dp = pool_elt_at_index (hsm->cache_pool, kv.value);
480 /* Update the cache entry, mark it in-use */
481 lru_update (hsm, dp, vlib_time_now (vlib_get_main ()));
482 hs->cache_pool_index = dp - hsm->cache_pool;
484 if (hsm->debug_level > 1)
485 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
494 if (hsm->debug_level > 1)
495 clib_warning ("lookup '%s' failed", kv.key, kv.value);
496 /* Need to recycle one (or more cache) entries? */
497 if (hsm->cache_size > hsm->cache_limit)
499 int free_index = hsm->last_index;
501 while (free_index != ~0)
504 dp = pool_elt_at_index (hsm->cache_pool, free_index);
505 free_index = dp->prev_index;
506 /* Which could be in use... */
509 if (hsm->debug_level > 1)
510 clib_warning ("index %d in use refcnt %d",
511 dp - hsm->cache_pool, dp->inuse);
514 kv.key = (u64) (dp->filename);
516 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
517 0 /* is_add */ ) < 0)
519 clib_warning ("LRU delete '%s' FAILED!", dp->filename);
521 else if (hsm->debug_level > 1)
522 clib_warning ("LRU delete '%s' ok", dp->filename);
524 lru_remove (hsm, dp);
525 hsm->cache_size -= vec_len (dp->data);
526 hsm->cache_evictions++;
527 vec_free (dp->filename);
529 if (hsm->debug_level > 1)
530 clib_warning ("pool put index %d", dp - hsm->cache_pool);
531 pool_put (hsm->cache_pool, dp);
532 if (hsm->cache_size < hsm->cache_limit)
538 error = clib_file_contents ((char *) (hs->path), &hs->data);
541 clib_warning ("Error reading '%s'", hs->path);
542 clib_error_report (error);
543 sc = HTTP_STATUS_INTERNAL_ERROR;
547 /* Create a cache entry for it */
548 pool_get_zero (hsm->cache_pool, dp);
549 dp->filename = vec_dup (hs->path);
551 hs->cache_pool_index = dp - hsm->cache_pool;
553 if (hsm->debug_level > 1)
554 clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
556 lru_add (hsm, dp, vlib_time_now (vlib_get_main ()));
557 kv.key = (u64) vec_dup (hs->path);
558 kv.value = dp - hsm->cache_pool;
559 /* Add to the lookup table */
560 if (hsm->debug_level > 1)
561 clib_warning ("add '%s' value %lld", kv.key, kv.value);
563 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
564 1 /* is_add */ ) < 0)
566 clib_warning ("BUG: add failed!");
568 hsm->cache_size += vec_len (dp->data);
581 hss_ts_rx_callback (session_t *ts)
587 http_status_code_t sc;
589 hs = hss_session_get (ts->thread_index, ts->opaque);
591 /* Read the http message header */
592 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
593 ASSERT (rv == sizeof (msg));
595 if (msg.type != HTTP_MSG_REQUEST ||
596 (msg.method_type != HTTP_REQ_GET && msg.method_type != HTTP_REQ_POST))
599 start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
604 vec_validate (request, msg.data.len - 1);
605 rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
606 ASSERT (rv == msg.data.len);
608 /* Find and send data */
609 sc = find_data (hs, msg.method_type, request);
610 start_send_data (hs, sc);
614 hss_session_disconnect_transport (hs);
620 hss_ts_tx_callback (session_t *ts)
626 hs = hss_session_get (ts->thread_index, ts->opaque);
627 if (!hs || !hs->data)
630 to_send = vec_len (hs->data) - hs->data_offset;
631 rv = svm_fifo_enqueue (ts->tx_fifo, to_send, hs->data + hs->data_offset);
635 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
641 hs->data_offset += rv;
642 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
645 if (svm_fifo_set_event (ts->tx_fifo))
646 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
651 /** \brief Session accept callback
654 hss_ts_accept_callback (session_t *ts)
659 hs = hss_session_alloc (ts->thread_index);
661 hs->rx_fifo = ts->rx_fifo;
662 hs->tx_fifo = ts->tx_fifo;
663 hs->vpp_session_index = ts->session_index;
664 hs->vpp_session_handle = session_handle (ts);
665 hs->session_state = HTTP_STATE_ESTABLISHED;
667 /* The application sets a threshold for it's fifo to get notified when
668 * additional data can be enqueued. We want to keep the TX fifo reasonably
669 * full, however avoid entering a state where the
670 * fifo is full all the time and small chunks of data are being enqueued
671 * each time. If the fifo is small (under 16K) we set
672 * the threshold to it's size, meaning a notification will be given when the
675 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HSS_FIFO_THRESH);
676 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
678 ts->opaque = hs->session_index;
679 ts->session_state = SESSION_STATE_READY;
684 hss_ts_disconnect_callback (session_t *ts)
686 hss_main_t *hsm = &hss_main;
687 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
689 a->handle = session_handle (ts);
690 a->app_index = hsm->app_index;
691 vnet_disconnect_session (a);
695 hss_ts_reset_callback (session_t *ts)
697 hss_main_t *hsm = &hss_main;
698 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
700 a->handle = session_handle (ts);
701 a->app_index = hsm->app_index;
702 vnet_disconnect_session (a);
706 hss_ts_connected_callback (u32 app_index, u32 api_context, session_t *ts,
709 clib_warning ("called...");
714 hss_add_segment_callback (u32 client_index, u64 segment_handle)
720 hss_ts_cleanup (session_t *s, session_cleanup_ntf_t ntf)
724 if (ntf == SESSION_CLEANUP_TRANSPORT)
727 hs = hss_session_get (s->thread_index, s->opaque);
731 hss_detach_cache_entry (hs);
732 vec_free (hs->rx_buf);
733 hss_session_free (hs);
736 static session_cb_vft_t hss_cb_vft = {
737 .session_accept_callback = hss_ts_accept_callback,
738 .session_disconnect_callback = hss_ts_disconnect_callback,
739 .session_connected_callback = hss_ts_connected_callback,
740 .add_segment_callback = hss_add_segment_callback,
741 .builtin_app_rx_callback = hss_ts_rx_callback,
742 .builtin_app_tx_callback = hss_ts_tx_callback,
743 .session_reset_callback = hss_ts_reset_callback,
744 .session_cleanup_callback = hss_ts_cleanup,
748 http_static_server_attach ()
750 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
751 hss_main_t *hsm = &hss_main;
752 u64 options[APP_OPTIONS_N_OPTIONS];
753 vnet_app_attach_args_t _a, *a = &_a;
754 u32 segment_size = 128 << 20;
756 clib_memset (a, 0, sizeof (*a));
757 clib_memset (options, 0, sizeof (options));
759 if (hsm->private_segment_size)
760 segment_size = hsm->private_segment_size;
762 a->api_client_index = ~0;
763 a->name = format (0, "test_http_static_server");
764 a->session_cb_vft = &hss_cb_vft;
765 a->options = options;
766 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
767 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
768 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
769 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
770 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
771 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
772 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
773 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
774 a->options[APP_OPTIONS_TLS_ENGINE] = CRYPTO_ENGINE_OPENSSL;
776 if (vnet_application_attach (a))
779 clib_warning ("failed to attach server");
783 hsm->app_index = a->app_index;
785 clib_memset (ck_pair, 0, sizeof (*ck_pair));
786 ck_pair->cert = (u8 *) test_srv_crt_rsa;
787 ck_pair->key = (u8 *) test_srv_key_rsa;
788 ck_pair->cert_len = test_srv_crt_rsa_len;
789 ck_pair->key_len = test_srv_key_rsa_len;
790 vnet_app_add_cert_key_pair (ck_pair);
791 hsm->ckpair_index = ck_pair->index;
797 hss_transport_needs_crypto (transport_proto_t proto)
799 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
800 proto == TRANSPORT_PROTO_QUIC;
806 hss_main_t *hsm = &hss_main;
807 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
808 vnet_listen_args_t _a, *a = &_a;
809 char *uri = "tcp://0.0.0.0/80";
813 clib_memset (a, 0, sizeof (*a));
814 a->app_index = hsm->app_index;
817 uri = (char *) hsm->uri;
819 if (parse_uri (uri, &sep))
822 need_crypto = hss_transport_needs_crypto (a->sep_ext.transport_proto);
824 sep.transport_proto = TRANSPORT_PROTO_HTTP;
825 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
829 session_endpoint_alloc_ext_cfg (&a->sep_ext,
830 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
831 a->sep_ext.ext_cfg->crypto.ckpair_index = hsm->ckpair_index;
834 rv = vnet_listen (a);
837 clib_mem_free (a->sep_ext.ext_cfg);
843 hss_create (vlib_main_t *vm)
845 vlib_thread_main_t *vtm = vlib_get_thread_main ();
846 hss_main_t *hsm = &hss_main;
849 num_threads = 1 /* main thread */ + vtm->n_threads;
850 vec_validate (hsm->sessions, num_threads - 1);
852 clib_spinlock_init (&hsm->cache_lock);
854 if (http_static_server_attach ())
856 clib_warning ("failed to attach server");
861 clib_warning ("failed to start listening");
865 /* Init path-to-cache hash table */
866 BV (clib_bihash_init) (&hsm->name_to_data, "http cache", 128, 32 << 20);
868 hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
869 hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
874 /** \brief API helper function for vl_api_http_static_enable_t messages
877 hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
878 u32 private_segment_size, u8 *www_root, u8 *uri)
880 hss_main_t *hsm = &hss_main;
883 hsm->fifo_size = fifo_size;
884 hsm->cache_limit = cache_limit;
885 hsm->prealloc_fifos = prealloc_fifos;
886 hsm->private_segment_size = private_segment_size;
887 hsm->www_root = format (0, "%s%c", www_root, 0);
888 hsm->uri = format (0, "%s%c", uri, 0);
890 if (vec_len (hsm->www_root) < 2)
891 return VNET_API_ERROR_INVALID_VALUE;
893 if (hsm->app_index != ~0)
894 return VNET_API_ERROR_APP_ALREADY_ATTACHED;
896 vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */ );
898 rv = hss_create (hsm->vlib_main);
904 vec_free (hsm->www_root);
906 return VNET_API_ERROR_INIT_FAILED;
911 static clib_error_t *
912 hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
913 vlib_cli_command_t *cmd)
915 hss_main_t *hsm = &hss_main;
916 unformat_input_t _line_input, *line_input = &_line_input;
921 hsm->prealloc_fifos = 0;
922 hsm->private_segment_size = 0;
924 /* 10mb cache limit, before LRU occurs */
925 hsm->cache_limit = 10 << 20;
927 /* Get a line of input. */
928 if (!unformat_user (input, unformat_line_input, line_input))
931 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
933 if (unformat (line_input, "www-root %s", &www_root))
936 if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
938 else if (unformat (line_input, "private-segment-size %U",
939 unformat_memory_size, &seg_size))
941 if (seg_size >= 0x100000000ULL)
943 vlib_cli_output (vm, "private segment size %llu, too large",
947 hsm->private_segment_size = seg_size;
949 else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
950 hsm->fifo_size <<= 10;
951 else if (unformat (line_input, "cache-size %U", unformat_memory_size,
954 if (hsm->cache_limit < (128 << 10))
956 return clib_error_return (0,
957 "cache-size must be at least 128kb");
961 else if (unformat (line_input, "uri %s", &hsm->uri))
963 else if (unformat (line_input, "debug %d", &hsm->debug_level))
965 else if (unformat (line_input, "debug"))
966 hsm->debug_level = 1;
967 else if (unformat (line_input, "ptr-thresh %U", unformat_memory_size,
968 &hsm->use_ptr_thresh))
971 return clib_error_return (0, "unknown input `%U'",
972 format_unformat_error, line_input);
974 unformat_free (line_input);
979 return clib_error_return (0, "Must specify www-root <path>");
982 if (hsm->app_index != (u32) ~0)
985 return clib_error_return (0, "http server already running...");
988 hsm->www_root = www_root;
990 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
992 rv = hss_create (vm);
998 vec_free (hsm->www_root);
999 return clib_error_return (0, "server_create returned %d", rv);
1005 * Enable the static http server
1008 * This command enables the static http server. Only the www-root
1009 * parameter is required
1011 * http static server www-root /tmp/www uri tcp://0.0.0.0/80 cache-size 2m
1013 * @cliexcmd{http static server www-root <path> [prealloc-fios <nn>]
1014 * [private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]}
1017 VLIB_CLI_COMMAND (hss_create_command, static) = {
1018 .path = "http static server",
1020 "http static server www-root <path> [prealloc-fifos <nn>]\n"
1021 "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
1022 "[ptr-thresh <nn>][debug [nn]]\n",
1023 .function = hss_create_command_fn,
1027 /** \brief format a file cache entry
1030 format_hss_cache_entry (u8 *s, va_list *args)
1032 file_data_cache_t *ep = va_arg (*args, file_data_cache_t *);
1033 f64 now = va_arg (*args, f64);
1038 s = format (s, "%40s%12s%20s", "File", "Size", "Age");
1041 s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
1042 now - ep->last_used);
1047 format_hss_session_state (u8 *s, va_list *args)
1049 http_session_state_t state = va_arg (*args, http_session_state_t);
1050 char *state_string = "bogus!";
1054 case HTTP_STATE_CLOSED:
1055 state_string = "closed";
1057 case HTTP_STATE_ESTABLISHED:
1058 state_string = "established";
1060 case HTTP_STATE_OK_SENT:
1061 state_string = "ok sent";
1063 case HTTP_STATE_SEND_MORE_DATA:
1064 state_string = "send more data";
1070 return format (s, "%s", state_string);
1074 format_hss_session (u8 *s, va_list *args)
1076 http_session_t *hs = va_arg (*args, http_session_t *);
1077 int verbose = va_arg (*args, int);
1079 s = format (s, "[%d]: state %U", hs->session_index, format_hss_session_state,
1083 s = format (s, "\n path %s, data length %u, data_offset %u",
1084 hs->path ? hs->path : (u8 *) "[none]",
1085 vec_len (hs->data), hs->data_offset);
1090 static clib_error_t *
1091 hss_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
1092 vlib_cli_command_t *cmd)
1094 hss_main_t *hsm = &hss_main;
1095 file_data_cache_t *ep, **entries = 0;
1098 int show_sessions = 0;
1102 if (hsm->www_root == 0)
1103 return clib_error_return (0, "Static server disabled");
1105 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1107 if (unformat (input, "verbose %d", &verbose))
1109 else if (unformat (input, "verbose"))
1111 else if (unformat (input, "cache"))
1113 else if (unformat (input, "sessions"))
1119 if ((show_cache + show_sessions) == 0)
1120 return clib_error_return (0, "specify one or more of cache, sessions");
1127 (vm, "www_root %s, cache size %lld bytes, limit %lld bytes, "
1129 hsm->www_root, hsm->cache_size, hsm->cache_limit,
1130 hsm->cache_evictions);
1134 now = vlib_time_now (vm);
1136 vlib_cli_output (vm, "%U", format_hss_cache_entry, 0 /* header */, now);
1138 for (index = hsm->first_index; index != ~0;)
1140 ep = pool_elt_at_index (hsm->cache_pool, index);
1141 index = ep->next_index;
1142 vlib_cli_output (vm, "%U", format_hss_cache_entry, ep, now);
1145 vlib_cli_output (vm, "%40s%12lld", "Total Size", hsm->cache_size);
1152 u32 *session_indices = 0;
1158 for (i = 0; i < vec_len (hsm->sessions); i++)
1161 pool_foreach (hs, hsm->sessions[i])
1163 vec_add1 (session_indices, hs - hsm->sessions[i]);
1167 for (j = 0; j < vec_len (session_indices); j++)
1170 vm, "%U", format_hss_session,
1171 pool_elt_at_index (hsm->sessions[i], session_indices[j]),
1174 vec_reset_length (session_indices);
1176 hss_cache_unlock ();
1177 vec_free (session_indices);
1183 * Display static http server cache statistics
1186 * This command shows the contents of the static http server cache
1188 * show http static server
1190 * @cliexcmd{show http static server sessions cache [verbose [nn]]}
1193 VLIB_CLI_COMMAND (hss_show_command, static) = {
1194 .path = "show http static server",
1195 .short_help = "show http static server sessions cache [verbose [<nn>]]",
1196 .function = hss_show_command_fn,
1200 static clib_error_t *
1201 hss_clear_cache_command_fn (vlib_main_t *vm, unformat_input_t *input,
1202 vlib_cli_command_t *cmd)
1204 hss_main_t *hsm = &hss_main;
1205 file_data_cache_t *dp;
1208 BVT (clib_bihash_kv) kv;
1210 if (hsm->www_root == 0)
1211 return clib_error_return (0, "Static server disabled");
1215 /* Walk the LRU list to find active entries */
1216 free_index = hsm->last_index;
1217 while (free_index != ~0)
1219 dp = pool_elt_at_index (hsm->cache_pool, free_index);
1220 free_index = dp->prev_index;
1221 /* Which could be in use... */
1225 free_index = dp->next_index;
1228 kv.key = (u64) (dp->filename);
1230 if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
1231 0 /* is_add */ ) < 0)
1233 clib_warning ("BUG: cache clear delete '%s' FAILED!", dp->filename);
1236 lru_remove (hsm, dp);
1237 hsm->cache_size -= vec_len (dp->data);
1238 hsm->cache_evictions++;
1239 vec_free (dp->filename);
1240 vec_free (dp->data);
1241 if (hsm->debug_level > 1)
1242 clib_warning ("pool put index %d", dp - hsm->cache_pool);
1243 pool_put (hsm->cache_pool, dp);
1244 free_index = hsm->last_index;
1246 hss_cache_unlock ();
1248 vlib_cli_output (vm, "Note: %d busy items still in cache...", busy_items);
1250 vlib_cli_output (vm, "Cache cleared...");
1255 * Clear the static http server cache, to force the server to
1256 * reload content from backing files
1259 * This command clear the static http server cache
1261 * clear http static cache
1263 * @cliexcmd{clear http static cache}
1266 VLIB_CLI_COMMAND (clear_hss_cache_command, static) = {
1267 .path = "clear http static cache",
1268 .short_help = "clear http static cache",
1269 .function = hss_clear_cache_command_fn,
1273 static clib_error_t *
1274 hss_main_init (vlib_main_t *vm)
1276 hss_main_t *hsm = &hss_main;
1278 hsm->app_index = ~0;
1279 hsm->vlib_main = vm;
1280 hsm->first_index = hsm->last_index = ~0;
1285 VLIB_INIT_FUNCTION (hss_main_init);
1288 * fd.io coding-style-patch-verification: ON
1291 * eval: (c-set-style "gnu")