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;
241 hts_start_send_data (hs, HTTP_STATUS_OK);
244 unformat_free (&input);
250 hts_ts_rx_callback (session_t *ts)
257 hs = hts_session_get (ts->thread_index, ts->opaque);
259 /* Read the http message header */
260 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
261 ASSERT (rv == sizeof (msg));
263 if (msg.type != HTTP_MSG_REQUEST || msg.method_type != HTTP_REQ_GET)
265 hts_start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
271 hts_start_send_data (hs, HTTP_STATUS_BAD_REQUEST);
275 vec_validate (request, msg.data.len - 1);
276 rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
278 if (try_test_file (hs, request))
279 hts_start_send_data (hs, HTTP_STATUS_NOT_FOUND);
287 hs_ts_tx_callback (session_t *ts)
291 hs = hts_session_get (ts->thread_index, ts->opaque);
295 hts_session_tx (hs, ts);
301 hts_ts_accept_callback (session_t *ts)
303 hts_main_t *htm = &hts_main;
306 hs = hts_session_alloc (ts->thread_index);
307 hs->vpp_session_index = ts->session_index;
309 ts->opaque = hs->session_index;
310 ts->session_state = SESSION_STATE_READY;
312 if (htm->debug_level > 0)
313 clib_warning ("Accepted session %u", ts->opaque);
319 hts_ts_connected_callback (u32 app_index, u32 api_context, session_t *s,
322 clib_warning ("called...");
327 hts_ts_disconnect_callback (session_t *ts)
329 hts_main_t *htm = &hts_main;
330 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
332 if (htm->debug_level > 0)
333 clib_warning ("Closed session %u", ts->opaque);
335 a->handle = session_handle (ts);
336 a->app_index = htm->app_index;
337 vnet_disconnect_session (a);
341 hts_ts_reset_callback (session_t *ts)
343 hts_main_t *htm = &hts_main;
344 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
346 if (htm->debug_level > 0)
347 clib_warning ("Reset session %u", ts->opaque);
349 a->handle = session_handle (ts);
350 a->app_index = htm->app_index;
351 vnet_disconnect_session (a);
355 hts_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
359 if (ntf == SESSION_CLEANUP_TRANSPORT)
362 hs = hts_session_get (s->thread_index, s->opaque);
366 hts_session_free (hs);
370 hts_add_segment_callback (u32 client_index, u64 segment_handle)
376 hts_del_segment_callback (u32 client_index, u64 segment_handle)
381 static session_cb_vft_t hs_session_cb_vft = {
382 .session_accept_callback = hts_ts_accept_callback,
383 .session_disconnect_callback = hts_ts_disconnect_callback,
384 .session_connected_callback = hts_ts_connected_callback,
385 .add_segment_callback = hts_add_segment_callback,
386 .del_segment_callback = hts_del_segment_callback,
387 .builtin_app_rx_callback = hts_ts_rx_callback,
388 .builtin_app_tx_callback = hs_ts_tx_callback,
389 .session_reset_callback = hts_ts_reset_callback,
390 .session_cleanup_callback = hts_ts_cleanup_callback,
394 hts_attach (hts_main_t *hm)
396 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
397 u64 options[APP_OPTIONS_N_OPTIONS];
398 vnet_app_attach_args_t _a, *a = &_a;
400 clib_memset (a, 0, sizeof (*a));
401 clib_memset (options, 0, sizeof (options));
403 a->api_client_index = ~0;
404 a->name = format (0, "http_tps");
405 a->session_cb_vft = &hs_session_cb_vft;
406 a->options = options;
407 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->segment_size;
408 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->segment_size;
409 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
410 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
411 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
413 if (vnet_application_attach (a))
416 clib_warning ("failed to attach server");
420 hm->app_index = a->app_index;
422 clib_memset (ck_pair, 0, sizeof (*ck_pair));
423 ck_pair->cert = (u8 *) test_srv_crt_rsa;
424 ck_pair->key = (u8 *) test_srv_key_rsa;
425 ck_pair->cert_len = test_srv_crt_rsa_len;
426 ck_pair->key_len = test_srv_key_rsa_len;
427 vnet_app_add_cert_key_pair (ck_pair);
428 hm->ckpair_index = ck_pair->index;
434 hts_transport_needs_crypto (transport_proto_t proto)
436 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
437 proto == TRANSPORT_PROTO_QUIC;
441 hts_start_listen (hts_main_t *htm, session_endpoint_cfg_t *sep, u8 *uri)
443 vnet_listen_args_t _a, *a = &_a;
449 clib_memset (a, 0, sizeof (*a));
450 a->app_index = htm->app_index;
452 need_crypto = hts_transport_needs_crypto (sep->transport_proto);
454 sep->transport_proto = TRANSPORT_PROTO_HTTP;
455 clib_memcpy (&a->sep_ext, sep, sizeof (*sep));
459 session_endpoint_alloc_ext_cfg (&a->sep_ext,
460 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
461 a->sep_ext.ext_cfg->crypto.ckpair_index = htm->ckpair_index;
464 rv = vnet_listen (a);
467 clib_mem_free (a->sep_ext.ext_cfg);
472 hls = hts_session_alloc (0);
473 hls->uri = vec_dup (uri);
474 ls = listen_session_get_from_handle (a->handle);
475 hls->vpp_session_index = ls->session_index;
476 hash_set_mem (htm->uri_to_handle, hls->uri, hls->session_index);
482 hts_stop_listen (hts_main_t *htm, u32 hls_index)
487 hls = hts_session_get (0, hls_index);
488 ls = listen_session_get (hls->vpp_session_index);
490 vnet_unlisten_args_t ua = {
491 .handle = listen_session_get_handle (ls),
492 .app_index = htm->app_index,
493 .wrk_map_index = 0 /* default wrk */
496 hash_unset_mem (htm->uri_to_handle, hls->uri);
498 if (vnet_unlisten (&ua))
502 hts_session_free (hls);
507 static clib_error_t *
508 hts_listen (hts_main_t *htm, u32 vrf, u8 *listen_uri, u8 is_del)
510 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
511 clib_error_t *error = 0;
516 uri = listen_uri ? listen_uri : htm->default_uri;
517 uri_key = format (0, "vrf%u-%s", vrf, uri);
518 p = hash_get_mem (htm->uri_to_handle, uri_key);
523 error = clib_error_return (0, "not listening on %v", uri);
524 else if (hts_stop_listen (htm, p[0]))
525 error = clib_error_return (0, "failed to unlisten");
531 error = clib_error_return (0, "already listening %v", uri);
535 if (parse_uri ((char *) uri, &sep))
537 error = clib_error_return (0, "failed to parse uri %v", uri);
546 fp = sep.is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
547 fib_index = fib_table_find (fp, vrf);
550 error = clib_error_return (0, "no such vrf %u", vrf);
553 sep.fib_index = fib_index;
556 if ((rv = hts_start_listen (htm, &sep, uri_key)))
558 error = clib_error_return (0, "failed to listen on %v: %U", uri,
559 format_session_error, rv);
569 hts_create (vlib_main_t *vm)
571 vlib_thread_main_t *vtm = vlib_get_thread_main ();
572 hts_main_t *htm = &hts_main;
575 num_threads = 1 /* main thread */ + vtm->n_threads;
576 vec_validate (htm->sessions, num_threads - 1);
579 vec_validate (htm->test_data, (64 << 10) - 1);
581 if (hts_attach (htm))
583 clib_warning ("failed to attach server");
587 htm->default_uri = format (0, "tcp://0.0.0.0/80%c", 0);
588 htm->uri_to_handle = hash_create_vec (0, sizeof (u8), sizeof (uword));
593 static clib_error_t *
594 hts_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
595 vlib_cli_command_t *cmd)
597 unformat_input_t _line_input, *line_input = &_line_input;
598 hts_main_t *htm = &hts_main;
599 clib_error_t *error = 0;
605 /* Get a line of input. */
606 if (!unformat_user (input, unformat_line_input, line_input))
609 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
611 if (unformat (line_input, "private-segment-size %U",
612 unformat_memory_size, &mem_size))
613 htm->segment_size = mem_size;
614 else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
616 htm->fifo_size = mem_size;
617 else if (unformat (line_input, "vrf %u", &vrf))
619 else if (unformat (line_input, "uri %s", &uri))
621 else if (unformat (line_input, "no-zc"))
623 else if (unformat (line_input, "debug"))
624 htm->debug_level = 1;
625 else if (unformat (line_input, "del"))
629 error = clib_error_return (0, "unknown input `%U'",
630 format_unformat_error, line_input);
635 unformat_free (line_input);
642 if (htm->app_index == (u32) ~0)
644 vnet_session_enable_disable (vm, 1 /* is_enable */);
648 error = clib_error_return (0, "http tps create failed");
653 error = hts_listen (htm, vrf, uri, is_del);
661 VLIB_CLI_COMMAND (http_tps_command, static) = {
663 .short_help = "http tps [uri <uri>] [fifo-size <nbytes>] "
664 "[segment-size <nMG>] [prealloc-fifos <n>] [debug] [no-zc] "
666 .function = hts_create_command_fn,
669 static clib_error_t *
670 hts_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
671 vlib_cli_command_t *cmd)
673 unformat_input_t _line_input, *line_input = &_line_input;
674 hts_main_t *htm = &hts_main;
675 clib_error_t *error = 0;
677 hts_session_t **sessions;
678 u32 n_listeners = 0, n_sessions = 0;
680 if (!unformat_user (input, unformat_line_input, line_input))
683 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
685 if (unformat (line_input, "listeners"))
689 error = clib_error_return (0, "unknown input `%U'",
690 format_unformat_error, line_input);
700 if (htm->app_index == ~0)
702 vlib_cli_output (vm, "http tps not enabled");
711 /* clang-format off */
712 hash_foreach (uri, handle, htm->uri_to_handle, ({
713 s = format (s, "%-30v%lx\n", uri, handle);
715 /* clang-format on */
719 vlib_cli_output (vm, "%-29s%s", "URI", "Index");
720 vlib_cli_output (vm, "%v", s);
726 n_listeners = hash_elts (htm->uri_to_handle);
727 vec_foreach (sessions, htm->sessions)
728 n_sessions += pool_elts (*sessions);
730 vlib_cli_output (vm, " app index: %u\n listeners: %u\n sesions: %u",
731 htm->app_index, n_listeners, n_sessions - n_listeners);
737 VLIB_CLI_COMMAND (show_http_tps_command, static) = {
738 .path = "show http tps",
739 .short_help = "http tps [listeners]",
740 .function = hts_show_command_fn,
743 static clib_error_t *
744 hs_main_init (vlib_main_t *vm)
746 hts_main_t *htm = &hts_main;
749 htm->segment_size = 128 << 20;
750 htm->fifo_size = 64 << 10;
755 VLIB_INIT_FUNCTION (hs_main_init);
758 * fd.io coding-style-patch-verification: ON
761 * eval: (c-set-style "gnu")