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:
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/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/session.h>
19 #include <http/http.h>
23 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
28 u32 vpp_session_index;
31 /** threshold after which connection is closed */
33 /** rate at which accepted sessions are marked for random close */
39 typedef struct hts_listen_cfg_
47 typedef struct hs_main_
49 hts_session_t **sessions;
55 /** Hash table of listener uris to handles */
70 static hts_main_t hts_main;
72 static hts_session_t *
73 hts_session_alloc (u32 thread_index)
75 hts_main_t *htm = &hts_main;
78 pool_get_zero (htm->sessions[thread_index], hs);
79 hs->session_index = hs - htm->sessions[thread_index];
80 hs->thread_index = thread_index;
85 static hts_session_t *
86 hts_session_get (u32 thread_index, u32 hts_index)
88 hts_main_t *htm = &hts_main;
90 if (pool_is_free_index (htm->sessions[thread_index], hts_index))
93 return pool_elt_at_index (htm->sessions[thread_index], hts_index);
97 hts_session_free (hts_session_t *hs)
99 hts_main_t *htm = &hts_main;
100 u32 thread = hs->thread_index;
102 if (htm->debug_level > 0)
103 clib_warning ("Freeing session %u", hs->session_index);
106 clib_memset (hs, 0xfa, sizeof (*hs));
108 pool_put (htm->sessions[thread], hs);
112 hts_disconnect_transport (hts_session_t *hs)
114 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
115 hts_main_t *htm = &hts_main;
118 if (htm->debug_level > 0)
119 clib_warning ("Actively closing session %u", hs->session_index);
121 ts = session_get (hs->vpp_session_index, hs->thread_index);
122 a->handle = session_handle (ts);
123 a->app_index = htm->app_index;
124 vnet_disconnect_session (a);
128 hts_session_tx_zc (hts_session_t *hs, session_t *ts)
134 rv = svm_fifo_fill_chunk_list (ts->tx_fifo);
137 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
141 max_send = hs->data_len - hs->data_offset;
142 space = svm_fifo_max_enqueue (ts->tx_fifo);
144 to_send = clib_min (space, max_send);
146 svm_fifo_enqueue_nocopy (ts->tx_fifo, to_send);
148 hs->data_offset += to_send;
150 if (to_send < max_send)
151 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
153 if (svm_fifo_set_event (ts->tx_fifo))
154 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
158 hts_session_tx_no_zc (hts_session_t *hs, session_t *ts)
160 u32 n_segs, buf_offset, buf_left;
161 u64 max_send = 32 << 10, left;
162 hts_main_t *htm = &hts_main;
163 svm_fifo_seg_t seg[2];
166 left = hs->data_len - hs->data_offset;
167 max_send = clib_min (left, max_send);
168 buf_offset = hs->data_offset % vec_len (htm->test_data);
169 buf_left = vec_len (htm->test_data) - buf_offset;
171 if (buf_left < max_send)
173 seg[0].data = htm->test_data + buf_offset;
174 seg[0].len = buf_left;
175 seg[1].data = htm->test_data;
176 seg[1].len = max_send - buf_left;
181 seg[0].data = htm->test_data + buf_offset;
182 seg[0].len = max_send;
186 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
187 1 /* allow partial */);
191 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
195 hs->data_offset += sent;
198 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
200 if (svm_fifo_set_event (ts->tx_fifo))
201 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
205 hts_session_tx (hts_session_t *hs, session_t *ts)
207 hts_main_t *htm = &hts_main;
210 hts_session_tx_zc (hs, ts);
212 hts_session_tx_no_zc (hs, ts);
214 if (hs->close_threshold > 0)
216 if ((f64) hs->data_offset / hs->data_len > hs->close_threshold)
217 hts_disconnect_transport (hs);
222 hts_start_send_data (hts_session_t *hs, http_status_code_t status)
228 msg.type = HTTP_MSG_REPLY;
230 msg.content_type = HTTP_CONTENT_APP_OCTET_STREAM;
231 msg.data.type = HTTP_MSG_DATA_INLINE;
232 msg.data.len = hs->data_len;
234 ts = session_get (hs->vpp_session_index, hs->thread_index);
235 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
236 ASSERT (rv == sizeof (msg));
240 if (svm_fifo_set_event (ts->tx_fifo))
241 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
245 hts_session_tx (hs, ts);
249 try_test_file (hts_session_t *hs, u8 *request)
251 char *test_str = "test_file";
252 hts_main_t *htm = &hts_main;
253 unformat_input_t input;
257 if (memcmp (request, test_str, clib_strnlen (test_str, 9)))
260 unformat_init_vector (&input, vec_dup (request));
261 if (!unformat (&input, "test_file_%U", unformat_memory_size, &file_size))
267 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
273 if (htm->debug_level)
274 clib_warning ("Requested file size %U", format_memory_size, file_size);
276 hs->data_len = file_size;
279 if (hs->close_threshold > 0)
281 /* Disconnect if the header is already enough to fill the quota */
282 if ((f64) 30 / hs->data_len > hs->close_threshold)
284 hts_disconnect_transport (hs);
289 hts_start_send_data (hs, HTTP_STATUS_OK);
292 unformat_free (&input);
298 hts_ts_rx_callback (session_t *ts)
305 hs = hts_session_get (ts->thread_index, ts->opaque);
307 /* Read the http message header */
308 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
309 ASSERT (rv == sizeof (msg));
311 if (msg.type != HTTP_MSG_REQUEST || msg.method_type != HTTP_REQ_GET)
313 hts_start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
319 hts_start_send_data (hs, HTTP_STATUS_BAD_REQUEST);
323 vec_validate (request, msg.data.len - 1);
324 rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
326 if (try_test_file (hs, request))
327 hts_start_send_data (hs, HTTP_STATUS_NOT_FOUND);
335 hs_ts_tx_callback (session_t *ts)
339 hs = hts_session_get (ts->thread_index, ts->opaque);
343 hts_session_tx (hs, ts);
349 hts_ts_accept_callback (session_t *ts)
351 hts_main_t *htm = &hts_main;
352 hts_session_t *hs, *lhs;
355 hs = hts_session_alloc (ts->thread_index);
356 hs->vpp_session_index = ts->session_index;
358 ts->opaque = hs->session_index;
359 ts->session_state = SESSION_STATE_READY;
361 /* Check if listener configured for random closes */
362 ls = listen_session_get_from_handle (ts->listener_handle);
363 lhs = hts_session_get (0, ls->opaque);
367 /* overload listener's data_offset as session counter */
368 u32 cnt = __atomic_add_fetch (&lhs->data_offset, 1, __ATOMIC_RELEASE);
369 if ((cnt % lhs->close_rate) == 0)
370 hs->close_threshold = random_f64 (&htm->seed);
373 if (htm->debug_level > 0)
374 clib_warning ("Accepted session %u close threshold %.2f", ts->opaque,
375 hs->close_threshold);
381 hts_ts_connected_callback (u32 app_index, u32 api_context, session_t *s,
384 clib_warning ("called...");
389 hts_ts_disconnect_callback (session_t *ts)
391 hts_main_t *htm = &hts_main;
392 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
394 if (htm->debug_level > 0)
395 clib_warning ("Transport closing session %u", ts->opaque);
397 a->handle = session_handle (ts);
398 a->app_index = htm->app_index;
399 vnet_disconnect_session (a);
403 hts_ts_reset_callback (session_t *ts)
405 hts_main_t *htm = &hts_main;
406 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
408 if (htm->debug_level > 0)
409 clib_warning ("Transport reset session %u", ts->opaque);
411 a->handle = session_handle (ts);
412 a->app_index = htm->app_index;
413 vnet_disconnect_session (a);
417 hts_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
421 if (ntf == SESSION_CLEANUP_TRANSPORT)
424 hs = hts_session_get (s->thread_index, s->opaque);
428 hts_session_free (hs);
432 hts_add_segment_callback (u32 client_index, u64 segment_handle)
438 hts_del_segment_callback (u32 client_index, u64 segment_handle)
443 static session_cb_vft_t hs_session_cb_vft = {
444 .session_accept_callback = hts_ts_accept_callback,
445 .session_disconnect_callback = hts_ts_disconnect_callback,
446 .session_connected_callback = hts_ts_connected_callback,
447 .add_segment_callback = hts_add_segment_callback,
448 .del_segment_callback = hts_del_segment_callback,
449 .builtin_app_rx_callback = hts_ts_rx_callback,
450 .builtin_app_tx_callback = hs_ts_tx_callback,
451 .session_reset_callback = hts_ts_reset_callback,
452 .session_cleanup_callback = hts_ts_cleanup_callback,
456 hts_attach (hts_main_t *hm)
458 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
459 u64 options[APP_OPTIONS_N_OPTIONS];
460 vnet_app_attach_args_t _a, *a = &_a;
462 clib_memset (a, 0, sizeof (*a));
463 clib_memset (options, 0, sizeof (options));
465 a->api_client_index = ~0;
466 a->name = format (0, "http_tps");
467 a->session_cb_vft = &hs_session_cb_vft;
468 a->options = options;
469 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->segment_size;
470 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->segment_size;
471 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
472 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
473 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
475 if (vnet_application_attach (a))
478 clib_warning ("failed to attach server");
482 hm->app_index = a->app_index;
484 clib_memset (ck_pair, 0, sizeof (*ck_pair));
485 ck_pair->cert = (u8 *) test_srv_crt_rsa;
486 ck_pair->key = (u8 *) test_srv_key_rsa;
487 ck_pair->cert_len = test_srv_crt_rsa_len;
488 ck_pair->key_len = test_srv_key_rsa_len;
489 vnet_app_add_cert_key_pair (ck_pair);
490 hm->ckpair_index = ck_pair->index;
496 hts_transport_needs_crypto (transport_proto_t proto)
498 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
499 proto == TRANSPORT_PROTO_QUIC;
503 hts_start_listen (hts_main_t *htm, session_endpoint_cfg_t *sep, u8 *uri,
506 vnet_listen_args_t _a, *a = &_a;
510 u32 thread_index = 0;
513 clib_memset (a, 0, sizeof (*a));
514 a->app_index = htm->app_index;
516 need_crypto = hts_transport_needs_crypto (sep->transport_proto);
518 sep->transport_proto = TRANSPORT_PROTO_HTTP;
519 clib_memcpy (&a->sep_ext, sep, sizeof (*sep));
523 session_endpoint_alloc_ext_cfg (&a->sep_ext,
524 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
525 a->sep_ext.ext_cfg->crypto.ckpair_index = htm->ckpair_index;
528 rv = vnet_listen (a);
531 clib_mem_free (a->sep_ext.ext_cfg);
536 hls = hts_session_alloc (thread_index);
537 hls->uri = vec_dup (uri);
538 hls->close_rate = (f64) 1 / rnd_close;
539 ls = listen_session_get_from_handle (a->handle);
540 hls->vpp_session_index = ls->session_index;
541 hash_set_mem (htm->uri_to_handle, hls->uri, hls->session_index);
543 /* opaque holds index of hls, which is used in `hts_ts_accept_callback`
544 * to get back the pointer to hls */
545 ls->opaque = hls - htm->sessions[thread_index];
551 hts_stop_listen (hts_main_t *htm, u32 hls_index)
556 hls = hts_session_get (0, hls_index);
557 ls = listen_session_get (hls->vpp_session_index);
559 vnet_unlisten_args_t ua = {
560 .handle = listen_session_get_handle (ls),
561 .app_index = htm->app_index,
562 .wrk_map_index = 0 /* default wrk */
565 hash_unset_mem (htm->uri_to_handle, hls->uri);
567 if (vnet_unlisten (&ua))
571 hts_session_free (hls);
576 static clib_error_t *
577 hts_listen (hts_main_t *htm, hts_listen_cfg_t *lcfg)
579 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
580 clib_error_t *error = 0;
585 uri = lcfg->uri ? lcfg->uri : htm->default_uri;
586 uri_key = format (0, "vrf%u-%s", lcfg->vrf, uri);
587 p = hash_get_mem (htm->uri_to_handle, uri_key);
592 error = clib_error_return (0, "not listening on %v", uri);
593 else if (hts_stop_listen (htm, p[0]))
594 error = clib_error_return (0, "failed to unlisten");
600 error = clib_error_return (0, "already listening %v", uri);
604 if (parse_uri ((char *) uri, &sep))
606 error = clib_error_return (0, "failed to parse uri %v", uri);
615 fp = sep.is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
616 fib_index = fib_table_find (fp, lcfg->vrf);
619 error = clib_error_return (0, "no such vrf %u", lcfg->vrf);
622 sep.fib_index = fib_index;
625 if ((rv = hts_start_listen (htm, &sep, uri_key, lcfg->rnd_close)))
627 error = clib_error_return (0, "failed to listen on %v: %U", uri,
628 format_session_error, rv);
638 hts_create (vlib_main_t *vm)
640 vlib_thread_main_t *vtm = vlib_get_thread_main ();
641 hts_main_t *htm = &hts_main;
644 num_threads = 1 /* main thread */ + vtm->n_threads;
645 vec_validate (htm->sessions, num_threads - 1);
648 vec_validate (htm->test_data, (64 << 10) - 1);
650 if (hts_attach (htm))
652 clib_warning ("failed to attach server");
656 htm->default_uri = format (0, "tcp://0.0.0.0/80%c", 0);
657 htm->uri_to_handle = hash_create_vec (0, sizeof (u8), sizeof (uword));
662 static clib_error_t *
663 hts_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
664 vlib_cli_command_t *cmd)
666 unformat_input_t _line_input, *line_input = &_line_input;
667 hts_main_t *htm = &hts_main;
668 hts_listen_cfg_t lcfg = {};
669 clib_error_t *error = 0;
672 /* Get a line of input. */
673 if (!unformat_user (input, unformat_line_input, line_input))
676 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
678 if (unformat (line_input, "private-segment-size %U",
679 unformat_memory_size, &mem_size))
680 htm->segment_size = mem_size;
681 else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
683 htm->fifo_size = mem_size;
684 else if (unformat (line_input, "no-zc"))
686 else if (unformat (line_input, "debug"))
687 htm->debug_level = 1;
688 else if (unformat (line_input, "vrf %u", &lcfg.vrf))
690 else if (unformat (line_input, "uri %s", &lcfg.uri))
692 else if (unformat (line_input, "rnd-close %f", &lcfg.rnd_close))
694 if (lcfg.rnd_close > 1.0)
696 error = clib_error_return (0, "invalid rnd close value %f",
701 else if (unformat (line_input, "del"))
705 error = clib_error_return (0, "unknown input `%U'",
706 format_unformat_error, line_input);
711 unformat_free (line_input);
718 if (htm->app_index == (u32) ~0)
720 vnet_session_enable_disable (vm, 1 /* is_enable */);
724 error = clib_error_return (0, "http tps create failed");
729 error = hts_listen (htm, &lcfg);
737 VLIB_CLI_COMMAND (http_tps_command, static) = {
739 .short_help = "http tps [uri <uri>] [fifo-size <nbytes>] "
740 "[segment-size <nMG>] [prealloc-fifos <n>] [debug] [no-zc] "
742 .function = hts_create_command_fn,
745 static clib_error_t *
746 hts_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
747 vlib_cli_command_t *cmd)
749 unformat_input_t _line_input, *line_input = &_line_input;
750 hts_main_t *htm = &hts_main;
751 clib_error_t *error = 0;
753 hts_session_t **sessions;
754 u32 n_listeners = 0, n_sessions = 0;
756 if (!unformat_user (input, unformat_line_input, line_input))
759 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
761 if (unformat (line_input, "listeners"))
765 error = clib_error_return (0, "unknown input `%U'",
766 format_unformat_error, line_input);
776 if (htm->app_index == ~0)
778 vlib_cli_output (vm, "http tps not enabled");
787 /* clang-format off */
788 hash_foreach (uri, handle, htm->uri_to_handle, ({
789 s = format (s, "%-30v%lx\n", uri, handle);
791 /* clang-format on */
795 vlib_cli_output (vm, "%-29s%s", "URI", "Index");
796 vlib_cli_output (vm, "%v", s);
802 n_listeners = hash_elts (htm->uri_to_handle);
803 vec_foreach (sessions, htm->sessions)
804 n_sessions += pool_elts (*sessions);
806 vlib_cli_output (vm, " app index: %u\n listeners: %u\n sesions: %u",
807 htm->app_index, n_listeners, n_sessions - n_listeners);
813 VLIB_CLI_COMMAND (show_http_tps_command, static) = {
814 .path = "show http tps",
815 .short_help = "http tps [listeners]",
816 .function = hts_show_command_fn,
819 static clib_error_t *
820 hs_main_init (vlib_main_t *vm)
822 hts_main_t *htm = &hts_main;
825 htm->segment_size = 128 << 20;
826 htm->fifo_size = 64 << 10;
831 VLIB_INIT_FUNCTION (hs_main_init);
834 * fd.io coding-style-patch-verification: ON
837 * eval: (c-set-style "gnu")