summaryrefslogtreecommitdiffstats
path: root/src/vnet/flow/flow_report.h
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2017-02-28 15:15:56 -0500
committerDamjan Marion <dmarion.lists@gmail.com>2017-03-01 20:25:48 +0000
commit68b0fb0c620c7451ef1a6380c43c39de6614db51 (patch)
treef4188fa09723152f3ebfcebbbe4cacad903e0cf1 /src/vnet/flow/flow_report.h
parentf869028740aaebeb0375077d4d84fa07a17fff1a (diff)
VPP-598: tcp stack initial commit
Change-Id: I49e5ce0aae6e4ff634024387ceaf7dbc432a0351 Signed-off-by: Dave Barach <dave@barachs.net> Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/vnet/flow/flow_report.h')
-rw-r--r--src/vnet/flow/flow_report.h2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/vnet/flow/flow_report.h b/src/vnet/flow/flow_report.h
index 4e764377dc8..e8ed3818534 100644
--- a/src/vnet/flow/flow_report.h
+++ b/src/vnet/flow/flow_report.h
@@ -23,7 +23,7 @@
#include <vnet/ip/ip_packet.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
-#include <vnet/ip/udp.h>
+#include <vnet/udp/udp.h>
#include <vlib/cli.h>
#include <vppinfra/error.h>
#include <vppinfra/hash.h>
='n178' href='#n178'>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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/*
 * Copyright (c) 2017-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_static.h>
#include <vppinfra/bihash_template.c>
#include <vppinfra/unix.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/** @file static_server.c
 *  Static http server, sufficient to serve .html / .css / .js content.
 */
/*? %%clicmd:group_label Static HTTP Server %% ?*/

#define HSS_FIFO_THRESH (16 << 10)

hss_main_t hss_main;

static void
hss_cache_lock (void)
{
  clib_spinlock_lock (&hss_main.cache_lock);
}

static void
hss_cache_unlock (void)
{
  clib_spinlock_unlock (&hss_main.cache_lock);
}

static hss_session_t *
hss_session_alloc (u32 thread_index)
{
  hss_main_t *hsm = &hss_main;
  hss_session_t *hs;

  pool_get_zero (hsm->sessions[thread_index], hs);
  hs->session_index = hs - hsm->sessions[thread_index];
  hs->thread_index = thread_index;
  hs->cache_pool_index = ~0;
  return hs;
}

static hss_session_t *
hss_session_get (u32 thread_index, u32 hs_index)
{
  hss_main_t *hsm = &hss_main;
  if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
    return 0;
  return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
}

static void
hss_session_free (hss_session_t *hs)
{
  hss_main_t *hsm = &hss_main;

  pool_put (hsm->sessions[hs->thread_index], hs);

  if (CLIB_DEBUG)
    {
      u32 save_thread_index;
      save_thread_index = hs->thread_index;
      /* Poison the entry, preserve timer state and thread index */
      memset (hs, 0xfa, sizeof (*hs));
      hs->thread_index = save_thread_index;
    }
}

/** \brief Detach cache entry from session
 */
static void
hss_detach_cache_entry (hss_session_t *hs)
{
  hss_main_t *hsm = &hss_main;
  hss_cache_entry_t *ep;

  /*
   * Decrement cache pool entry reference count
   * Note that if e.g. a file lookup fails, the cache pool index
   * won't be set
   */
  if (hs->cache_pool_index != ~0)
    {
      ep = pool_elt_at_index (hsm->cache_pool, hs->cache_pool_index);
      ep->inuse--;
      if (hsm->debug_level > 1)
	clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
		      ep->inuse);
    }
  hs->cache_pool_index = ~0;
  if (hs->free_data)
    vec_free (hs->data);
  hs->data = 0;
  hs->data_offset = 0;
  hs->free_data = 0;
  vec_free (hs->path);
}

/** \brief Disconnect a session
 */
static void
hss_session_disconnect_transport (hss_session_t *hs)
{
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
  a->handle = hs->vpp_session_handle;
  a->app_index = hss_main.app_index;
  vnet_disconnect_session (a);
}

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

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

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

/** \brief Remove a data cache entry from the LRU lists
 */
static inline void
lru_remove (hss_main_t *hsm, hss_cache_entry_t *ep)
{
  hss_cache_entry_t *next_ep, *prev_ep;
  u32 ep_index;

  lru_validate (hsm);

  ep_index = ep - hsm->cache_pool;

  /* Deal with list heads */
  if (ep_index == hsm->first_index)
    hsm->first_index = ep->next_index;
  if (ep_index == hsm->last_index)
    hsm->last_index = ep->prev_index;

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

/** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
 */
static inline void
lru_add (hss_main_t *hsm, hss_cache_entry_t *ep, f64 now)
{
  hss_cache_entry_t *next_ep;
  u32 ep_index;

  lru_validate (hsm);

  ep_index = ep - hsm->cache_pool;

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

  ep->prev_index = ~0;

  /* ep now the new head of the LRU forward list */
  ep->next_index = hsm->first_index;
  hsm->first_index = ep_index;

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

  lru_validate (hsm);
}

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

static void
start_send_data (hss_session_t *hs, http_status_code_t status)
{
  http_msg_t msg;
  session_t *ts;
  int rv;

  ts = session_get (hs->vpp_session_index, hs->thread_index);

  msg.type = HTTP_MSG_REPLY;
  msg.code = status;
  msg.content_type = HTTP_CONTENT_TEXT_HTML;
  msg.data.len = vec_len (hs->data);

  if (vec_len (hs->data) > hss_main.use_ptr_thresh)
    {
      msg.data.type = HTTP_MSG_DATA_PTR;
      rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
      ASSERT (rv == sizeof (msg));

      uword data = pointer_to_uword (hs->data);
      rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
      ASSERT (rv == sizeof (sizeof (data)));

      goto done;
    }

  msg.data.type = HTTP_MSG_DATA_INLINE;

  rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
  ASSERT (rv == sizeof (msg));

  if (!msg.data.len)
    goto done;

  rv = svm_fifo_enqueue (ts->tx_fifo, vec_len (hs->data), hs->data);

  if (rv != vec_len (hs->data))
    {
      hs->data_offset = rv;
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
    }

done:

  if (svm_fifo_set_event (ts->tx_fifo))
    session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
}

static int
try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
		 u8 *request, http_status_code_t *sc)
{
  uword *p, *url_table;
  hss_url_handler_t fp;
  int rv;

  if (!hsm->enable_url_handlers)
    return -1;

  /* Look for built-in GET / POST handlers */
  url_table =
    (rt == HTTP_REQ_GET) ? hsm->get_url_handlers : hsm->post_url_handlers;

  p = hash_get_mem (url_table, request);
  if (!p)
    return -1;

  if (hsm->debug_level > 0)
    clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", request);

  fp = (hss_url_handler_t) p[0];
  hs->path = 0;
  rv = fp (rt, request, hs);
  if (rv)
    {
      clib_warning ("builtin handler %llx hit on %s '%s' but failed!", p[0],
		    (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
      *sc = HTTP_STATUS_NOT_FOUND;
    }

  return 0;
}

static int
find_data (hss_session_t *hs, http_req_method_t rt, u8 *request)
{
  http_status_code_t sc = HTTP_STATUS_OK;
  hss_main_t *hsm = &hss_main;
  struct stat _sb, *sb = &_sb;
  clib_error_t *error;
  u8 *path;

  if (!try_url_handler (hsm, hs, rt, request, &sc))
    goto done;

  /*
   * Construct the file to open
   * Browsers are capable of sporadically including a leading '/'
   */
  if (request[0] == '/')
    path = format (0, "%s%s%c", hsm->www_root, request, 0);
  else
    path = format (0, "%s/%s%c", hsm->www_root, request, 0);

  if (hsm->debug_level > 0)
    clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", path);

  /* Try to find the file. 2x special cases to find index.html */
  if (stat ((char *) path, sb) < 0	/* cant even stat the file */
      || sb->st_size < 20	/* file too small */
      || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
    {
      u32 save_length = vec_len (path) - 1;
      /* Try appending "index.html"... */
      _vec_len (path) -= 1;
      path = format (path, "index.html%c", 0);
      if (stat ((char *) path, sb) < 0	/* cant even stat the file */
	  || sb->st_size < 20	/* file too small */
	  || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
	{
	  _vec_len (path) = save_length;
	  path = format (path, "/index.html%c", 0);

	  /* Send a redirect, otherwise the browser will confuse itself */
	  if (stat ((char *) path, sb) < 0	/* cant even stat the file */
	      || sb->st_size < 20	/* file too small */
	      || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
	    {
	      sc = HTTP_STATUS_NOT_FOUND;
	      goto done;
	    }
	  else
	    {
	      transport_endpoint_t endpoint;
	      transport_proto_t proto;
	      u16 local_port;
	      int print_port = 0;
	      u8 *port_str = 0;
	      session_t *ts;

	      /*
	       * To make this bit work correctly, we need to know our local
	       * IP address, etc. and send it in the redirect...
	       */
	      u8 *redirect;

	      vec_delete (path, vec_len (hsm->www_root) - 1, 0);

	      ts = session_get (hs->vpp_session_index, hs->thread_index);
	      session_get_endpoint (ts, &endpoint, 1 /* is_local */);

	      local_port = clib_net_to_host_u16 (endpoint.port);

	      proto = session_type_transport_proto (ts->session_type);

	      if ((proto == TRANSPORT_PROTO_TCP && local_port != 80)
		  || (proto == TRANSPORT_PROTO_TLS && local_port != 443))
		{
		  print_port = 1;
		  port_str = format (0, ":%u", (u32) local_port);
		}

	      redirect = format (0, "HTTP/1.1 301 Moved Permanently\r\n"
				 "Location: http%s://%U%s%s\r\n\r\n",
				 proto == TRANSPORT_PROTO_TLS ? "s" : "",
				 format_ip46_address, &endpoint.ip,
				 endpoint.is_ip4,
				 print_port ? port_str : (u8 *) "", path);
	      if (hsm->debug_level > 0)
		clib_warning ("redirect: %s", redirect);

	      vec_free (port_str);

	      hs->data = redirect;
	      goto done;
	    }
	}
    }

  /* find or read the file if we haven't done so yet. */
  if (hs->data == 0)
    {
      BVT (clib_bihash_kv) kv;
      hss_cache_entry_t *ce;

      hs->path = path;

      /* First, try the cache */
      kv.key = (u64) hs->path;
      if (BV (clib_bihash_search) (&hsm->name_to_data, &kv, &kv) == 0)
	{
	  if (hsm->debug_level > 1)
	    clib_warning ("lookup '%s' returned %lld", kv.key, kv.value);

	  hss_cache_lock ();

	  /* found the data.. */
	  ce = pool_elt_at_index (hsm->cache_pool, kv.value);
	  hs->data = ce->data;
	  /* Update the cache entry, mark it in-use */
	  lru_update (hsm, ce, vlib_time_now (vlib_get_main ()));
	  hs->cache_pool_index = ce - hsm->cache_pool;
	  ce->inuse++;
	  if (hsm->debug_level > 1)
	    clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
			  ce->inuse);

	  hss_cache_unlock ();
	}
      else
	{
	  hss_cache_lock ();

	  if (hsm->debug_level > 1)
	    clib_warning ("lookup '%s' failed", kv.key, kv.value);
	  /* Need to recycle one (or more cache) entries? */
	  if (hsm->cache_size > hsm->cache_limit)
	    {
	      int free_index = hsm->last_index;

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

		  lru_remove (hsm, ce);
		  hsm->cache_size -= vec_len (ce->data);
		  hsm->cache_evictions++;
		  vec_free (ce->filename);
		  vec_free (ce->data);
		  if (hsm->debug_level > 1)
		    clib_warning ("pool put index %d", ce - hsm->cache_pool);
		  pool_put (hsm->cache_pool, ce);
		  if (hsm->cache_size < hsm->cache_limit)
		    break;
		}
	    }

	  /* Read the file */
	  error = clib_file_contents ((char *) (hs->path), &hs->data);
	  if (error)
	    {
	      clib_warning ("Error reading '%s'", hs->path);
	      clib_error_report (error);
	      sc = HTTP_STATUS_INTERNAL_ERROR;
	      hss_cache_unlock ();
	      goto done;
	    }
	  /* Create a cache entry for it */
	  pool_get_zero (hsm->cache_pool, ce);
	  ce->filename = vec_dup (hs->path);
	  ce->data = hs->data;
	  hs->cache_pool_index = ce - hsm->cache_pool;
	  ce->inuse++;
	  if (hsm->debug_level > 1)
	    clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
			  ce->inuse);
	  lru_add (hsm, ce, vlib_time_now (vlib_get_main ()));
	  kv.key = (u64) vec_dup (hs->path);
	  kv.value = ce - hsm->cache_pool;
	  /* Add to the lookup table */
	  if (hsm->debug_level > 1)
	    clib_warning ("add '%s' value %lld", kv.key, kv.value);

	  if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
					1 /* is_add */ ) < 0)
	    {
	      clib_warning ("BUG: add failed!");
	    }
	  hsm->cache_size += vec_len (ce->data);

	  hss_cache_unlock ();
	}
      hs->data_offset = 0;
    }

done:

  return sc;
}

static int
hss_ts_rx_callback (session_t *ts)
{
  hss_session_t *hs;
  u8 *request = 0;
  http_msg_t msg;
  int rv;
  http_status_code_t sc;

  hs = hss_session_get (ts->thread_index, ts->opaque);

  /* Read the http message header */
  rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
  ASSERT (rv == sizeof (msg));

  if (msg.type != HTTP_MSG_REQUEST ||
      (msg.method_type != HTTP_REQ_GET && msg.method_type != HTTP_REQ_POST))
    {
      hs->data = 0;
      start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
      return 0;
    }

  /* Read request */
  vec_validate (request, msg.data.len - 1);
  rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
  ASSERT (rv == msg.data.len);

  /* Find and send data */
  sc = find_data (hs, msg.method_type, request);
  start_send_data (hs, sc);

  vec_free (request);
  if (!hs->data)
    hss_session_disconnect_transport (hs);

  return 0;
}

static int
hss_ts_tx_callback (session_t *ts)
{
  hss_session_t *hs;
  u32 to_send;
  int rv;

  hs = hss_session_get (ts->thread_index, ts->opaque);
  if (!hs || !hs->data)
    return 0;

  to_send = vec_len (hs->data) - hs->data_offset;
  rv = svm_fifo_enqueue (ts->tx_fifo, to_send, hs->data + hs->data_offset);

  if (rv <= 0)
    {
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
      return 0;
    }

  if (rv < to_send)
    {
      hs->data_offset += rv;
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
    }

  if (svm_fifo_set_event (ts->tx_fifo))
    session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);

  return 0;
}

/** \brief Session accept callback
 */
static int
hss_ts_accept_callback (session_t *ts)
{
  hss_session_t *hs;
  u32 thresh;

  hs = hss_session_alloc (ts->thread_index);

  hs->vpp_session_index = ts->session_index;
  hs->vpp_session_handle = session_handle (ts);

  /* The application sets a threshold for it's fifo to get notified when
   * additional data can be enqueued. We want to keep the TX fifo reasonably
   * full, however avoid entering a state where the
   * fifo is full all the time and small chunks of data are being enqueued
   * each time. If the fifo is small (under 16K) we set
   * the threshold to it's size, meaning a notification will be given when the
   * fifo empties.
   */
  thresh = clib_min (svm_fifo_size (ts->tx_fifo), HSS_FIFO_THRESH);
  svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);

  ts->opaque = hs->session_index;
  ts->session_state = SESSION_STATE_READY;
  return 0;
}

static void
hss_ts_disconnect_callback (session_t *ts)
{
  hss_main_t *hsm = &hss_main;
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;

  a->handle = session_handle (ts);
  a->app_index = hsm->app_index;
  vnet_disconnect_session (a);
}

static void
hss_ts_reset_callback (session_t *ts)
{
  hss_main_t *hsm = &hss_main;
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;

  a->handle = session_handle (ts);
  a->app_index = hsm->app_index;
  vnet_disconnect_session (a);
}

static int
hss_ts_connected_callback (u32 app_index, u32 api_context, session_t *ts,
			   session_error_t err)
{
  clib_warning ("called...");
  return -1;
}

static int
hss_add_segment_callback (u32 client_index, u64 segment_handle)
{
  return 0;
}

static void
hss_ts_cleanup (session_t *s, session_cleanup_ntf_t ntf)
{
  hss_session_t *hs;

  if (ntf == SESSION_CLEANUP_TRANSPORT)
    return;

  hs = hss_session_get (s->thread_index, s->opaque);
  if (!hs)
    return;

  hss_detach_cache_entry (hs);
  hss_session_free (hs);
}

static session_cb_vft_t hss_cb_vft = {
  .session_accept_callback = hss_ts_accept_callback,
  .session_disconnect_callback = hss_ts_disconnect_callback,
  .session_connected_callback = hss_ts_connected_callback,
  .add_segment_callback = hss_add_segment_callback,
  .builtin_app_rx_callback = hss_ts_rx_callback,
  .builtin_app_tx_callback = hss_ts_tx_callback,
  .session_reset_callback = hss_ts_reset_callback,
  .session_cleanup_callback = hss_ts_cleanup,
};

static int
hss_attach ()
{
  vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
  hss_main_t *hsm = &hss_main;
  u64 options[APP_OPTIONS_N_OPTIONS];
  vnet_app_attach_args_t _a, *a = &_a;
  u32 segment_size = 128 << 20;

  clib_memset (a, 0, sizeof (*a));
  clib_memset (options, 0, sizeof (options));

  if (hsm->private_segment_size)
    segment_size = hsm->private_segment_size;

  a->api_client_index = ~0;
  a->name = format (0, "http_static_server");
  a->session_cb_vft = &hss_cb_vft;
  a->options = options;
  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
  a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
  a->options[APP_OPTIONS_RX_FIFO_SIZE] =
    hsm->fifo_size ? hsm->fifo_size : 8 << 10;
  a->options[APP_OPTIONS_TX_FIFO_SIZE] =
    hsm->fifo_size ? hsm->fifo_size : 32 << 10;
  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
  a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
  a->options[APP_OPTIONS_TLS_ENGINE] = CRYPTO_ENGINE_OPENSSL;

  if (vnet_application_attach (a))
    {
      vec_free (a->name);
      clib_warning ("failed to attach server");
      return -1;
    }
  vec_free (a->name);
  hsm->app_index = a->app_index;

  clib_memset (ck_pair, 0, sizeof (*ck_pair));
  ck_pair->cert = (u8 *) test_srv_crt_rsa;
  ck_pair->key = (u8 *) test_srv_key_rsa;
  ck_pair->cert_len = test_srv_crt_rsa_len;
  ck_pair->key_len = test_srv_key_rsa_len;
  vnet_app_add_cert_key_pair (ck_pair);
  hsm->ckpair_index = ck_pair->index;

  return 0;
}

static int
hss_transport_needs_crypto (transport_proto_t proto)
{
  return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
	 proto == TRANSPORT_PROTO_QUIC;
}

static int
hss_listen (void)
{
  hss_main_t *hsm = &hss_main;
  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
  vnet_listen_args_t _a, *a = &_a;
  char *uri = "tcp://0.0.0.0/80";
  u8 need_crypto;
  int rv;

  clib_memset (a, 0, sizeof (*a));
  a->app_index = hsm->app_index;

  if (hsm->uri)
    uri = (char *) hsm->uri;

  if (parse_uri (uri, &sep))
    return -1;

  need_crypto = hss_transport_needs_crypto (a->sep_ext.transport_proto);

  sep.transport_proto = TRANSPORT_PROTO_HTTP;
  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));

  if (need_crypto)
    {
      session_endpoint_alloc_ext_cfg (&a->sep_ext,
				      TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
      a->sep_ext.ext_cfg->crypto.ckpair_index = hsm->ckpair_index;
    }

  rv = vnet_listen (a);

  if (need_crypto)
    clib_mem_free (a->sep_ext.ext_cfg);

  return rv;
}

static void
hss_url_handlers_init (hss_main_t *hsm)
{
  if (!hsm->get_url_handlers)
    {
      hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
      hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
    }

  hss_builtinurl_json_handlers_init ();
}

int
hss_create (vlib_main_t *vm)
{
  vlib_thread_main_t *vtm = vlib_get_thread_main ();
  hss_main_t *hsm = &hss_main;
  u32 num_threads;

  num_threads = 1 /* main thread */  + vtm->n_threads;
  vec_validate (hsm->sessions, num_threads - 1);

  clib_spinlock_init (&hsm->cache_lock);

  if (hss_attach ())
    {
      clib_warning ("failed to attach server");
      return -1;
    }
  if (hss_listen ())
    {
      clib_warning ("failed to start listening");
      return -1;
    }

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

  if (hsm->enable_url_handlers)
    hss_url_handlers_init (hsm);

  return 0;
}

static clib_error_t *
hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
		       vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  hss_main_t *hsm = &hss_main;
  clib_error_t *error = 0;
  u64 seg_size;
  int rv;

  if (hsm->app_index != (u32) ~0)
    return clib_error_return (0, "http server already running...");

  hsm->prealloc_fifos = 0;
  hsm->private_segment_size = 0;
  hsm->fifo_size = 0;
  /* 10mb cache limit, before LRU occurs */
  hsm->cache_limit = 10 << 20;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    goto no_input;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "www-root %s", &hsm->www_root))
	;
      else
	if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
	;
      else if (unformat (line_input, "private-segment-size %U",
			 unformat_memory_size, &seg_size))
	hsm->private_segment_size = seg_size;
      else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
	hsm->fifo_size <<= 10;
      else if (unformat (line_input, "cache-size %U", unformat_memory_size,
			 &hsm->cache_limit))
	;
      else if (unformat (line_input, "uri %s", &hsm->uri))
	;
      else if (unformat (line_input, "debug %d", &hsm->debug_level))
	;
      else if (unformat (line_input, "debug"))
	hsm->debug_level = 1;
      else if (unformat (line_input, "ptr-thresh %U", unformat_memory_size,
			 &hsm->use_ptr_thresh))
	;
      else if (unformat (line_input, "url-handlers"))
	hsm->enable_url_handlers = 1;
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, line_input);
	}
    }

  unformat_free (line_input);

no_input:

  if (error)
    goto done;

  if (hsm->www_root == 0 && !hsm->enable_url_handlers)
    {
      error = clib_error_return (0, "Must set www-root or url-handlers");
      goto done;
    }

  if (hsm->cache_limit < (128 << 10))
    {
      error = clib_error_return (0, "cache-size must be at least 128kb");
      vec_free (hsm->www_root);
      goto done;
    }

  vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );

  if ((rv = hss_create (vm)))
    {
      error = clib_error_return (0, "server_create returned %d", rv);
      vec_free (hsm->www_root);
    }

done:

  return error;
}

/*?
 * Enable the static http server
 *
 * @cliexpar
 * This command enables the static http server. Only the www-root
 * parameter is required
 * @clistart
 * http static server www-root /tmp/www uri tcp://0.0.0.0/80 cache-size 2m
 * @cliend
 * @cliexcmd{http static server www-root <path> [prealloc-fios <nn>]
 *   [private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]}
?*/
VLIB_CLI_COMMAND (hss_create_command, static) = {
  .path = "http static server",
  .short_help =
    "http static server www-root <path> [prealloc-fifos <nn>]\n"
    "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
    "[ptr-thresh <nn>] [url-handlers] [debug [nn]]\n",
  .function = hss_create_command_fn,
};

/** \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;
}

static u8 *
format_hss_session (u8 *s, va_list *args)
{
  hss_session_t *hs = va_arg (*args, hss_session_t *);
  int __clib_unused verbose = va_arg (*args, int);

  s = format (s, "\n path %s, data length %u, data_offset %u",
	      hs->path ? hs->path : (u8 *) "[none]", vec_len (hs->data),
	      hs->data_offset);
  return s;
}

static clib_error_t *
hss_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
		     vlib_cli_command_t *cmd)
{
  hss_main_t *hsm = &hss_main;
  hss_cache_entry_t *ep, **entries = 0;
  int verbose = 0;
  int show_cache = 0;
  int show_sessions = 0;
  u32 index;
  f64 now;

  if (hsm->www_root == 0)
    return clib_error_return (0, "Static server disabled");

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "verbose %d", &verbose))
	;
      else if (unformat (input, "verbose"))
	verbose = 1;
      else if (unformat (input, "cache"))
	show_cache = 1;
      else if (unformat (input, "sessions"))
	show_sessions = 1;
      else
	break;
    }

  if ((show_cache + show_sessions) == 0)
    return clib_error_return (0, "specify one or more of cache, sessions");

  if (show_cache)
    {
      if (verbose == 0)
	{
	  vlib_cli_output
	    (vm, "www_root %s, cache size %lld bytes, limit %lld bytes, "
	     "evictions %lld",
	     hsm->www_root, hsm->cache_size, hsm->cache_limit,
	     hsm->cache_evictions);
	  return 0;
	}

      now = vlib_time_now (vm);

      vlib_cli_output (vm, "%U", format_hss_cache_entry, 0 /* header */, now);

      for (index = hsm->first_index; index != ~0;)
	{
	  ep = pool_elt_at_index (hsm->cache_pool, index);
	  index = ep->next_index;
	  vlib_cli_output (vm, "%U", format_hss_cache_entry, ep, now);
	}

      vlib_cli_output (vm, "%40s%12lld", "Total Size", hsm->cache_size);

      vec_free (entries);
    }

  if (show_sessions)
    {
      u32 *session_indices = 0;
      hss_session_t *hs;
      int i, j;

      hss_cache_lock ();

      for (i = 0; i < vec_len (hsm->sessions); i++)
	{
	  pool_foreach (hs, hsm->sessions[i])
           {
            vec_add1 (session_indices, hs - hsm->sessions[i]);
          }

	  for (j = 0; j < vec_len (session_indices); j++)
	    {
	      vlib_cli_output (
		vm, "%U", format_hss_session,
		pool_elt_at_index (hsm->sessions[i], session_indices[j]),
		verbose);
	    }
	  vec_reset_length (session_indices);
	}
      hss_cache_unlock ();
      vec_free (session_indices);
    }
  return 0;
}

/*?
 * Display static http server cache statistics
 *
 * @cliexpar
 * This command shows the contents of the static http server cache
 * @clistart
 * show http static server
 * @cliend
 * @cliexcmd{show http static server sessions cache [verbose [nn]]}
?*/
VLIB_CLI_COMMAND (hss_show_command, static) = {
  .path = "show http static server",
  .short_help = "show http static server sessions cache [verbose [<nn>]]",
  .function = hss_show_command_fn,
};

static clib_error_t *
hss_clear_cache_command_fn (vlib_main_t *vm, unformat_input_t *input,
			    vlib_cli_command_t *cmd)
{
  hss_main_t *hsm = &hss_main;
  hss_cache_entry_t *ce;
  u32 free_index;
  u32 busy_items = 0;
  BVT (clib_bihash_kv) kv;

  if (hsm->www_root == 0)
    return clib_error_return (0, "Static server disabled");

  hss_cache_lock ();

  /* Walk the LRU list to find active entries */
  free_index = hsm->last_index;
  while (free_index != ~0)
    {
      ce = pool_elt_at_index (hsm->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) (&hsm->name_to_data, &kv,
				    0 /* is_add */ ) < 0)
	{
	  clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
	}

      lru_remove (hsm, ce);
      hsm->cache_size -= vec_len (ce->data);
      hsm->cache_evictions++;
      vec_free (ce->filename);
      vec_free (ce->data);
      if (hsm->debug_level > 1)
	clib_warning ("pool put index %d", ce - hsm->cache_pool);
      pool_put (hsm->cache_pool, ce);
      free_index = hsm->last_index;
    }
  hss_cache_unlock ();
  if (busy_items > 0)
    vlib_cli_output (vm, "Note: %d busy items still in cache...", busy_items);
  else
    vlib_cli_output (vm, "Cache cleared...");
  return 0;
}

/*?
 * Clear the static http server cache, to force the server to
 * reload content from backing files
 *
 * @cliexpar
 * This command clear the static http server cache
 * @clistart
 * clear http static cache
 * @cliend
 * @cliexcmd{clear http static cache}
?*/
VLIB_CLI_COMMAND (clear_hss_cache_command, static) = {
  .path = "clear http static cache",
  .short_help = "clear http static cache",
  .function = hss_clear_cache_command_fn,
};

static clib_error_t *
hss_main_init (vlib_main_t *vm)
{
  hss_main_t *hsm = &hss_main;

  hsm->app_index = ~0;
  hsm->vlib_main = vm;
  hsm->first_index = hsm->last_index = ~0;

  return 0;
}

VLIB_INIT_FUNCTION (hss_main_init);

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