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;
32 typedef struct hs_main_
34 hts_session_t **sessions;
40 /** Hash table of listener uris to handles */
54 static hts_main_t hts_main;
56 static hts_session_t *
57 hts_session_alloc (u32 thread_index)
59 hts_main_t *htm = &hts_main;
62 pool_get_zero (htm->sessions[thread_index], hs);
63 hs->session_index = hs - htm->sessions[thread_index];
64 hs->thread_index = thread_index;
69 static hts_session_t *
70 hts_session_get (u32 thread_index, u32 hts_index)
72 hts_main_t *htm = &hts_main;
74 if (pool_is_free_index (htm->sessions[thread_index], hts_index))
77 return pool_elt_at_index (htm->sessions[thread_index], hts_index);
81 hts_session_free (hts_session_t *hs)
83 hts_main_t *htm = &hts_main;
84 u32 thread = hs->thread_index;
86 if (htm->debug_level > 0)
87 clib_warning ("Freeing session %u", hs->session_index);
90 clib_memset (hs, 0xfa, sizeof (*hs));
92 pool_put (htm->sessions[thread], hs);
96 hts_session_tx_zc (hts_session_t *hs, session_t *ts)
102 rv = svm_fifo_fill_chunk_list (ts->tx_fifo);
105 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
109 max_send = hs->data_len - hs->data_offset;
110 space = svm_fifo_max_enqueue (ts->tx_fifo);
112 to_send = clib_min (space, max_send);
114 svm_fifo_enqueue_nocopy (ts->tx_fifo, to_send);
116 hs->data_offset += to_send;
118 if (to_send < max_send)
119 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
121 if (svm_fifo_set_event (ts->tx_fifo))
122 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
126 hts_session_tx_no_zc (hts_session_t *hs, session_t *ts)
128 u32 n_segs, buf_offset, buf_left;
129 u64 max_send = 32 << 10, left;
130 hts_main_t *htm = &hts_main;
131 svm_fifo_seg_t seg[2];
134 left = hs->data_len - hs->data_offset;
135 max_send = clib_min (left, max_send);
136 buf_offset = hs->data_offset % vec_len (htm->test_data);
137 buf_left = vec_len (htm->test_data) - buf_offset;
139 if (buf_left < max_send)
141 seg[0].data = htm->test_data + buf_offset;
142 seg[0].len = buf_left;
143 seg[1].data = htm->test_data;
144 seg[1].len = max_send - buf_left;
149 seg[0].data = htm->test_data + buf_offset;
150 seg[0].len = max_send;
154 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
155 1 /* allow partial */);
159 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
163 hs->data_offset += sent;
166 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
168 if (svm_fifo_set_event (ts->tx_fifo))
169 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
173 hts_session_tx (hts_session_t *hs, session_t *ts)
175 hts_main_t *htm = &hts_main;
178 hts_session_tx_zc (hs, ts);
180 hts_session_tx_no_zc (hs, ts);
184 hts_start_send_data (hts_session_t *hs, http_status_code_t status)
190 msg.type = HTTP_MSG_REPLY;
192 msg.content_type = HTTP_CONTENT_APP_OCTET_STREAM;
193 msg.data.type = HTTP_MSG_DATA_INLINE;
194 msg.data.len = hs->data_len;
196 ts = session_get (hs->vpp_session_index, hs->thread_index);
197 rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
198 ASSERT (rv == sizeof (msg));
202 if (svm_fifo_set_event (ts->tx_fifo))
203 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
207 hts_session_tx (hs, ts);
211 try_test_file (hts_session_t *hs, u8 *request)
213 char *test_str = "test_file";
214 hts_main_t *htm = &hts_main;
215 unformat_input_t input;
219 if (memcmp (request, test_str, clib_strnlen (test_str, 9)))
222 unformat_init_vector (&input, vec_dup (request));
223 if (!unformat (&input, "test_file_%U", unformat_memory_size, &file_size))
229 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
235 if (htm->debug_level)
236 clib_warning ("Requested file size %U", format_memory_size, file_size);
238 hs->data_len = file_size;
240 hts_start_send_data (hs, HTTP_STATUS_OK);
243 unformat_free (&input);
249 hts_ts_rx_callback (session_t *ts)
256 hs = hts_session_get (ts->thread_index, ts->opaque);
258 /* Read the http message header */
259 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
260 ASSERT (rv == sizeof (msg));
262 if (msg.type != HTTP_MSG_REQUEST || msg.method_type != HTTP_REQ_GET)
264 hts_start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
270 hts_start_send_data (hs, HTTP_STATUS_BAD_REQUEST);
274 vec_validate (request, msg.data.len - 1);
275 rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
277 if (try_test_file (hs, request))
278 hts_start_send_data (hs, HTTP_STATUS_NOT_FOUND);
286 hs_ts_tx_callback (session_t *ts)
290 hs = hts_session_get (ts->thread_index, ts->opaque);
294 hts_session_tx (hs, ts);
300 hts_ts_accept_callback (session_t *ts)
302 hts_main_t *htm = &hts_main;
305 hs = hts_session_alloc (ts->thread_index);
306 hs->vpp_session_index = ts->session_index;
308 ts->opaque = hs->session_index;
309 ts->session_state = SESSION_STATE_READY;
311 if (htm->debug_level > 0)
312 clib_warning ("Accepted session %u", ts->opaque);
318 hts_ts_connected_callback (u32 app_index, u32 api_context, session_t *s,
321 clib_warning ("called...");
326 hts_ts_disconnect_callback (session_t *ts)
328 hts_main_t *htm = &hts_main;
329 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
331 if (htm->debug_level > 0)
332 clib_warning ("Closed session %u", ts->opaque);
334 a->handle = session_handle (ts);
335 a->app_index = htm->app_index;
336 vnet_disconnect_session (a);
340 hts_ts_reset_callback (session_t *ts)
342 hts_main_t *htm = &hts_main;
343 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
345 if (htm->debug_level > 0)
346 clib_warning ("Reset session %u", ts->opaque);
348 a->handle = session_handle (ts);
349 a->app_index = htm->app_index;
350 vnet_disconnect_session (a);
354 hts_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
358 if (ntf == SESSION_CLEANUP_TRANSPORT)
361 hs = hts_session_get (s->thread_index, s->opaque);
365 hts_session_free (hs);
369 hts_add_segment_callback (u32 client_index, u64 segment_handle)
375 hts_del_segment_callback (u32 client_index, u64 segment_handle)
380 static session_cb_vft_t hs_session_cb_vft = {
381 .session_accept_callback = hts_ts_accept_callback,
382 .session_disconnect_callback = hts_ts_disconnect_callback,
383 .session_connected_callback = hts_ts_connected_callback,
384 .add_segment_callback = hts_add_segment_callback,
385 .del_segment_callback = hts_del_segment_callback,
386 .builtin_app_rx_callback = hts_ts_rx_callback,
387 .builtin_app_tx_callback = hs_ts_tx_callback,
388 .session_reset_callback = hts_ts_reset_callback,
389 .session_cleanup_callback = hts_ts_cleanup_callback,
393 hts_attach (hts_main_t *hm)
395 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
396 u64 options[APP_OPTIONS_N_OPTIONS];
397 vnet_app_attach_args_t _a, *a = &_a;
399 clib_memset (a, 0, sizeof (*a));
400 clib_memset (options, 0, sizeof (options));
402 a->api_client_index = ~0;
403 a->name = format (0, "http_tps");
404 a->session_cb_vft = &hs_session_cb_vft;
405 a->options = options;
406 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->segment_size;
407 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->segment_size;
408 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
409 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
410 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
412 if (vnet_application_attach (a))
415 clib_warning ("failed to attach server");
419 hm->app_index = a->app_index;
421 clib_memset (ck_pair, 0, sizeof (*ck_pair));
422 ck_pair->cert = (u8 *) test_srv_crt_rsa;
423 ck_pair->key = (u8 *) test_srv_key_rsa;
424 ck_pair->cert_len = test_srv_crt_rsa_len;
425 ck_pair->key_len = test_srv_key_rsa_len;
426 vnet_app_add_cert_key_pair (ck_pair);
427 hm->ckpair_index = ck_pair->index;
433 hts_transport_needs_crypto (transport_proto_t proto)
435 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
436 proto == TRANSPORT_PROTO_QUIC;
439 static clib_error_t *
440 hts_listen (hts_main_t *htm, u8 *listen_uri, u8 is_del)
442 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
443 vnet_listen_args_t _a, *a = &_a;
444 u8 need_crypto, *uri;
450 uri = listen_uri ? listen_uri : htm->default_uri;
451 p = hash_get_mem (htm->uri_to_handle, uri);
456 return clib_error_return (0, "not listening on %v", uri);
458 hls = hts_session_get (0, *p);
459 ls = listen_session_get (hls->vpp_session_index);
461 vnet_unlisten_args_t ua = {
462 .handle = listen_session_get_handle (ls),
463 .app_index = htm->app_index,
464 .wrk_map_index = 0 /* default wrk */
467 hash_unset_mem (htm->uri_to_handle, uri);
469 if (vnet_unlisten (&ua))
470 return clib_error_return (0, "failed to unlisten");
473 hts_session_free (hls);
479 return clib_error_return (0, "already listening %v", uri);
481 if (parse_uri ((char *) uri, &sep))
482 return clib_error_return (0, "failed to parse uri %v", uri);
484 clib_memset (a, 0, sizeof (*a));
485 a->app_index = htm->app_index;
487 need_crypto = hts_transport_needs_crypto (sep.transport_proto);
489 sep.transport_proto = TRANSPORT_PROTO_HTTP;
490 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
494 session_endpoint_alloc_ext_cfg (&a->sep_ext,
495 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
496 a->sep_ext.ext_cfg->crypto.ckpair_index = htm->ckpair_index;
499 rv = vnet_listen (a);
502 clib_mem_free (a->sep_ext.ext_cfg);
505 return clib_error_return (0, "failed to listen on %v", uri);
507 hls = hts_session_alloc (0);
508 hls->uri = vec_dup (uri);
509 ls = listen_session_get_from_handle (a->handle);
510 hls->vpp_session_index = ls->session_index;
511 hash_set_mem (htm->uri_to_handle, hls->uri, hls->session_index);
517 hts_create (vlib_main_t *vm)
519 vlib_thread_main_t *vtm = vlib_get_thread_main ();
520 hts_main_t *htm = &hts_main;
523 num_threads = 1 /* main thread */ + vtm->n_threads;
524 vec_validate (htm->sessions, num_threads - 1);
527 vec_validate (htm->test_data, (64 << 10) - 1);
529 if (hts_attach (htm))
531 clib_warning ("failed to attach server");
535 htm->default_uri = format (0, "tcp://0.0.0.0/80%c", 0);
536 htm->uri_to_handle = hash_create_vec (0, sizeof (u8), sizeof (uword));
541 static clib_error_t *
542 hts_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
543 vlib_cli_command_t *cmd)
545 unformat_input_t _line_input, *line_input = &_line_input;
546 hts_main_t *htm = &hts_main;
547 clib_error_t *error = 0;
552 /* Get a line of input. */
553 if (!unformat_user (input, unformat_line_input, line_input))
556 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
558 if (unformat (line_input, "private-segment-size %U",
559 unformat_memory_size, &mem_size))
560 htm->segment_size = mem_size;
561 else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
563 htm->fifo_size = mem_size;
564 else if (unformat (line_input, "uri %s", &uri))
566 else if (unformat (line_input, "no-zc"))
568 else if (unformat (line_input, "debug"))
569 htm->debug_level = 1;
570 else if (unformat (line_input, "del"))
574 error = clib_error_return (0, "unknown input `%U'",
575 format_unformat_error, line_input);
580 unformat_free (line_input);
587 if (htm->app_index == (u32) ~0)
589 vnet_session_enable_disable (vm, 1 /* is_enable */);
593 error = clib_error_return (0, "http tps create failed");
598 error = hts_listen (htm, uri, is_del);
606 VLIB_CLI_COMMAND (http_tps_command, static) = {
608 .short_help = "http tps [uri <uri>] [fifo-size <nbytes>] "
609 "[segment-size <nMG>] [prealloc-fifos <n>] [debug] [no-zc] "
611 .function = hts_create_command_fn,
614 static clib_error_t *
615 hts_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
616 vlib_cli_command_t *cmd)
618 unformat_input_t _line_input, *line_input = &_line_input;
619 hts_main_t *htm = &hts_main;
620 clib_error_t *error = 0;
622 hts_session_t **sessions;
623 u32 n_listeners = 0, n_sessions = 0;
625 if (!unformat_user (input, unformat_line_input, line_input))
628 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
630 if (unformat (line_input, "listeners"))
634 error = clib_error_return (0, "unknown input `%U'",
635 format_unformat_error, line_input);
645 if (htm->app_index == ~0)
647 vlib_cli_output (vm, "http tps not enabled");
656 /* clang-format off */
657 hash_foreach (uri, handle, htm->uri_to_handle, ({
658 s = format (s, "%-30v%lx\n", uri, handle);
660 /* clang-format on */
664 vlib_cli_output (vm, "%-29s%s", "URI", "Index");
665 vlib_cli_output (vm, "%v", s);
671 n_listeners = hash_elts (htm->uri_to_handle);
672 vec_foreach (sessions, htm->sessions)
673 n_sessions += pool_elts (*sessions);
675 vlib_cli_output (vm, " app index: %u\n listeners: %u\n sesions: %u",
676 htm->app_index, n_listeners, n_sessions - n_listeners);
682 VLIB_CLI_COMMAND (show_http_tps_command, static) = {
683 .path = "show http tps",
684 .short_help = "http tps [listeners]",
685 .function = hts_show_command_fn,
688 static clib_error_t *
689 hs_main_init (vlib_main_t *vm)
691 hts_main_t *htm = &hts_main;
694 htm->segment_size = 128 << 20;
695 htm->fifo_size = 64 << 10;
700 VLIB_INIT_FUNCTION (hs_main_init);
703 * fd.io coding-style-patch-verification: ON
706 * eval: (c-set-style "gnu")